1

我有一个 mysql sql select 需要很长时间才能返回数据。

╔════════════════╗    ╔════════════════╗
║ ITEM           ║    ║ Workspace      ║
╠════════════════║    ╠════════════════║
║ id             ║    ║ id             ║
║ guid           ║    ║ guid           ║
║ workspace_id   ║    ║ company_id     ║
║ deleted        ║    ║ deleted        ║
╚════════════════╝    ╚════════════════╝
Indexes: id, guid     Indexes: id, guid,
 workspace_id          company_id


╔════════════════╗    ╔════════════════════╗
║ COMPANY        ║    ║ item_category_xref ║
╠════════════════║    ╠════════════════════║
║ id             ║    ║ item_id            ║
║ deleted        ║    ║ category_id        ║
╚════════════════╝    ╚════════════════════╝
Indexes: id           Indexes: item_id, category_id

╔════════════════╗    ╔═════════════════════╗
║ item_image     ║    ║ tracking_action     ║
╠════════════════║    ╠═════════════════════║
║ item_id        ║    ║ id                  ║
║ sequence       ║    ║ guid                ║
╚════════════════╝    ║ action              ║
Indexes:              ║ context             ║
 (item_id, sequence)  ║ deleted             ║
                      ╚═════════════════════╝

SQL

    SELECT
        itm.id "item.id",
        ws.id "workspace.id", 
        co.id "company.id", 
       ((SELECT count(*) FROM item_category_xref icx
          WHERE icx.item_id = itm.id
            AND icx.featured = 1) > 0) "featured",
        (SELECT COUNT(*) FROM tracking_action ta1
          WHERE ta1.context = 'ITEM'
            AND ta1.context_guid = itm.guid
            AND ta1.action = 'VIEW') ta_view_count ,
        (SELECT COUNT(*) FROM tracking_action ta2
          WHERE ta2.context = 'ITEM'
            AND ta2.context_guid = itm.guid
            AND ta2.action = 'SEARCH_RESULT') ta_search_count 
     FROM item itm 
     JOIN workspace ws
            ON itm.workspace_id = ws.id
            AND ws.deleted != 1
     JOIN company co
            ON ws.company_id = co.id
            AND co.deleted != 1
     JOIN item_category_xref icx
            ON itm.id = icx.item_id
            AND icx.category_id = 1
     LEFT JOIN item_image ii
            ON itm.id = ii.item_id
            AND ii.sequence = 1 
    WHERE itm.deleted != 1 
   HAVING featured > 0;

解释 SQL 解释

这个查询是我努力减少和改进的结果。我已经从需要 180 秒的原始查询到现在需要大约 20 秒的查询,但仍然不够。

谁能为此查询提供性能改进?

我们正在搜索几百万行数据,所以每一点都会有所帮助。

4

2 回答 2

0

子查询中使用的许多字段不是索引的一部分。如果您打算在此类查询中大量使用它,请尝试创建与这些子查询对应的复合索引。您可能不需要所有这些(这取决于您的表有多大以及数据在那里的分布方式)。

此外,您没有指定 tracking_action 表的外观,但我看到您在那里使用文本字段(如果它是文本字段),如果该字段没有被索引,那么它也会减慢查询速度。

我会尝试创建(一些)以下复合索引:

item_category_xref - (item_id, featured) and (item_id, category_id)
tracking_action - (context_guid, context, action)
item_image - (item_id, sequence)
于 2013-11-14T22:10:09.700 回答
0

我会将您的子查询移动到整体查询中更合适的位置。任何你想要的结果,将他们的子查询加入到项目表中。您要比较的东西应该在 where 字段中。此外,您在查询中比较的任何内容都需要编制索引。显而易见的是delete字段,但我会包括跟踪操作context_guidaction字段,可能作为复合索引。另外,我会确保action在您的查询中正确引用,因为它是保留字。

这将为您提供额外的好处,即能够分解每个子查询并单独测试它们以寻找性能命中。这将允许您隔离有问题的表或索引。

这是我粗略的凝视点,语法可能并不完美。

SELECT
    itm.id "item.id",
    ws.id "workspace.id", 
    co.id "company.id",
    tav.ta_view_count,
    tas.ta_search_count
FROM item itm
    LEFT JOIN (SELECT ta1.context_guid, COUNT(*) as ta_view_count FROM tracking_action ta1 GROUP BY ta1.context_guid HAVING ta1.context_guid = 'ITEM' AND ta1.`action` = 'VIEW') tav ON tav.context_guid = itm.guid
    LEFT JOIN (SELECT ta2.context_guid, COUNT(*) as ta_search_count FROM tracking_action ta2 GROUP BY ta2.context_guid HAVING ta2.context_guid = 'ITEM' AND ta2.`action` = 'SEARCH_RESULT') tas ON tas.context_guid = itm.guid
WHERE   itm.deleted != 1 AND
        itm.id IN (SELECT icx.item_id, COUNT(*) featured FROM item_category_xref icx GROUP BY icx.item_id HAVING featured > 0) AND
        itm.id IN (SELECT company.id FROM company WHERE company.deleted != 1) AND
        itm.id IN (SELECT workspace.id FROM workspace WHERE workspace.deleted != 1) AND
        itm.id IN (SELECT item_image.id FROM item_image WHERE item_image.sequence != 1);
于 2013-11-14T22:28:53.727 回答