想要取出goods表中的不重復的cas的對應的goods_id資料。
goods_id是主鍵 cas和is_check 和 is_sale 都建了常規索引
select goods_id from hxj_goods where is_check=1 and is_sale=1 group by cas limit 51992,8 --- 執行了29秒。
select goods_id from hxj_goods where is_check=1 and is_sale=1 group by cas limit 0,8 ---執行了0.002秒。
select goods_id from (SELECT goods_id,cas from hxj_goods WHERE is_check=1 and is_sale=1) a group by cas limit 0,8----執行了16秒 ,如果limit更大 就更慢,比前面那一潭訓差。
select goods_id from hxj_goods where is_check=1 and is_sale=1 limit 235472,8 ---執行了0.7秒 去掉group by條件后,但是結果不是想要的。
select distinct(cas),goods_id from hxj_goods where is_check=1 and is_sale=1 limit 312,8 ---執行了0.001秒 使用distinct ,但是他的結果是對沒一頁的資料進行去重,不是全部資料去重再分頁。
select goods_id from (select distinct(cas),goods_id from hxj_goods where is_check=1 and is_sale=1) a limit 16,8 ----執行了8秒 很慢。
uj5u.com熱心網友回復:
這個快慢其實和 group by cas 和 limit有關系。第2個之所以快,相對于第1個,是group by生成了8條資料,就直接回傳了,而第一個,的至少生成51992+8 條資料后,才能回傳資料。
uj5u.com熱心網友回復:
子查詢在 5。7 的版本中有改進,2/3 應該不會有那么大的差距至于其他情況的性能差異,其實你自己寫的已經很清楚了,不涉及 group by , 或者在 group by 前分頁,性能差異當然小,如果 group by 之后 再分頁,當然是越后的頁,需要 group by 的資料越多,性能下載越厲害
uj5u.com熱心網友回復:
2/3 你可以看下執行計劃,在 5.7 版本中,這兩個的執行計劃應該一樣的( 之前看到過本版的一個問題,與 2/3 的查詢類似,帳主提供的 5.6 的執行計劃,子查詢是兩個步驟,在 5.7中,是一個步驟)
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/115307.html
標籤:MySQL
上一篇:關于SQL server2008與MySQL資料同步問題!
下一篇:mysql 死鎖問題
