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

x
?
Solved

SQL Server Query Help

Posted on 2009-07-15
7
Medium Priority
?
165 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 1500 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
When trying to connect from SSMS v17.x to a SQL Server Integration Services 2016 instance or previous version, you get the error “Connecting to the Integration Services service on the computer failed with the following error: 'The specified service …
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

721 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