简体   繁体   中英

MYSQL Best Row Count Method - Large Database

I have a MySQL database that contains over 400,000 rows. For my web based script, I have a page function. One of the steps to determine how many pages there should be is returning the number of rows in the table.

Let's pretend the table name is data .

I'm wondering what is the most efficient method to ONLY return the number of rows in the database.

I could obviously do something like:

$getRows = mysql_query("SELECT id FROM `data`") or die(mysql_error());
$rows = mysql_num_rows($getRows);

So that it only selects the id. But still, that will be selecting 400,000 + ID's worth of data and storing it on the stack (i think?) and seems less efficient as using a method such as finding the table status. I'm just not 100% sure how to use the table status method.

Feedback & opinions would be awesome. Thanks guys!

use count

SELECT count(id) FROM data

See this question for more info on getting counts. Make sure your id has an index in your table.


Now, to find the number of unique rows, you can do

SELECT count(distinct(id)) FROM data


alternatively, if you want to find the highest ID number (if you ID are autoincremental and unique) you can try SELECT max(id) FROM data to return the highest ID number present.


I'd highly recommend this site to learn these basic functions: http://sqlzoo.net/

400,000 rows is not a lot at all. Keep it simple and just do:

select count(*) 
from `data`

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM