Results 1 to 9 of 9

Thread: How to optimize search on multi cols?

  1. #1

    Thread Starter
    Member
    Join Date
    Jun 2000
    Location
    Hong Kong
    Posts
    62

    Question

    How should I create index on the SQL server so that the following query is optimized?

    select * from table1 where col1='aaa' and col2='bbb' and col3='ccc'

    Thank you very much!
    Please Visit My WebCam!!
    http://www.hmcheung.com

  2. #2
    Fanatic Member simonm's Avatar
    Join Date
    Sep 2000
    Location
    Devon, England
    Posts
    796

    Lightbulb Indexes

    Indexing the three fields might be an option (three seperate indexes that is).

    It's usually worth indexing a column if the column's values are not mostly the same thing or are not all totally different.

    For example, you wouldn't bother indexing a BIT field as there are only two possible values (OK, three including NULL).

  3. #3
    New Member
    Join Date
    Feb 2001
    Posts
    15

    SQL Server Indexing

    Swings and roundabouts!

    You must way up the following: -

    1. How much data insertion is going to happen
    2. How big the table is going to get
    3. The other possible search criteria

    I tend to max out on the indexing, but it can impare on data submission as indexes must be maintained when delete, update and insert are called.

    The size of the table would impact on how many indexes you have. Some people create index tables that are larger that the table they are searching.

    I never try and cover all possibilities. Cover the main variations (in you case create a three field index). You can always trace for slow queries and use that trace file to tweak even further.

    Oh - tell the bloke who said that a bit has three values - no a bit has two - that is why it is called a bit !
    brock loves to code

  4. #4
    Fanatic Member simonm's Avatar
    Join Date
    Sep 2000
    Location
    Devon, England
    Posts
    796

    Angry What the bloke has to say

    OK, I know NULL isn't technically a value but it's still distinguishable from 1 or 0 and is therefore a third allowable state for a BIT field.

    Only create a combined index on all three fields if you will always search on those fields together.

  5. #5
    New Member
    Join Date
    Feb 2001
    Posts
    15

    I beg to Differ

    Simon me old buddy

    A pure bit field can only ever hold two values. If you allow bits to hold NULL's they are not longer bits! You could call them tri's (or twits), but not bits.

    Come on, don't go bending 70 year old laws now.
    brock loves to code

  6. #6
    Fanatic Member simonm's Avatar
    Join Date
    Sep 2000
    Location
    Devon, England
    Posts
    796

    Talking Endless argument

    OK, Maybe a "pure" bit field should not allow NULL values but surely you agree with the point I was making in that you wouldn't bother indexing a BIT field would you?

  7. #7
    Hyperactive Member
    Join Date
    Oct 2000
    Posts
    303

    an opinion

    you should create an index by combining the
    three fields from the most unique (left) to the least
    unique (right) as follows:

    CREATE INDEX your_index_name on
    tablename(col1,col2,col3);

    Col1 should be the most unique (ie, 100% unique)
    Col2 should be 80%
    col3 should be >= 60%. If it is <= 50%, skip it

  8. #8
    Fanatic Member simonm's Avatar
    Join Date
    Sep 2000
    Location
    Devon, England
    Posts
    796
    jxiong,

    What's the point of indexing a field that is 100% unique?

    Surely the index would take as long to search as the table itself?

  9. #9
    Hyperactive Member
    Join Date
    Oct 2000
    Posts
    303
    Well, unique and sorted are not the same!

    Let say you have 26 unique chars, ie, A-Z, would you
    agree that it would take you longer to find the
    char W if it is not sorted?

    Your logical is valid for unique, but does not make it
    faster as far as searching for the specific key/value.

Posting Permissions

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



Click Here to Expand Forum to Full Width