Solved

Performance consideration of index ms sql

Posted on 2009-07-01
6
151 Views
Last Modified: 2012-05-07
I have a query which is executed against one of our databases 100+ per minute

The queries bombarding this table look like this:

SELECT  mt.myPKCol, mt.myDataCol1, mt.myDataCol2, mt.myDataCol3, ot.myDataCol1
  FROM myTable mt
      INNER JOIN myOtherTable ot ON (mt.myFK1Col=ot.myPKCol)
  WHERE mt.myFK1Col=x AND mt.myFK2Col=y

myTable looks like this:

CREATE TABLE [myTable] (
         [myPKCol] [int] IDENTITY(1,1) NOT NULL,
      [myFK1Col] [int] NOT NULL,
      [myFK2Col] [int] NOT NULL,
      [myDataCol1] [nvarchar](255) NULL,
      [myDataCol2] [datetime] NULL,
      [myDataCol3] [int] NULL,
        constraint [PK_myPKCol] primary key ([myPKCol])
)

There are 25 or so possible values for FKCol1 and hundreds of thousands of values for FKCol2.

There are currently over 4 million rows in mytable and Profiler shows each instance of this problem query with an avg CPU time of over 300ms and there are hundreds per minute. CPU runs 90% + during peak hours.

I cannot affect the query because it is coming from third party software

Here is my question:

I would like to add the following index to improve performance of above SELECT query:

CREATE INDEX [idx_myFKCol1_myFKCol2] on [myTable]([myFKCol1],[myFKCol2)

In a test environment I have added the index and the select queries perform MUCH better

What is the impact on inserts, updates, deletes?

This table is constantly inserted to, updated and ocasionally but less frequently deleted from.
20-30 very active  concurrent users of third application

Although once a row is inserted its myFK1Col and myFK2Col columns are NEVER updated, only myDataCol1, myDataCol2, and myDataCol3 are updated

Will I solve the problem with the select queries by creating the index only to create a new problem with insert, update, and delete queries on table?




     
 
0
Comment
Question by:angelz4kali
  • 3
  • 2
6 Comments
 
LVL 142

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 500 total points
ID: 24756863
better would be:
CREATE INDEX [idx_myFKCol1_myFKCol2] on [myTable]([myFKCol2], [myFKCol1])

because the value of myFKCol2 is more selective than myFKCol1.
0
 

Author Comment

by:angelz4kali
ID: 24756916
myFKCol2  has hundreds of thousands of possible values
myFKCol1 only has 20 to 30 possible values
does this make myFKCol2 more selective???

Will insert, update, delete performance be hurt significantly?
0
 
LVL 60

Expert Comment

by:chapmandew
ID: 24757015
Yes, that makes col2 more selective.

It probably won't hurt performance TOO much because the index isn't a clustered one...also, since the columns are integers they won't take up much space.
0
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 
LVL 142

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 500 total points
ID: 24757053
>myFKCol2  has hundreds of thousands of possible values
>myFKCol1 only has 20 to 30 possible values
>does this make myFKCol2 more selective???

explanation:
say you have 1 Million rows.
for 1 given value of myFKCol1 (of which you have +-25 distinct values), that will potentially bring back 100000/25 = 40000 records.
for 1 given value of myFKCol2 (of which you have +-100000 distinct values), that will potentially bring back 100000/100000 = 10 records.

so, searching (and hence indexing) by myFKCol2 first will be more selective, as immediately much less records have to be considered for further conditions.
0
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 24757064
note: if your table does not yet have a clustered index, you should consider to create this one as clustered, or change one of the existing index to clustered.
the decision is usually taken by considering
§ range queries on the indexed columns (pro clustered index)
§ updates of the valued of the indexed columns (con clustered index)
0
 

Author Comment

by:angelz4kali
ID: 24804516
I went ahead and implemented the index and have a gorgeous video watching the CPU drop from avg 99% utilization during peak usage to avg 4% utilization during peak usage.

Thanks for all your help
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

803 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