I have a person and a task table. The task table is much bigger and contains millions of rows.
With the following (fast) SQL statement I get a list of tasks for a given person:
select p.name, t.name
from person p
join task t on t.person_id = p.id
where p.id = 1234
Unfortunately, I need to work with the following (equivalent but slow) SQL statement:
select p.name, t.name
from person p
join task t on 1 = 1
where t.id in (
select t.id
from task t
where t.person_id = p.id )
and p.id = 1234
Why does the second statement take about 20 seconds, while the first only runs for less than a second. Can I use an index or something to speed up the second statement as well? I cannot change the SQL statement.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…