使用count(*)vs num_rows [英] Using count(*) vs num_rows
问题描述
要获得结果集中的行数,有两种方法:
To get number of rows in result set there are two ways:
-
是使用查询来获取计数
Is to use query to get count
$query="Select count(*) as count from some_table where type='t1'";
然后获取count的值.
and then retrieving the value of count.
正在通过php中的num_rows()进行计数.
Is getting count via num_rows(), in php.
那么哪个是更好的性能明智的选择?
so which one is better performance wise?
推荐答案
如果您的目标是实际计算行数,请使用COUNT(*)
. num_rows
通常(以我的经验)仅用于确认返回了多于零的行,并在这种情况下继续执行.与查询COUNT
上的聚合相比,MySQL可能要花费更长的时间来读取许多选定的行,即使查询本身花费相同的时间.
If your goal is to actually count the rows, use COUNT(*)
. num_rows
is ordinarily (in my experience) only used to confirm that more than zero rows were returned and continue on in that case. It will probably take MySQL longer to read out many selected rows compared to the aggregation on COUNT
too even if the query itself takes the same amount of time.
这篇关于使用count(*)vs num_rows的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!