採用的最佳化方法:
limit最佳化,先取出分頁對應的ID,再依照欄位
索引
myisam引擎
附上代碼:
查詢資料總數:
下圖是查詢分頁對應的ID:
下圖是查詢結果:
---------------2016.7.6 17:47更新--------------------
like那個地方確實不該要,已經做了處理,不該要的不要了。 代碼圖也更新了。現在響應能維持在2.5s以下
執行計畫如下:這是第一條語句,查詢資料總量
<code>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` != ''</code>
第二條語句 查詢分頁首位欄位ID
<code>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</code>
第三條語句:查詢資料結果集
<code>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` < '109541' ORDER BY `id` DESC LIMIT 12</code>
請各位大神幫忙分析下...
採用的最佳化方法:
limit最佳化,先取出分頁對應的ID,再依照欄位
索引
myisam引擎
附上代碼:
查詢資料總數:
下圖是查詢分頁對應的ID:
下圖是查詢結果:
---------------2016.7.6 17:47更新--------------------
like那個地方確實不該要,已經做了處理,不該要的不要了。 代碼圖也更新了。現在響應能維持在2.5s以下
執行計畫如下:這是第一條語句,查詢資料總量
<code>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` != ''</code>
第二條語句 查詢分頁首位欄位ID
<code>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</code>
第三條語句:查詢資料結果集
<code>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` < '109541' ORDER BY `id` DESC LIMIT 12</code>
請各位大神幫忙分析下...
能不能把sql,執行計畫貼出來
在事務裡執行會提高效率的
1.先把表連接查詢拆成簡單查詢
2.like有沒有必要?
初步來看3個sql:
1.總量的sql,samplerecord沒走任何索引,全表掃描,必然會慢些
2.查詢id的sql,排序+全表,預計也慢
3.最後有一個應該還好
所以整個來說就是避免全表掃描,如果確實要全表數據,那麼limit不會慢到哪裡去。
最後猜測,code!=''條件篩選性較高,也就是有很多這類條件,導致的篩選比較慢,試試看加個索引。