星号cdr mysql查询性能问题

时间:2016-10-28 13:44:49

标签: mysql sql

我遇到了一个我无法理解的问题。 我有两个查询(数据库是MySQL,只有一个表包含所有cdr数据):

SELECT * FROM cdr WHERE userfield LIKE 'audio:%' 
  AND (calldate >= '2016-10-26 00:00:00' 
  AND calldate <= '2016-10-26 23:59:59') 
  AND duration > 8 
ORDER BY uniqueid DESC LIMIT 200 OFFSET 0;

以0.05秒运行,另一个运行:

SELECT * FROM cdr WHERE userfield LIKE 'audio:%' 
  AND (calldate >= '2016-10-26 15:00:00' 
  AND calldate <= '2016-10-26 16:00:00') 
  AND duration > 8 
ORDER BY uniqueid DESC LIMIT 200 OFFSET 0;

在39.12秒内运行!! (请注意,唯一的区别在于calldate字段)

我真的不太了解MySQL的性能,所以我不知道从哪里开始,也许是索引问题?

1 个答案:

答案 0 :(得分:1)

1)为calldate字段创建索引(或者&#34;从cdr&#34显示索引;看看你是否已经有一个

http://dev.mysql.com/doc/refman/5.7/en/show-index.html

2)创建使用索引的查询。

http://dev.mysql.com/doc/refman/5.7/en/index-hints.html