Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mysql mysql_num_rows($result); alternatives

Tags:

php

mysql

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

like image 933
chillers Avatar asked Nov 25 '25 12:11

chillers


1 Answers

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'
like image 77
Ace7k3 Avatar answered Nov 27 '25 00:11

Ace7k3



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!