If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > Record count

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 08-22-08, 15:45
RandyRiegel RandyRiegel is offline
Registered User
 
Join Date: Sep 2003
Posts: 22
Record count

What is the most efficient way to find the record count of a large table?

Here are the two way's i've did it, would just like opinions on which one is better and more efficient. I'm coding PHP which shouldn't make a difference I'm just wondering about the SQL syntax.

Code:
select count(*) as myCount from myTable
or
Code:
select * from myTable
in the above after selecting all I use the mysql_num_rows function.

Randy
Reply With Quote
  #2 (permalink)  
Old 08-22-08, 18:56
shammat shammat is offline
Registered User
 
Join Date: Nov 2003
Posts: 2,407
The first returns only a single row (and column)

The second returns the entire data from the table.

Now think about which is more efficient...
Reply With Quote
  #3 (permalink)  
Old 08-28-08, 09:07
RandyRiegel RandyRiegel is offline
Registered User
 
Join Date: Sep 2003
Posts: 22
That's what I figured but I also figured it had to go through the whole table to get the count.
Reply With Quote
  #4 (permalink)  
Old 08-28-08, 09:30
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
Quote:
Originally Posted by RandyRiegel
That's what I figured but I also figured it had to go through the whole table to get the count.
no, it doesn't always do that -- myisam tables store the count, so getting the count is lightning fast

but even if the database has to count all the rows, that's still a lot faster than exporting all the rows to let you count them
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On