Solved

Tune SQL – WHERE by 2 fileds in one Table – 2 indexes or 1 index with 2 fields?

Posted on 2014-10-21
3
124 Views
Last Modified: 2014-10-21
Dear experts,

I have the task to tune one SQL 2008R2 DB. I have follow t-SQL

Select Table1.F1,Table1.F2, Table1.F3, Table1.F4,OtherTable.F10,OtherTable.F11 .... ,OtherTable.F12 FROM ….  JOIN ...  WHERE Table1.F1 <getdate() AND Table1.F2=2 and …

My question is focus on Table1 where I search by 2 criteria. I wonder I should create 2 separated indexes on every filed, or one index with 2 fields. I know the best way it to measure this via Execution plan, but I think this is a common situation with common recommended solution. I’m focused on speed; My Table1 will have less than 10 000 records, will be relative rarely write, but will be read from this SP all the time.

And I have the same question, but for other table which will have millions of records and will be heavy read and write as well. Then what is the standard solution?
0
Comment
Question by:dvplayltd
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 40393843
1 index with the 2 fields would be the general rule, and the field with the best cardinality first.
means if you have the 2 fields like gender + firstname (just an example), gender has 2-3 distinct values, while firstname has lots of different values, so you would create the filter on firstname + gender and not gender + firstname

note that such a index (firstname + gender) will also be able to help queries that will only search by firstname
note that such a index (firstname + gender) will not be able to help queries that will only search by gender
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 40393847
in your concrete scenario, there come several possible solution paths:
* clustered index on F2 + F1
* partition on F2 + index on F1
* partition on F1 + index on F2
0
 

Author Closing Comment

by:dvplayltd
ID: 40393866
10x, i got what you mean. I'll put first field of the index Date /may have many different values/, then on second position Type which have 4 possible values only.
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
point in time restore in SQL server 26 43
SQL Select Query help 1 37
user database (login sql or login windows) 3 25
SQL Server Compression Decision 5 43
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question