优化选择案例查询

时间:2014-11-14 19:53:13

标签: mysql optimization

有没有办法可以重写或优化此查询?

SELECT
    MIN(CASE WHEN id = '001' THEN value END) Col1,  
    MIN(CASE WHEN id = '002' THEN value END) Col2,
    MIN(CASE WHEN id = '003' THEN value END) Col3,  
    MIN(CASE WHEN id = '004' THEN value END) Col4,
    MIN(CASE WHEN id = '005' THEN value END) Col5,  
    MIN(CASE WHEN id = '006' THEN value END) Col6

FROM table
WHERE id IN (001, 002, 003, 004, 005, 006) and

DATE(from_unixtime(t_stamp/1000)) "2014-11-12"

AND

value > -5 OR value > 5


GROUP BY DATE(from_unixtime(t_stamp/1000)), HOUR(from_unixtime(t_stamp/1000)), MINUTE(from_unixtime(t_stamp/1000)) DIV 60

在我添加

之后,它工作正常
AND

value > -5 OR value > 5

它停止执行,刚刚超时。 关于如何更快地进行此查询的任何建议?

====

更新,我运行了这个查询并没有关闭窗口,它确实执行但是花了太长时间 Query took 323.0750 seconds.

1 个答案:

答案 0 :(得分:0)

好像你错过了那里的剪辑:

...
AND

(value > -5 OR value > 5)
...

如果您不设置剪辑,则SQL定义为:

WHERE id IN (001, 002, 003, 004, 005, 006) and

DATE(from_unixtime(t_stamp/1000)) "2014-11-12"

AND

value > -5

OR value > 5

//编辑:

btw:为什么你搜索> -5 OR> 5? > -5意味着> 5 ...