mysql 8000+分页点击任何一页响应控制在3-4s之间,请问如何继续优化?
采用的优化方法:
limit优化,先取出分页对应的ID,然后根据字段
值,然后再limit 索引
myisam引擎
附上代码:
查询数据总数:
下图是查询分页对应的ID:
下图是查询结果:
---------------2016.7.6 17:47更新--------------------
like那个地方确实不该要,已经做了处理,不该要的不要了。代码图也更新了。现在响应能保持在2.5s以下
执行计划如下:这是第一条语句,查询数据总量
SELECT `r`.`id` FROM (`samplerecord` as r) LEFT JOIN `statementsample` as s ON `r`.`sample_id`=`s`.`id` LEFT JOIN `breed` as b ON `s`.`food_id`=`b`.`id` LEFT JOIN `user` as u ON `r`.`user_id`=`u`.`id` WHERE `s`.`code` != ''
第二条语句 查询分页首位字段ID
SELECT `r`.`id` as id FROM (`samplerecord` as r) LEFT JOIN `statementsample` as s ON `r`.`sample_id`=`s`.`id` LEFT JOIN `breed` as b ON `s`.`food_id`=`b`.`id` LEFT JOIN `user` as u ON `r`.`user_id`=`u`.`id` WHERE `s`.`code` != '' ORDER BY `id` DESC LIMIT 1 OFFSET 5988
第三条语句:查询数据结果集
SELECT `r`.`id` as id, `s`.`code` as code, `b`.`breed_name`, `r`.`state`, `u`.`username`, `r`.`recordtime`, `r`.`remark` FROM (`samplerecord` as r) LEFT JOIN `statementsample` as s ON `r`.`sample_id`=`s`.`id` LEFT JOIN `breed` as b ON `s`.`food_id`=`b`.`id` LEFT JOIN `user` as u ON `r`.`user_id`=`u`.`id` WHERE `s`.`code` != '' AND `r`.`id`
请各位大神帮忙分析下...
回复内容:
采用的优化方法:
limit优化,先取出分页对应的ID,然后根据字段
值,然后再limit 索引
myisam引擎
附上代码:
查询数据总数:
下图是查询分页对应的ID:
下图是查询结果:
---------------2016.7.6 17:47更新--------------------
like那个地方确实不该要,已经做了处理,不该要的不要了。代码图也更新了。现在响应能保持在2.5s以下
执行计划如下:这是第一条语句,查询数据总量
SELECT `r`.`id` FROM (`samplerecord` as r) LEFT JOIN `statementsample` as s ON `r`.`sample_id`=`s`.`id` LEFT JOIN `breed` as b ON `s`.`food_id`=`b`.`id` LEFT JOIN `user` as u ON `r`.`user_id`=`u`.`id` WHERE `s`.`code` != ''
第二条语句 查询分页首位字段ID
SELECT `r`.`id` as id FROM (`samplerecord` as r) LEFT JOIN `statementsample` as s ON `r`.`sample_id`=`s`.`id` LEFT JOIN `breed` as b ON `s`.`food_id`=`b`.`id` LEFT JOIN `user` as u ON `r`.`user_id`=`u`.`id` WHERE `s`.`code` != '' ORDER BY `id` DESC LIMIT 1 OFFSET 5988
第三条语句:查询数据结果集
SELECT `r`.`id` as id, `s`.`code` as code, `b`.`breed_name`, `r`.`state`, `u`.`username`, `r`.`recordtime`, `r`.`remark` FROM (`samplerecord` as r) LEFT JOIN `statementsample` as s ON `r`.`sample_id`=`s`.`id` LEFT JOIN `breed` as b ON `s`.`food_id`=`b`.`id` LEFT JOIN `user` as u ON `r`.`user_id`=`u`.`id` WHERE `s`.`code` != '' AND `r`.`id`
请各位大神帮忙分析下...
能不能把sql,执行计划贴出来
在事务里执行会提高效率的
1.先把表连接查询拆成简单查询
2.like有没有必要?
初步来看3个sql:
1.总量的sql,samplerecord没走任何索引,全表扫描,必然会慢些
2.查询id的sql,排序+全表,预计也慢
3.最后有一个应该还好
所以整个来说就是避免全表扫描,如果确实要全表数据,那么limit不会慢到哪儿去。
最终猜测,code!=''条件筛选性较高,也就是有很多这类条件,导致的筛选比较慢,试试加个索引。