r/mysql • u/Affectionate-Gift652 • Dec 05 '24
question Optimising select where exists...
I have been bashing my head against this for a few days now, and can't figure out a good solution, so I thought I would share the misery...
I have two pretty big tables, let's call them bigtable and hugetable. They have a common compound key on columns keyA and keyB (there is a compound index on this column pair on both tables).
What I basically want to do is:
select count(*) from bigtable where exists (select 1 from hugetable where hugetable.keyA=bigtable.keyA and hugetable.keyB=bigtable.keyB);Which should be easy enough, but no matter how hard I try, I can not get it to use an index for the match, and the resulting query takes about 3 hours.
select count(*) from bigtable inner join hugetable on hugetable.keyA=bigtable.keyA and hugetable.keyB=bigtable.keyB;Does use an index, and only takes a few minutes, but rows are duplicated, so counts are wrong.
alter table bigtable add myrowid bigint not null primary key auto_increment;
(insert rows here)
select count(distinct bigtable.myrowid) from bigtable inner join hugetable on hugetable.keyA=bigtable.keyA and hugetable.keyB=bigtable.keyB;Is also really quick if there are only a few matches, but gets ludicrously slow when there are a few million.
Now the MySQL query engine obviously has all the information available, and this should optimise down to a simple index count, IF I can figure out a syntax that can get MySQL to do that for me...
Any hints/tips would be appreciated!
1
u/Wiikend Dec 05 '24
Wild guess, but does wrapping the query help? i.e. select count(*) from (select * from bigtable inner join hugetable ... )
1
u/ComicOzzy Dec 05 '24
What indexes have you tried? I'd recommend one on both keyA and keyB for both tables.
1
u/ssnoyes Dec 05 '24
What about the fast query, but using
select count(distinct bigtable.keyA, bigtable.keyB) from...?1
u/r3pr0b8 Dec 05 '24
aha!!
so how about this --
2
u/GreenWoodDragon Dec 05 '24
Have you examined the queries using
EXPLAIN?That should tell you more about how the queries are being dealt with by the optimiser.
2
u/TimIgoe Dec 05 '24
SELECT COUNT(DISTINCT {Field}) ....
1
u/HadiMhPy Dec 07 '24
I think using an inner join would help