MySQL Forums
Forum List  »  Optimizer & Parser

Re: Really need help with optimization
Posted by: Toa Sty
Date: August 24, 2006 04:53AM

Hiya,

Just some general comments. Hope they're useful. Without having the tables with data in front of me it's hard to be sure what the best way to speed up these queries is.



In each of your queries, are you aware that your GROUP BY may give you unexpected/unpredictable values in the columns you're not grouping on, in the case where more than one row matches the same values in the fields you are grouping on?

So for example, for a particular set of values for
(sProductTitle, sShopLink, dblCurrentPrice)
you may see any value for say sSale, sStockStatus, etc if there's more than one row that matches the same values for (sProductTitle, sShopLink, dblCurrentPrice) but different values for sSale, sStockStatus, etc

See: http://mysql.com/doc/refman/5.0/en/group-by-hidden-fields.html




As you're not calling any aggregation funcitons (count/sum/max/etc), if it's possible to remove the need for a GROUP BY by being more specific with your WHERE (or changing your schema) you might gain some performance.


For the second query, you're not able to use an index to do the LIKE statement as you have a leading %, so you're having to scan the table. Currently the 2nd and 3rd queries exectute pretty much the same way. For the 2nd query you could try adding a fulltext index on tblPriceData.sProductTitle I guess. It should help.


For both the 2nd and 3rd queries, if you can remove the GROUP BY statement you might get some performance gains. In many cases MySQL will stop processing the query once you have enough rows returned (in your case 200) and with no GROUP BY/ORDER BY this is often very easy for MySQL to determine. With a GROUP BY or ORDER BY on cols that aren't fully indexed, I'm think MySQL has to get *all* the matching rows, group them, sort them then return just the top 200.


So one thing you can try is to add a single multi-column index on the fields your grouping/ordering by, and make sure you're grouping and ordering by the same fields in the same order. (GROUP BY does an internal sort anyway, so hopefully if the ORDER BY is the same as the GROUP BY MySQL will be smart enough not to try to re-sort)

(so try:

GROUP BY tblPriceData.sProductTitle, tblPriceData.dblCurrentPrice, tblPriceData.sShopLink
ORDER BY tblPriceData.sProductTitle, tblPriceData.dblCurrentPrice, tblPriceData.sShopLink

with an index on:
(tblPriceData.sProductTitle, tblPriceData.dblCurrentPrice, tblPriceData.sShopLink)

and see what happens. If it does help and you want to keep this index, you'll end up with a duplicate index on sProductTitle so remove the single col one)


So as I say these are just some general thoughts to get you started. Maybe they'll help or maybe not - I can't test them :)

Let me know how it goes - if you can include typical query execution times as well as explian plans that'd be useful.

Toasty

Options: ReplyQuote


Subject
Views
Written By
Posted
2937
August 22, 2006 01:43PM
1792
August 23, 2006 12:50AM
Re: Really need help with optimization
1805
August 24, 2006 04:53AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.