-
通过explain 查出执行情况,是否使用索引
-
查询sql语句耗时:
set profiling = 1; /SQL/; show profiles;set profiling = 0;
- 查看具体环节耗时
show profile Query 1;
- 延迟关联,通过在索引中筛选出符合条件的主键,然后通过主键获取所有行数。
SELECT a.* FROM log_warn a, (select id_log_warn from log_warn where stop_time>='2017-10-06 10:58:11' and start_time<='2017-10-13 10:58:11' order by start_time limit 0,20 ) b where a.id_log_warn=b.id_log_warn;
select * from log_warn join (select id_log_warn from log_warn where stop_time>='2017-09-06 10:58:11' and start_time<='2017-09-13 10:58:11' order by start_time limit 0,20) as t using(id_log_warn);
-
统计行数:
添加辅助索引,并强制使用 (force index) 辅助索引进行统计 -
复合索引:
将常用的多列合并作为一个索引,并把最常用的列放在复合索引内的最左边,可以涵盖多种查询条件。
firstname,lastname,age
firstname,lastname
firstname
网友评论