是SELECT COUNT(*)昂贵吗? [英] is SELECT COUNT(*) expensive?

查看:151
本文介绍了是SELECT COUNT(*)昂贵吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您认为在每个网页载入时,计算真正大的表格(如50K行)中的条目是否是个好主意?

Do you think it's a good idea to count entries from a really big table (like 50K rows) on each page load?

SELECT COUNT(*) FROM table

现在我有2000行,我没有看到页面加载的任何延迟:)

Right now I have like 2000 rows and seems pretty fast, I don't see any delays in page load :)

但是表应该达到50K条目...我很好奇它将如何加载然后

But the table should reach up to 50K entries... And I'm curious how it will load then

(ps:此页面显示行计数为私有,在管理界面中不公开)

(ps: this page which shows the row count is private, in a Admin interface, not public)

推荐答案


如果SELECT从一个表中检索,没有检索到其他列,并且没有WHERE子句,则COUNT(*)被优化为非常快地返回。例如:

COUNT(*) is optimized to return very quickly if the SELECT retrieves from one table, no other columns are retrieved, and there is no WHERE clause. For example:



mysql> SELECT COUNT(*) FROM student;




此优化仅适用于MyISAM表,为此存储引擎存储,可以很快访问。

This optimization applies only to MyISAM tables only, because an exact row count is stored for this storage engine and can be accessed very quickly.

来源

正如您所说,您使用MyISAM和你的查询是整个表,不管它的1或100000行。

As you said you use MyISAM and your query is for the whole table, it doesn't matter if its 1 or 100000 rows.

这篇关于是SELECT COUNT(*)昂贵吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆