Help. Mysql Query optimisation not very clever?
| From: | Ross Honniball | Date: | Sat, 17 Jul 2004 01:31:40 +0000 |
| Subject: | Help. Mysql Query optimisation not very clever? | ||
| Groups: | php.db | ||
| Request: | Send a blank email to php-db+get-35329@lists.php.net to get a copy of this message | ||
Attention all Sql gurus,
Is there a way to FORCE mysql resolve specific conditions within a query before wasting it's time trying to resolve other conditions?
I THINK mssql is behaving foolishly in the way it goes about resolving the following situation.
Tables :
BN_SupMast : Reference table joined by SupCode field
\ /
|
|
BN_StkMast : Master table with 20,000 records
|
|
/ \
BN_StkLevl : Exactly 2 records per BN_StkMast table; Joined by StkCode
When I execute a query explicitly specifying BN_StkMast key, it works perfectly as expected.
EXAMPLE OF QUERY THAT WORKS FINE :
$Q = "select * from BN_StkLevl as sl, BN_StkMast as sm, BN_SupMast as sup where"
only dif.---> ." sm.StkCode='0005'"
." and sl.StkCode=sm.StkCode"
." and sm.StkSupplierUsual=sup.SupCode"
If, instead, I execute a query with a more vague condition on the BN_StkMast table, the entire system hangs. I'm guessing this is because mySql is first resolving the condition that links all the BN_SupMast records against all the BN_StkMast records? I can't think why else it would hang.
EXAMPLE OF QUERY THAT HANGS:
$Q = "select * from BN_StkLevl as sl, BN_StkMast as sm, BN_SupMast as sup where"
only dif.---> ." sm.StkDescription like 'age%'"
." and sl.StkCode=sm.StkCode"
." and sm.StkSupplierUsual=sup.SupCode"
In case you are thinking that this query might just return truck-loads of results, this is not the case. If I REMOVE the BN_StkLevl table from the above queries, then both work fine (also, if I remove BN_SupMast they both work fine). It must be MySql somehow doing something odd.
Sorry for being so long-winded, but can any one help?
Thanks ... Ross
. Ross Honniball JCU Bookshop Cairns Supervisor. James Cook Uni, McGreggor Rd, Smithfield, Qld. 4878, Australia . Ph:07.4042.1157 Fx:07.4042.1158 Em:ross@bookshop.jcu.edu.au . There are no problems. Only solutions.