Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Performance consideration of index ms sql

Posted on 2009-07-01
6
Medium Priority
?
163 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
[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
  • 3
  • 2
6 Comments
 
LVL 143

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 2000 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
Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

 
LVL 143

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 2000 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 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 2000 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

Recently, Microsoft released a best-practice guide for securing Active Directory. It's a whopping 300+ pages long. Those of us tasked with securing our company’s databases and systems would, ideally, have time to devote to learning the ins and outs…
Recently I was talking with Tim Sharp, one of my colleagues from our Technical Account Manager team about MongoDB’s scalability. While doing some quick training with some of the Percona team, Tim brought something to my attention...
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

730 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