Force Table Selection Order in JOIN Statement?
Hi,
first my problem:
i have this query that is sometimes very slow, so one of those times, i took the query and put an explain in front of it, i saw that the first table that the other query components depended upon was not using a key, and that on a table with 25+ million records... so no wonder it is extremely slow..
so i did a show index for the first table and the weird thing is that everything looked alright, the key that was supposed to be used was a combination key, but the value that was used in the query was indeed the first sequence component of that key...
anyway.. i went to the development server and did an explain on exactly the same query, there it was using the key properly and it searched only 25 instead of 25 million records... i looked at the table and index structure, i found that the the cardinality was the only thing that was different on the show index statements..
not sure how that came to be about... but then the really weird thing happened,
i went back to the main server and re-executed the explain query.. and suddenly it was working perfectly! i mean properly using the key... i re-executed the show index and suddenly the cardinality was the same as on the dev server as well!!
i mean this is weird becuase i did not do a 'analyze table', i did do a few inserts and a 'flush tables'.. but does that modify the index cardinality?
i guess my first question is:
what actions change the index cardinality?
and second:
how can i force a certain table select order in a multiple join statement?
using subqueries is a very bad idea since mysql seeems to have a bug where it will hang indefinitly (sometimes) on the 'copy to tmp table' with that...
for the cardinality i can just use 'force index', but still, the problem with multiple joins, how can i gurantee that a certain order of table selection is followed? for example, table B has 100 records, table A has 1,000,000 and they are linked, i want to select only those records where B.someKey = 'X' and have it linked with A of course... the problem is that apperently depending upon the cardinality mysql changes order in which the tables are joined.. so is there a way to force this as well?
server info:
i am using innodb for all tables and the server version is 4.1.15-nt-max-log
thanks for any help with this..
Edited 2 time(s). Last edit at 08/21/2006 06:06AM by Frank Osterberg.