2012-12-21 42 views
0

我有在mysql_slow_queries查詢日誌如下:MYSQL解釋Rows_examined不匹配

Query_time:4.642323 Lock_time:1.821996 Rows_sent:14 Rows_examined:27099

SET TIMESTAMP=1356068688; 
SELECT gw.id website_id, gw.name,gw.url,gw.language,gw.title,gw.nickname, gd.id, 
     gd.deal_title, gd.cdeal_title, gd.deal_details, gd.cdeal_details, 
     gd.discount_price, gd.original_price, gd.savings, gd.expiry, gd.shop, 
     gd.location, gd.clocation, gd.limited_offer, gd.contact, gd.url website, 
     gd.affiliate_url, gd.tags, gd.pic_url, gd.featured, gd.top_pos, 
     gd.sub_pos, gd.appeal, gd.redeem_until, gd.noofpurchased 
FROM groupon_deals gd 
INNER JOIN groupon_websites gw ON gw.id=gd.groupon_websites_id 
WHERE gd.tags LIKE '%technology-and-gadgets%' AND gd.pubDate >= SYSDATE() - INTERVAL 24 HOUR AND 
     gd.hidden = 0 AND gd.pubDate < SYSDATE() AND 
     gd.id NOT IN (1,30079,30090,30116,30118,30070,30136,30137,30138,30156,30103,30157,30038,30044,30084,30025,30013,30111,30030,30020,30059,30087,30026,30016,30112,30031,30021,30005,30092,30027,30017,30113,30049,30032,30023,30006,30096,30040,30028,30018,30120,30033,30024,30008,30110,30029,30019,30128,30131,30129,30100,30004,29995,30076,30126,30069,30078,30071,30034,30080,30065,30073,30082,29987,30074,30117,30068,29981,30098,30102,30088,30119,30135,30155,30107,29997,30041,30046,30077,30003,29992,30058,30097,30014,29999,30066,30127,30009,30081,29993,30060,30015,30114,30000,29985,30099,30010,30083,29994,30061,30022,30115,30001,30072,29986,30011,30086,30062,30123,30002,30075,29990,30054,30160,30094,30012,29998,30064,30125,30039,30130,30134,29982,30159,30048,30047,30158,30043,30101,30104,30106,30122,30056,30057,30063,30161,30053,29984,30132,30109,30036,30108,30037,30121,30045,30124) AND 
     (gw.language = 'C' OR gw.language = 'B') 
ORDER BY gd.sub_pos,gd.noofpurchased DESC 

現在,當我去phpMyAdmin的使用EXPLAIN運行相同的查詢,我來到這裏的輸出: http://algaryeung.com/temp/explain-output.jpg

我有2個問題:

1)日誌中的rows_examined如何與27099中的rows_examined相比,756是不同的?我是否需要在EXPLAIN中多取2個值才能檢查真實的行?

2)我知道這是一種開放式,但我會如何去改善現有的查詢?我已經將字段groupon_deals.groupon_websites_id編入索引,並且我認爲可能有某種方法可以改進查詢中的NOT IN部分。不期待在這裏有完整的答案,但任何想法開始挖掘/學習?

回答

2

MySQL EXPLAIN提供了對每個步驟結果返回的行數的預測,即預測。

EXPLAIN真正爲您提供的是執行計劃,即將要使用的訪問路徑,操作順序以及將使用哪些索引。它實際上並沒有處理語句以獲得準確的行計數,它僅基於其關於表中的行數的信息以及列中的值的基數和分佈來預測將檢索多少行。

根據您提供的EXPLAIN輸出,該查詢正在對groupon_websites表進行全面掃描。對於檢索到的每個id值(不由謂詞消除),MySQL正在groupon_deals表的groupon_websites_id列上執行索引查找。


對於此查詢,性能可能會提高一點與索引

... ON groupon_deals (groupon_websites_id, hidden, pubDate, id) 

我認爲一個良好的開端「挖」是理解EXPLAIN聲明。

如果您對MySQL如何處理SQL語句,MySQL可以執行哪些操作以及哪些操作可以使用合適的索引有一定了解,那麼這是理解來自MySQL的輸出的基礎。 EXPLAIN

我建議從這裏開始,MySQL的文檔:Understanding the Query Execution Plan

+0

感謝您的回答,知道EXPLAIN並沒有真正處理語句,而是給出了一個計劃,解釋了很多(沒有雙關語意)。 – Algar