简体   繁体   English

Mysql mysql_num_rows($ result); 备择方案

[英]Mysql mysql_num_rows($result); alternatives

Is there a quicker query of counting how many rows there are using a WHERE statement? 是否有更快的查询使用WHERE语句计算有多少行? The current query I use in PHP is: 我在PHP中使用的当前查询是:

$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. 表引擎是InnoDB,ID是主键,用户被索引。 This query is ran 300+ a second so any performance gain would be good. 此查询每秒运行300次以上,因此任何性能提升都会很好。 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. 通常,使用SQL的聚合函数来计算某些内容会更快。 You could for example try 你可以试试

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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