简体   繁体   English

由 200 万条记录组成的 Mysql 查询组需要 7 秒

[英]Mysql query group by 2 million records takes 7 seconds

I had a query with group by condition takes 7 seconds.我有一个按条件分组的查询需要 7 秒。 I had optimized all its data type and also given index.我已经优化了它的所有数据类型并给出了索引。

Before index Execution time: 15 sec After index and optimize data type its taken time: 7 seconds.索引前执行时间:15 秒索引和优化数据类型后其花费时间:7 秒。

My database is much larger and I have to run approx 15 queries to prepare my report the single query takes 7 sec then 15*7 = 105 sec it's too much time-consuming.我的数据库要大得多,我必须运行大约 15 个查询来准备我的报告,单个查询需要 7 秒,然后 15*7 = 105 秒,这太耗时了。

Sample Query:示例查询:

Select 
    ca_id, 
    count(ca_id),
    email,
    cadate
from
    tbl_zonec
group by 
    email, 
    cadate,
    ca_id 

Is there any way to optimize the query performance?有什么办法可以优化查询性能吗?

  • Get rid of id ;去掉id it is just plain wrong not to have it in the GROUP BY .GROUP BY没有它是完全错误的。 Ditto for sample . sample同上。

  • If ca_id cannot be NULL , say COUNT(*) .如果ca_id不能为NULL ,请说COUNT(*)

  • You seem to be scanning the entire table every time.您似乎每次都在扫描整个表格。 Do you get different results?你得到不同的结果吗? Is the table being added to?是否正在添加表? Is old data being modified or deleted, or is this effectively a write-once table?旧数据是否被修改或删除,或者这实际上是一个写一次表? If the latter, then consider building and maintaining a Summary table ;如果是后者,则考虑构建和维护汇总表 it will be a lot faster.它会快很多。 (Perhaps 10-fold.) (可能是 10 倍。)

  • Please provide SHOW CREATE TABLE .请提供SHOW CREATE TABLE What is the PRIMARY KEY ?什么是PRIMARY KEY If it is does not start with email, cadate, ca_id , then there is an improvement possible.如果它不是email, cadate, ca_id ,那么可能会有改进。

  • Without changing the PK, this would run a little faster: INDEX(email, cadate, ca_id, sample, id) -- it is optimal for the GROUP BY and it is "covering".如果不更改 PK,这会运行得更快一点: INDEX(email, cadate, ca_id, sample, id) -- 它是GROUP BY最佳选择,它是“覆盖”的。

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

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