Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using count(*) vs num_rows

Tags:

php

mysql

To get number of rows in result set there are two ways:

  1. Is to use query to get count

    $query="Select count(*) as count from some_table where type='t1'";

    and then retrieving the value of count.

  2. Is getting count via num_rows(), in php.

so which one is better performance wise?

like image 539
Manish Avatar asked Aug 23 '13 18:08

Manish


People also ask

What is num_rows in Dba_tables?

The num_rows value was created to assist the cost-based SQL optimizer in determining the optimal execution plan for any SQL that accesses the table. The num_rows column of the dba_tables view can be set with any of these commands: analyze table FRED; EXEC DBMS_UTILITY.

How do I count rows in SQL?

Use the COUNT aggregate function to count the number of rows in a table. This function takes the name of the column as its argument (e.g., id ) and returns the number of rows for this particular column in the table (e.g., 5).

How does count * Work SQL?

The SQL COUNT() function returns the number of rows in a table satisfying the criteria specified in the WHERE clause. It sets the number of rows or non NULL column values. COUNT() returns 0 if there were no matching rows. The above syntax is the general SQL 2003 ANSI standard syntax.


4 Answers

There are a few differences between the two:

  1. num_rows is the number of result rows (records) received.
  2. count(*) is the number of records in the database matching the query.

The database may be configured to limit the number of returned results (MySQL allows this for instance), in which case the two may differ in value if the limit is lower than the number of matching records. Note that limits may be configured by the DBA, so it may not be obvious from the SQL query code itself what limits apply.

Using num_rows to count records implies "transmitting" each record, so if you only want a total number (which would be a single record/row) you are far better off getting the count instead.

Additionally count can be used in more complex query scenario's to do things like sub-totals, which is not easily done with num_rows.

like image 150
user268396 Avatar answered Oct 04 '22 21:10

user268396


If your goal is to actually count the rows, use COUNT(*). num_rows is ordinarily (in my experience) only used to confirm that more than zero rows were returned and continue on in that case. It will probably take MySQL longer to read out many selected rows compared to the aggregation on COUNT too even if the query itself takes the same amount of time.

like image 35
Explosion Pills Avatar answered Oct 04 '22 21:10

Explosion Pills


count is much more efficient both performance wise and memory wise as you're not having to retrieve so much data from the database server. If you count by a single column such as a unique id then you can get it a little more efficient

like image 32
Matthew Mcveigh Avatar answered Oct 04 '22 21:10

Matthew Mcveigh


It depends on your implementation. If you're dealing with a lot of rows, count(*) is better because it doesn't have to pass all of those rows to PHP. If, on the other hand, you're dealing with a small amount of rows, the difference is negligible.

like image 37
Michael Brook Avatar answered Oct 01 '22 21:10

Michael Brook