我有下面的查询,我试图改写它来改善性能,我可以用什么方法来重写它。如何重写MySQL查询
select
notes.id, notes.name, notes.parent_type, notes.contact_id from notes
JOIN
(
SELECT contact_id as id from accounts_contacts where account_id = 'acct1876' and deleted = '0' union
SELECT quote_id as id from quotes_accounts where account_id = 'acct1876' and deleted = '0' union
SELECT opportunity_id as id from accounts_opportunities where account_id = 'acct1876' and deleted = '0' union
SELECT leads.id as id from leads where account_id = 'acct1876' and deleted = '0' union
SELECT project_id as id from projects_accounts where account_id = 'acct1876' and deleted = '0' union
select 'acct1876' as id
) A
ON A.id = notes.parent_id and deleted = '0' OR contact_id in
(SELECT contact_id from accounts_contacts where account_id = 'acct1876' and deleted = '0') and deleted = '0'
group by notes.id;
你可以学习JOIN,但是你真的不知道你在这里做什么!?!?! – Strawberry
如果你能解释你的需求,而不是希望人们能正确地从你的查询中反向工程,那将会有很大帮助。另外,您滥用“GROUP BY”;在查询中没有聚合函数(如'SUM()'或'GROUP_CONCAT()')。 –
基本上查询很慢。索引是确定的,但总体表现并不理想。我正在尝试重新构造查询以生成相同的输出。 –