-2

I wan to know the difference between getting row count via select COUNT(*) from table fetching data AND mysqli_num_rows($ofquery)(in php)?

I tried in both ways and works well. But method is faster?

user3045457
  • 164
  • 1
  • 3
  • 12

1 Answers1

0

Use COUNT, internally the server will process the request differently.

When doing COUNT, the server will only allocate memory to store the result of the count.

When using mysqli_num_rows, the server will process the entire result set, allocate memory for all those results, and put the server in fetching mode, which involves a lot of different details, such as locking.

Think of it like the following pseudo scenario:

1) Hey , how many people are in the class room? (count)

2) Hey , get me a list of all the people in the classroom, ... I'll calculate the number of people myself (mysqli_num_rows)

count is the best than mysqli_num_rows

Reference from here.

Community
  • 1
  • 1
Shailesh Katarmal
  • 2,757
  • 1
  • 12
  • 15
  • 2
    Don't copy-paste identical answers (espacially the ones using `mysql_*` instead of `mysqli_*`, even if the difference is slim in this particular case). In this case, just close the question as duplicate. – xlecoustillier Sep 24 '15 at 10:12