Jump to content

MySQL ORDER BY optimization?


fenway

Recommended Posts

[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]In some cases, MySQL cannot use indexes to resolve the ORDER BY, although it still uses indexes to find the rows that match the WHERE clause. These cases include the following: ... You are joining many tables, and the columns in the ORDER BY are not all from the first non-constant table that is used to retrieve rows. (This is the first table in the EXPLAIN output that does not have a const join type.) [/quote]
So I know all about adding indexes for use with ORDER BY, multi-column keys, preventing table scans, preventing table reads at all, etc.

But I haven't been able to work around this particular issue -- in fact, I didn't even know it wasn't possible until I read the refman. This is, of course, independent of version.

Basically, if you use JOINs of any type -- like a 4-table LEFT JOIN -- you will be unable to sort on any keys that do not appear in the "first" non-joined table. I understand in principle why this might be difficult, because you can't request an order for the rows to be joined in, but still, it's troublesome.

I was thinking that perhaps I could use subqueries to work around this -- perhaps if there was just a single ORDER BY column, it could rewrite this, but it gets cumbersome with 3 such columns. And besides, I prefer JOINs anyway, if possible.

Any ideas? Anyone else run into this problem?
Link to comment
Share on other sites

  • 3 weeks later...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.