Solved

SQL Server Query Help

Posted on 2009-07-15
7
155 Views
Last Modified: 2012-05-07
Hello,
I am running 1 query against SQL Server 2005 and Its taking too much time to return and Eating up all the CPU. The problem with the SQL is we have 2 different database on 1 server and doing a Join and query looks something like this...

and seems like A.TNId = B.NPA + B.DBB + B.RDV is causing all the trouble.

Can someone recommend any better approach to handle this without making changes to db structure? B is really big table with some million records in it.
Select Distinct TNid from db1.dbo.Table A ,  Db2.dbo.Table1 B where 
A.CreateDate > b.CreateDate
And A.TNId = B.NPA + B.DBB + B.RDV

Open in new window

0
Comment
Question by:Aanvik
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 8

Expert Comment

by:Bob Hoffman
ID: 24863896
do you have indexes built on the fields in your where clause?
0
 
LVL 5

Author Comment

by:Aanvik
ID: 24863919
yes, I have non clustered index on these columns.
0
 
LVL 75

Accepted Solution

by:
Aneesh Retnakaran earned 500 total points
ID: 24863958
test it after creating an include index   createDate Include (NPA, DBB, RDV)

or else if you can alter the table, add a computed column for  B.NPA + B.DBB + B.RDV and index createdDate (newComputedColumn )
0
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.

 
LVL 3

Expert Comment

by:TechSinger
ID: 24863965
It doesn't appear that you are using a Join statement.  This can really cause things to slow down.
Try the code in the attached snippet.
 

Select Distinct TNid from db1.dbo.Table A 
JOIN  Db2.dbo.Table1 B ON And A.TNId = B.NPA + B.DBB + B.RDV
where A.CreateDate > b.CreateDate

Open in new window

0
 
LVL 3

Expert Comment

by:TechSinger
ID: 24863981
Oop, I left the And in.  Let's try that again.
 

Select Distinct TNid from db1.dbo.Table A 
JOIN  Db2.dbo.Table1 B ON A.TNId = B.NPA + B.DBB + B.RDV
where A.CreateDate > b.CreateDate

Open in new window

0
 
LVL 8

Expert Comment

by:Bob Hoffman
ID: 24864144
Does TNid appear more then once in table 'A'? If not get rid of the "Distinct". If it does can they have a different CreateDate?
0
 
LVL 5

Author Closing Comment

by:Aanvik
ID: 31603986
Thx
0

Featured Post

Microsoft Certification Exam 74-409

VeeamĀ® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

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.
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

773 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