我有这样的查询:
SELECT * FROM Backlog b
WHERE b.projectid=10
AND EXISTS (SELECT 1 FROM Requirement req
WHERE req.projectid=10
AND req.status='Approved'
AND req.deleted!=TRUE
AND req.backlog=b.id
AND b.majorrelease IS NULL
AND b.iteration IS NULL )
OR EXISTS(SELECT 1 FROM Defect def
WHERE def.projectid=10
AND def.status!='Open'
AND def.deleted!=TRUE
AND def.backlog=b.id
AND b.majorrelease IS NULL
AND b.iteration IS NULL )
ORDER BY b.rank ASC,b.priority ASC;
及其解释计划是 -
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY b ALL projectid 4258 Using where; Using filesort
3 DEPENDENT SUBQUERY def ref defect_backlog_fk,defect_bl_fk,projectid defect_backlog_fk 8 eyeagile.b.id 1 Using where
2 DEPENDENT SUBQUERY req ref req_backlog_FK,projectid req_backlog_FK 9 eyeagile.b.id 1 Using where
使用'OR'时不使用projectid上的索引。我已经尝试过强制索引,但也没有使用索引。有什么不对?请让我知道,谢谢。
答案 0 :(得分:1)
试试这个:
SELECT *
FROM backlog b
LEFT JOIN requirement req
ON req.backlog = b.id
AND req.projectid = 10
AND req.status = 'Approved'
AND req.deleted != true
AND b.majorrelease IS NULL
AND b.iteration IS NULL
LEFT JOIN defect def
ON def.backlog = b.id
AND def.projectid = 10
AND def.status != 'Open'
AND def.deleted != true
AND b.majorrelease IS NULL
AND b.iteration IS NULL
WHERE b.projectid = 10
AND ( req.reqid IS NOT NULL
OR def.defid IS NOT NULL )
ORDER BY b.rank ASC,
b.priority ASC;