如何查看执行较慢的sql:
壹:
1、使用show variables like '%query%';
查询结果:
ft_query_expansion_limit 20
have_query_cacheYES
long_query_time10.000000
query_alloc_block_size8192
query_cache_limit1048576
query_cache_min_res_unit4096
query_cache_size15728640
query_cache_typeON
query_cache_wlock_invalidateOFF
query_prealloc_size8192
slow_query_logOFF
slow_query_log_fileD:\MySQL\MySQL Server 5.5\Data\某某-slow.log
2、将slow_query_log 设置为ON
使用 set global slow_query_log = ON
3、根据slow_query_log_file 日志文件地址查看:
所有long_query_time 执行10.000000s以上的sql都会记录
4、永久更改慢sql查询时间极限:
在配置文件my.ini中添加 long_query_time = 几(数值) 即可----更改完成需重启数据库
贰:
使用explain
在要执行的sql前加上explain
结果如下:
看type:
type快到慢的排序:
system > const > eq_ref > ref > fulltext > ref_or_null > index_merge > unique_subquery > index_subquery > range > index > all
如果是后面两个就需要优化