I have a super-simple query in a star schema. One fact, two dimensions. I have verified that I am doing the joins properly. But when I execute the query plan, I get a merge cartesian join right before the steps to add the dimensions.
explain plan for
select * from fact f
inner join dim1 d1
on d1.id = f.d1_id
inner join dim2 d2
on d2.id = f.d2_id
where d1.code = 'A' and d2.code = 'B';
If I change to search by the dimension ID instead of the code, my plan is fine - no cartesian.
explain plan for
select * from fact f
inner join dim1 d1
on d1.id = f.d1_id
inner join dim2 d2
on d2.id = f.d2_id
where d1.id= '1' and d2.id = '2';
Any ideas what could cause the cartesian to happen?
EDIT: I just created the tables and indexes today. I will verify that I did "compute statistics" on them to be sure all is up-to-date.
more information on the tables now that I have edited them and got rid of the cartesian:
Fact table:
bitmap index on dim1.id
bitmap index on dim2.id
(and lots more bitmap indexes)
Dim1
unique index on id
bitmap index on code --this is new, but it didn't seem to change the query plan any.
Dim2
unique index on id
unique index on code --when I added this, the cartesian went away.
My fact table has 50 million records, dim1 has 44 records, and dim2 has 6 records. So I didn't originally have indexes on such short tables. But adding the unique index to dim2 is what got rid of the cartesian join and dropped the query plan time estimate from 5 minutes to a few seconds.