|P is for Practical|
Re: (OT) Improving a SQL JOIN statementby jorg (Friar)
|on Apr 10, 2001 at 18:02 UTC||Need Help??|
For future diagnostics : it would help a lot if people would includ an 'Explain Plan' output whenever there are database performance issues on certain queries. For Mysql just do a 'EXPLAIN select foobar from tableBar a, tableFoo b where a.foo=b.bar' and it will tell you how the mysql optimizer handles this query.
From what you've told us it looks like mysql is ignoring the indexes :
- make sure you choose your 'driving' table correctly, if you want to group all the ratings per item then your itemtable will be driving the query. (and thus put 'select blah from itemtable, ratingtable where...' instead of 'select blah from ratingtable, itemtable where...'
- looking at the mysql docs: 'MySQL can't (yet) use indexes on columns efficiently if they are declared differently' ie make sure you've declared your join columns in the same way, don't declare one as a char(10) and the other as a char(15)
- you can tell the optimizer to use an index by using hints on which index to use. Mysql docs have all the info on that as well.
"Do or do not, there is no try" -- Yoda