【发布时间】:2018-06-16 13:48:18
【问题描述】:
这是我的查询:
select coalesce(qa2.subject, qa.subject) as question_subject,
qa.body,
(select count(*)
from viewed_items vi
where coalesce(qa.related, qa.id) = vi.question_id
) as total_question_viewed
from questions_and_answers qa
left join questions_and_answers qa2 on qa.related = qa.id
where body like ':entry';
如您所知,MySQL 优化器永远无法在 coalesce(qa.related, qa.id) = vi.question_id 上使用索引。那么知道如何才能更优化地编写此查询吗?
【问题讨论】:
-
试一试
where vi.question_id in(qa.related, qa.id),确保检查两个查询的解释计划
标签: mysql sql query-optimization