Solved

SQL Server Query Help

Posted on 2009-07-15
7
158 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
[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
  • 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
The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

 
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

Comparison of Amazon Drive, Google Drive, OneDrive

What is Best for Backup: Amazon Drive, Google Drive or MS OneDrive? In this free whitepaper we look at their performance, pricing, and platform availability to help you decide which cloud drive is right for your situation. Download and read the results of our testing for free!

Question has a verified solution.

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

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
I have a large data set and a SSIS package. How can I load this file in multi threading?
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

738 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