Hi,
I've the following db:
Person(rid, name .....) containing some 10000 persons;
Pcategory(rid, cat) containing some 50000 records with categories for persons.
Now I want to select persons which have two specific categories 240 and 781. I use the following query:
select * from Person p
where p.rid in
(select rid from Pcategory where cat in (240,781)
group by rid having count(*) = 2)
This query takes a very long time (13 minutes). How can I optimize this query?
When I create a temporary table Tmprid(rid) and fill this table first:
insert into Tmprid
select rid from Pcategory where cat in (240,781)
group by rid having count(*) = 2
This takes no time (12 persons). Next:
select * from Person p
where p.rid in
(select rid from Tmprid)
This takes 2 seconds. Why is the first query so slow? I would think Interbase proceeds internally the same way as I did.
Anyone an idea?
Henk
I've the following db:
Person(rid, name .....) containing some 10000 persons;
Pcategory(rid, cat) containing some 50000 records with categories for persons.
Now I want to select persons which have two specific categories 240 and 781. I use the following query:
select * from Person p
where p.rid in
(select rid from Pcategory where cat in (240,781)
group by rid having count(*) = 2)
This query takes a very long time (13 minutes). How can I optimize this query?
When I create a temporary table Tmprid(rid) and fill this table first:
insert into Tmprid
select rid from Pcategory where cat in (240,781)
group by rid having count(*) = 2
This takes no time (12 persons). Next:
select * from Person p
where p.rid in
(select rid from Tmprid)
This takes 2 seconds. Why is the first query so slow? I would think Interbase proceeds internally the same way as I did.
Anyone an idea?
Henk