采用的优化方法:
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!=''条件筛选性较高,也就是有很多这类条件,导致的筛选比较慢,试试加个索引。