我在 mysql_slow_queries 日志中有如下查询:
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
我有两个问题:
1) 为什么日志中的 rows_examined 是 27099 与 EXPLAIN 37、756 中的 rows_examined 不同?我是否需要将 EXPLAIN 中的 2 个值乘以检查实际行?
2)我知道这是一种开放式的,但我将如何改进现有的查询?我已经索引了字段 groupon_deals.groupon_websites_id 并且我认为可能有一些方法可以改进查询的 NOT IN 部分。不期待这里有完整的答案,但知道从哪里开始挖掘/学习吗?