SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Hybrid View

  1. #1
    SitePoint Evangelist
    Join Date
    Apr 2003
    Location
    Chicago, USA
    Posts
    417
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Which Is Faster?

    A query like:

    SELECT * FROM table

    Or:

    SELECT * FROM table WHERE id = 1 OR id = 2 OR id = 3 OR id = 4 OR id = 5

    Assuming that the table was pretty huge?

    Thanks.

  2. #2
    Database Jedi MattR's Avatar
    Join Date
    Jan 2001
    Location
    buried in the database shell (Washington, DC)
    Posts
    1,107
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If there is an index on ID, and you have more than just those five IDs in there...

    It depends.

    We need more information than just that, including the table schema, indexes (if any), what you are trying to do, etc.

  3. #3
    SitePoint Addict Ramiro S's Avatar
    Join Date
    May 2003
    Posts
    321
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What a question... query 2 of course. Try this instead:

    SELECT * FROM table WHERE id IN (1,2,3,4,5)

    You are comparing selecting everything and selecting only 5.
    Quasar - Web Development - Free Avatars

  4. #4
    Database Jedi MattR's Avatar
    Join Date
    Jan 2001
    Location
    buried in the database shell (Washington, DC)
    Posts
    1,107
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ramiro: If there is no index on ID, or if {1,2,3,4,5} is a superset of the actual data in the table, it should take approximately the same time.

    There is more to this issue than simply the existance of predicates in the where clause.

  5. #5
    SitePoint Addict Ramiro S's Avatar
    Join Date
    May 2003
    Posts
    321
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I agree Im talking about a simple MySQL table with an index on the Id column.
    Quasar - Web Development - Free Avatars


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
  •