简体   繁体   English

如何避免sql查询中的临时表?

[英]How to avoid temporary table in sql query?

I am running a query (taking 3 minutes to execute)-我正在运行一个查询(执行需要 3 分钟)-

SELECT c.eveDate,c.hour,SUM(c.dataVolumeDownLink)+SUM(c.dataVolumeUpLink) 
FROM cdr c 
WHERE c.evedate>='2013-10-19'
GROUP BY c.hour;

with explain plan -与解释计划 -

id  select_type  table  type  possible_keys                 key  key_len  ref  rows      Extra                                         
1   SIMPLE       c      ALL   evedate_index,eve_hour_index                     31200000  Using where; Using temporary; Using filesort  

I am using table(Myisam)-我正在使用表(Myisam)-

Primary key(id,evedate),主键(id,evedate),

with weekly 8 partitions with key evedate,每周有 8 个分区,带有密钥 evedate,

index key- on evedate,索引键在 evate,

composite index on evedate,hour. evate,hour 上的综合指数。

I have changed the mysql tuning parameters from my.ini as (4GB RAM)-我已将 my.ini 中的 mysql 调整参数更改为(4GB RAM)-

tmp_table_size=200M tmp_table_size=200M

key_buffer_size=400M key_buffer_size=400M

read_rnd_buffer_size=2M read_rnd_buffer_size=2M

But still its using temporary table and file sort.但仍然使用临时表和文件排序。 Please let me know what should I do to exclude this.请让我知道我该怎么做才能排除这种情况。

After adding new composite index(evedate,msisdn)添加新的复合索引(evedate,msisdn)后

I have found some changes in few queries they were not using any temporary case, even in above query if I omit group by clause, its not using temporary table.我发现一些查询中有一些变化,他们没有使用任何临时案例,即使在上面的查询中,如果我省略 group by 子句,它也没有使用临时表。

You cannot do anything.你什么也做不了。 MySql is not able to optimize this query and avoid temporaty table. MySql 无法优化此查询并避免临时表。

According to this link: http://dev.mysql.com/doc/refman/5.7/en/group-by-optimization.html根据此链接: http : //dev.mysql.com/doc/refman/5.7/en/group-by-optimization.html
there are two methods MySql is using to optimize GROUP BY. MySql 使用两种方法来优化 GROUP BY。


The first method - Loose Index Scan - cannot be used for your query because this condition is not meet:第一种方法 -松散索引扫描- 不能用于您的查询,因为不满足此条件:

The only aggregate functions used in the select list (if any) are MIN() and MAX() ....选择列表中使用的唯一聚合函数(如果有)是 MIN() 和 MAX() ....

Your query contains SUM, therefore MySql cannot use the above optimalization method.您的查询包含 SUM,因此 MySql 无法使用上述优化方法。


The second method - Tight Index Scan - cannot be used for your query, because this condition is not meet:第二种方法 -紧密索引扫描- 不能用于您的查询,因为不满足此条件:

For this method to work, it is sufficient that there is a constant equality condition for all columns in a query referring to parts of the key coming before or in between parts of the GROUP BY key.要使此方法起作用,查询中的所有列都有一个恒定的相等条件就足够了,这些列引用了 GROUP BY 键部分之前或之间的键部分。

Your query is using only a range operator : WHERE c.evedate>='2013-10-19' , there is no any equality condition in the WHERE clause, therefore this method cannot be used to optimize the query.您的查询仅使用范围运算符: WHERE c.evedate>='2013-10-19' ,WHERE 子句中没有任何相等条件,因此无法使用此方法优化查询。

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

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