Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. index order

index order

Scheduled Pinned Locked Moved Database
databasesql-serveroraclesysadmintools
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Shajeel
    wrote on last edited by
    #1

    hi i have created the index in order col1, col2, now if in my application or on query analyzer i write the script select * from table1 where col2 = 'value 2' and col1 = 'value 1' will the index be used or not as order is changed in my srcipt. i want to know about both sql server and oracle.

    Regards Shajeel

    P 1 Reply Last reply
    0
    • S Shajeel

      hi i have created the index in order col1, col2, now if in my application or on query analyzer i write the script select * from table1 where col2 = 'value 2' and col1 = 'value 1' will the index be used or not as order is changed in my srcipt. i want to know about both sql server and oracle.

      Regards Shajeel

      P Offline
      P Offline
      Pete OHanlon
      wrote on last edited by
      #2

      This sounds suspiciously like homework to me, but I'll answer it anyway. If you have created two indexes, then they will most likely be used. Note that the usage depends entirely on the query execution plan that the DB server determines is the best usage. Suppose that there are only 2 possible values in col2, then the engine may determine that it needs to do a sequential scan because there is not enough differentiation in the data to allow it to perform an indexed scan. Similarly, if there are only a few rows in the table then it may perform a sequential scan because the engine has determined that the row count is too few to allow it to index search efficiently. As always, the best way to find out the answer is to try it out yourself.

      Deja View - the feeling that you've seen this post before.

      S 1 Reply Last reply
      0
      • P Pete OHanlon

        This sounds suspiciously like homework to me, but I'll answer it anyway. If you have created two indexes, then they will most likely be used. Note that the usage depends entirely on the query execution plan that the DB server determines is the best usage. Suppose that there are only 2 possible values in col2, then the engine may determine that it needs to do a sequential scan because there is not enough differentiation in the data to allow it to perform an indexed scan. Similarly, if there are only a few rows in the table then it may perform a sequential scan because the engine has determined that the row count is too few to allow it to index search efficiently. As always, the best way to find out the answer is to try it out yourself.

        Deja View - the feeling that you've seen this post before.

        S Offline
        S Offline
        Shajeel
        wrote on last edited by
        #3

        i think i explained it wrong i have not created two indexes, i have created only one index with two columns like CREATE UNIQUE INDEX Index1 ON table1 (col1, col2) so in order col1 is first and col2 is second. in where i used where col2 = 'val 2' and col1 = 'val 1' i.e. filter in wrong order.

        Regards Shajeel

        A 1 Reply Last reply
        0
        • S Shajeel

          i think i explained it wrong i have not created two indexes, i have created only one index with two columns like CREATE UNIQUE INDEX Index1 ON table1 (col1, col2) so in order col1 is first and col2 is second. in where i used where col2 = 'val 2' and col1 = 'val 1' i.e. filter in wrong order.

          Regards Shajeel

          A Offline
          A Offline
          andyharman
          wrote on last edited by
          #4

          Hi Shajeel It shouldn't matter which way round you have the where-clause, because SQL-Server (and Oracle) are able to recognise that the index is potentially applicable (because both RDBMS products use a cost-based optimiser). The main criteria as-to whether the index will be used will be how selective the index is (i.e. how many records does SQL-Server think will be returned from the query based on its statistics). Regards Andy

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups