MySQL Forums
Forum List  »  Newbie

Re: optimization
Posted by: Roland Bouman
Date: August 04, 2005 08:09AM

Well, to be honest, I can't give you just one answer. In fact I may be totally wrong on my assertion that the join will probably perform worse than your solution. First, I'm not really into performance, so I can not give you very good advice on that. Performance is a mighty subject that involves not just the particular SQL yoju write, but also the version of the optimizer (e.g. the MySQL version), wheter you have indexes and on what columns, how many rows you are fetching (because the internal optimizer's strategy may vary according to that), the engine that's used to store data, and so many more....So, Sorry, I can't help you there.
Second, performance is not everything. Im find myself a lot of situations where performance is good enough (everyone will sue me for this one!); in these cases i tend to write what i think reads best, and ill probably end up with the subquery there. But I don't think there's any need to be rigourous about that. I just think that that solution is a quite natural translation of the question, "give me only those records where that date is equal to the largest value that cound be found in that column", whereas your solution would back translate to: "give me all those records, sort them largest values first, and then give me the one record on top of the stack". The 'join' solution would be something like: "give me all records, and for each record, give me all combinations with all those records AGAIN, then, for each record I got in the first step, please calculate the maximum of the date column found in all the records i got in the second step, and when you're done with that, please single out all those records from the first step that happen to have a date column value equal to that maximum you just calculated". The user variable solution would be like the subquery translation, but instead of "...is equal to the largest value that cound be found in that column..." you should read "...is equal to that funny variable that i hope was just initialized to what i hope is still the largest value found in my column...".
Think about that for a while:
1) user 1 initializes the variable to what is, at that moment, the largest value in the date column
2) user 2 deletes all records where the date column is equal to that value, in a separate session
3) user 1 tries to retrieve the records where the date column is equal to the value he/she just put into that variable, and ...gets an empty set...Magic!

If performance is an issue, I really advise you to try for yourself what performs best. Try It lots and lots of times, under lots and lots of different circumstances (indexes, table engines, number of rows, all that). Also, check out the relevant parts of the manual:

http://dev.mysql.com/doc/mysql/en/mysql-optimization.html

and the performance forum, and post there if the performance of your statement is an issue.
.

Options: ReplyQuote


Subject
Written By
Posted
August 03, 2005 03:42PM
August 03, 2005 04:01PM
August 03, 2005 04:05PM
August 04, 2005 07:30AM
Re: optimization
August 04, 2005 08:09AM


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.