是否有最好的方法来计算表mysql中的所有行?

时间:2014-07-11 16:26:07

标签: php mysql sql

我想计算表中与我的条件匹配的所有行,就像mysql一样快 所以,我有四个SQL,并希望你解释所有这些,每个SQL有什么不同?
哪个是查询时间和服务器性能最快或最好的?或者它有另一种方法可以比这些更好。谢谢。

select count(*) as total from table where my_condition
select count(1) as total from table where my_condition
select count(primary_key) as total from table where my_condition

select * from table where my_condition
//and then use mysql_num_rows()

1 个答案:

答案 0 :(得分:0)

首先,不要考虑做最后一个!它的字面意思是选择我在数据库中的所有数据,将其返回给客户端,客户端将对它们进行计数!

其次,您需要了解如果按列名计数,count将不会返回空值,例如:select table(column1)from table_name;如果有一行其中column1为null,则不计算它,所以要小心。

选择计数(*),选择计数(1),选择计数(primary_key)都是相等的,你将看不出任何差别。