Solved

SQL Server Query Help

Posted on 2009-07-15
7
157 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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
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

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
disk usage reporting tools 27 52
Searching a barcode number within a string. 7 25
sql query 5 44
Connect remotely to SQL Server DB without TCP/IP enabled? 2 33
Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

749 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