我有一个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;
EXPLAIN
此查询是我努力减少和改进的结果。我已经从原来的查询开始了180秒,现在需要大约20秒,但仍然不够。
任何人都可以为此查询提供性能改进吗?
我们正在搜索几百万行数据,所以每一点都会有所帮助。
答案 0 :(得分: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)
答案 1 :(得分:0)
我会将您的子查询移动到整个查询中更合适的位置。您在结果中想要的任何内容,将它们的子查询加入到项目表中。你想要比较的东西应该在where字段中。此外,您在查询中要比较的任何内容都需要编制索引。显而易见的是delete
字段,但我会将跟踪操作context_guid
和action
字段包括在内,可能作为复合索引。另外,我会确保在您的查询中正确引用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);