0

我在 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 部分。不期待这里有完整的答案,但知道从哪里开始挖掘/学习吗?

4

1 回答 1

2

MySQLEXPLAIN提供了对每个步骤将返回的行数的估计和预测。

真正为您EXPLAIN提供的是执行计划,即将使用的访问路径、操作顺序以及将使用哪些索引。它实际上并不处理该语句以获得准确的行计数,它仅根据它所拥有的有关表中的行数以及列中值的基数和分布的信息来预测将检索多少行。

根据您提供的 EXPLAIN 输出,查询正在对groupon_websites表进行全面扫描。对于检索到的每个值(不会被谓词消除),MySQL 正在表的列id上执行索引查找。groupon_websites_idgroupon_deals


对于此查询,使用索引可能会提高性能

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

我认为开始“挖掘”的一个好地方是理解该EXPLAIN声明。

如果您对 MySQL 实际如何处理 SQL 语句、MySQL 可以执行哪些“操作”以及哪些“操作”可以使用合适的索引有一定的了解,这是了解EXPLAIN.

我建议从这里开始,在 MySQL 文档中:Understanding the Query Execution Plan

于 2012-12-21T06:07:18.747 回答