简体   繁体   English

MySQL选择计数(唯一ID)非常慢

[英]MySQL select count(distinct id) is very slow

I have a MyISAM table with about 70 million records. 我有一个MyISAM表,大约有7000万条记录。 When I do select count(distinct id) it takes about 80 seconds to get the query results. 当我select count(distinct id)时,大约需要80秒才能获得查询结果。 This table is a de-normalized table, that's why I need to get the unique count for id, and it has to be done dynamically. 此表是反规范化的表,这就是为什么我需要获得ID唯一的计数,它具有动态地进行。 If I add a where clause, depending on the range I give, it takes a shorter time between 4 - 90 secs. 如果我添加where子句,具体取决于我给出的范围,则它花费的时间会较短,介于4-90秒之间。

I'm wondering if there is any way I can optimize this to improve the query speed. 我想知道是否有什么方法可以优化它以提高查询速度。

尝试SELECT SQL_CALC_FOUND_ROWS DISTINCT(id) FROM ... ,然后执行查询SELECT FOUND_ROWS() AS Total以获取结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM