mysql - Huge performance difference in almost identical queries -
i have 2 queries count number of rows in relatively large table. allow filtering particular name, i've left-joined smaller table it.
my first attempt yielded query #1 (see below), turned out very, slow (6-7 seconds). after playing around query bit, got query #2 (see below) seems same thing faster (< 0.05 seconds).
query #1 ( > 6 seconds):
select count(*) bigtable left join ( select distinct localkey, name smalltable ) smalltable on bigtable.foreignkey = smalltable.localkey; query #2 ( < 0.05 seconds):
select count(*) bigtable left join ( select distinct localkey, name smalltable ) smalltable on bigtable.foreignkey = smalltable.localkey smalltable.localkey "%"; both queries return exact same number (50300). bigtable has 50300 rows, smalltable has 680 rows. remove many factors possible, i've ensured records in bigtable have (unique) matching row in smalltable. smalltable.localkey indexed, primary key bigtable. both tables have been optimised using optimize table [table];. rows smalltable matched smalltable.localkey "%".
i've tried searching phenomenon exhaustively, however, i've found no explanations whatsoever. have explanation why first query slower, and, if possible, better solution query #2?
edit:
immediately after running each query, show $warnings show optimized query execution order. posting show $warnings query #1 , query #2 should understand why query #2 quicker.
Comments
Post a Comment