SELECT COUNT(*) 贵吗?

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

问题描述

您认为在每次加载页面时计算一个非常大的表(例如 50K 行)中的条目是否是个好主意?

SELECT COUNT(*) FROM table

现在我有大约 2000 行,而且看起来非常快,我没有看到页面加载有任何延迟 :)

但该表最多应达到 50K 个条目......我很好奇它会如何加载

(ps:这个显示行数的页面是私有的,在管理界面中,不是公开的)

解决方案

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

mysql>从学生中选择 COUNT(*);

<块引用>

此优化仅适用于 MyISAM 表,因为此存储引擎存储了准确的行数,并且可以非常快速地访问.

来源

正如您所说,您使用 MyISAM 并且您的查询是针对整个表的,它是 1 行还是 100000 行都没有关系.

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

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

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

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

解决方案

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;

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.

Source

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(*) 贵吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Hibernate reactive No Vert.x context active in aws rds(AWS RDS中的休眠反应性非Vert.x上下文处于活动状态)
Bulk insert with mysql2 and NodeJs throws 500(使用mysql2和NodeJS的大容量插入抛出500)
Flask + PyMySQL giving error no attribute #39;settimeout#39;(FlASK+PyMySQL给出错误,没有属性#39;setTimeout#39;)
auto_increment column for a group of rows?(一组行的AUTO_INCREMENT列?)
Sort by ID DESC(按ID代码排序)
SQL/MySQL: split a quantity value into multiple rows by date(SQL/MySQL:按日期将数量值拆分为多行)