SQL使用总结-like,MAX,MIN

1. 时间索引不容许使用like

对时间索引适应like,会时间索引变成字符串操作,成为遍历动作,失去索引价值。

错误写法:

EXPLAIN  SELECT AVG(data_value) AS avg_value  FROM `host_monitor` WHERE host_ip= '192.168.106.11' AND data_type=3 AND add_time LIKE '2017-05-19 01%';

 

正确写法:

EXPLAIN SELECT AVG(data_value) AS avg_value  FROM `host_monitor` WHERE host_ip= '192.168.106.11' AND data_type=4  AND add_time >= '2017-05-19 01:00:00' AND add_time<='2017-05-19 01:59:59';

2. 索引不容许使用MaxMin

直接对索引采用采用MAXmin关键字,将执行遍历操作,索引毫无价值。

 错误写法:

EXPLAIN SELECT MAX(add_time)  FROM host_monitor  WHERE host_ip = '192.168.106.11' AND data_type = 6

正确写法:

EXPLAIN SELECT add_time  FROM host_monitor  WHERE host_ip = '192.168.106.11' AND data_type = 6 ORDER BY  add_time  DESC LIMIT 1

 

 

 

posted @ 2017-06-17 13:03  Rainyn  阅读(484)  评论(0编辑  收藏  举报