SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Evangelist
    Join Date
    May 2006
    Posts
    438
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

    Using index on ORDER BY

    The documentation on the MySQL seems lacking in this area.

    If you had a table where you typically select everything a “page” at a time using LIMIT x, y and ORDERing BY a column, is it worth adding an index to the ORDER BY column?

    Let's say you had a simple table with 100k records:

    id, name, html

    And you're doing queries like:

    SELECT * FROM table ORDER BY name LIMIT 50000, 50;

    Can you optimise this or will it always use filesort?

    Thanks.

  2. #2
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,323
    Mentioned
    63 Post(s)
    Tagged
    3 Thread(s)
    yeah, that will be optimized by an index on name

    note: i have yet to see a real world application where there is a user patient enough to hit the "next" link one thousand times (to get to the scenario you used, LIMIT 50000,50)
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  3. #3
    SitePoint Evangelist
    Join Date
    May 2006
    Posts
    438
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by r937 View Post
    yeah, that will be optimized by an index on name

    note: i have yet to see a real world application where there is a user patient enough to hit the "next" link one thousand times (to get to the scenario you used, LIMIT 50000,50)
    LOL, I know. I guess I was just testing the theory. Thanks for the reply.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •