Solved

Performance consideration of index ms sql

Posted on 2009-07-01
6
146 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 142

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 500 total points
Comment Utility
>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
Comment Utility
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
Comment Utility
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

APEX (Application Express) is used to develop a web application from Oracle. SQL Workshop is one of the tools that comes with Oracle APEX to query or modify the database objects or to make any changes to the structure.
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
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…

763 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now