简体   繁体   中英

Mysql mysql_num_rows($result); alternatives

Is there a quicker query of counting how many rows there are using a WHERE statement? The current query I use in PHP is:

$result = mysql_query("SELECT SQL_NO_CACHE id FROM users WHERE user = '$tnuser'");
$total = mysql_num_rows($result);

The table engine is InnoDB and ID is primary key and user is indexed. This query is ran 300+ a second so any performance gain would be good. The total count is always changing and users are deleted/added very often.

Thank you

Usually its faster to use the aggregate functions of SQL to count something. You could for example try

SELECT COUNT(*) FROM users WHERE user = '$tnuser'

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