简体   繁体   中英

MySQL select count(distinct id) is very slow

I have a MyISAM table with about 70 million records. When I do select count(distinct id) it takes about 80 seconds to get the query results. 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. If I add a where clause, depending on the range I give, it takes a shorter time between 4 - 90 secs.

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以获取结果。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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