我有一个mysql sql选择,这种方式需要很长时间才能返回数据。提高mysql查询的性能
表
╔════════════════╗ ╔════════════════╗
║ 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秒但仍然不够的原始查询。
任何人都可以提供此查询的性能改进?
我们正在搜索数百万行的数据,所以每一点点都会有所帮助。
如果你注释掉select语句中的三个子查询,它运行得有多快? –
在此处发布解释说明的输出。 – Namphibian
另外,item_category_xref中是否有精选字段? –