Solved

SQL Server Query Help

Posted on 2009-07-15
7
153 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
Get up to 2TB FREE CLOUD per backup license!

An exclusive Black Friday offer just for Expert Exchange audience! Buy any of our top-rated backup solutions & get up to 2TB free cloud per system! Perform local & cloud backup in the same step, and restore instantly—anytime, anywhere. Grab this deal now before it disappears!

 
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

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Join & Write a Comment

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
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…
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 set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

757 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

21 Experts available now in Live!

Get 1:1 Help Now