Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

SQL Server 2008 - Improve Select Statement

Posted on 2014-02-04
2
Medium Priority
?
441 Views
Last Modified: 2014-02-04
Is there anyway to make the below SQL faster?  When I run it it takes over a minute to return results.

Select DISTINCT top 1000
	cmp.cmpname,
	ins.first, ins.last, bp.Amount,
	ccc.amount, ccc.authorization, ccc.transaction, 
	ben.description,
	ccc.updateDT
from creditCardCharge ccc
INNER JOIN insured ins ON ccc.empid = ins.empid
INNER JOIN employee emp ON ccc.empid = emp.empid
INNER JOIN company cmp ON emp.cmpid = cmp.cmpid
INNER JOIN insuredBenefit ib ON ccc.empid = ib.empid
INNER JOIN benefitPayment bp ON ib.ibid = bp.ibid
INNER JOIN benefitPaymentBatch bpb ON bp.bpbid = bpb.bpbid
INNER JOIN benefitCoverage bc ON ib.bcid = bc.bcid 
INNER JOIN benefit ben ON bc.benid = ben.benid
ORDER BY ccc.cccupdateDT DESC

Open in new window

0
Comment
Question by:CipherIS
2 Comments
 
LVL 11

Accepted Solution

by:
John_Vidmar earned 1000 total points
ID: 39832546
Try to reduce how many tables are in your query, i.e., is benefitPaymentBatch needed?

Introduce a where-clause (ccc.cccupdateDT would make sense).

Ensure indexes exist on joined fields.

Update statistics so indexes are used.

Is there a data purge strategy?   Some tables can get huge.
0
 
LVL 12

Assisted Solution

by:Paul_Harris_Fusion
Paul_Harris_Fusion earned 1000 total points
ID: 39832660
As John says,  benefitPaymentBatch appears to play no part in the query.

I accept you may need it,  but for diagnostics,  try removing the order by clause.
If it speeds things up,  indexing the creditCardCharge UpdateDT column might help.
0

Featured Post

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

Question has a verified solution.

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

In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

885 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