Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 slow query due to criteria

Author  Topic 

smorty44
Yak Posting Veteran

93 Posts

Posted - 2008-01-24 : 10:14:23
I have a query in access that is running extremely slow and I'm trying to find a better way to write it. It appears to be the criteria statement that is causing the lag. Is there a better way to write this? And unfortunately I have to keep it in access.

query:
SELECT DISTINCT "JBC" AS ClientCode, PaymentDetail.PatientNumber, Procedures.CaseNumber, IIf(IsNull([TicketNumber]),Procedures.patientnumber & Year(Procedures.dateofservice) & Month(Procedures.dateofservice) & Day(Procedures.dateofservice),Procedures.ticketnumber) AS ClaimNumber, PaymentDetail.PaymentCounter, PaymentDetail.TransAmount, Payments.PaymentDate, PaymentDetail.AccountingDate, PaymentDetail.TransDate, payments.PaymentType & "-" & Adjustments.adjustmentcode AS CombinedPmtType, Payments.PaymentType, Payments.PaymentCode, Adjustments.AdjustmentCode, PaymentCodes.BriefDescription, PaymentCodes.LongDescription, Payments.CarrierCode, Payments.Remarks, Procedures.ProcedureCode, Procedures.DateOfService, PaymentDetail.DetailCounter
FROM ((((PaymentDetail LEFT JOIN Procedures ON (PaymentDetail.PaymentCounter = Procedures.Counter) AND (PaymentDetail.AccountingDate = Procedures.AccountingDate) AND (PaymentDetail.PatientNumber = Procedures.PatientNumber)) LEFT JOIN Payments ON (PaymentDetail.TransDate = Payments.AccountingDate) AND (PaymentDetail.TransCounter = Payments.Counter) AND (PaymentDetail.PatientNumber = Payments.PatientNumber)) LEFT JOIN PaymentCodes ON Payments.PaymentCode = PaymentCodes.PaymentCode) LEFT JOIN Adjustments ON (PaymentDetail.TransDate = Adjustments.AccountingDate) AND (PaymentDetail.PatientNumber = Adjustments.PatientNumber)) LEFT JOIN AdjustmentCodes ON Adjustments.AdjustmentCode = AdjustmentCodes.AdjustmentCode
WHERE (((Procedures.CaseNumber)=0) AND ((PaymentDetail.TransAmount)<>0) AND ((Payments.PaymentType) Is Null) AND ((PaymentDetail.DetailCounter)=2)) OR (((Procedures.CaseNumber)=0) AND ((PaymentDetail.TransAmount)<>0) AND ((Payments.PaymentType) Is Null) AND ((PaymentDetail.DetailCounter)=5) AND ((AdjustmentCodes.InsuranceIndicator)="N"));

crever
Starting Member

7 Posts

Posted - 2008-01-24 : 10:21:24
The only advice I can offer you on this is make sure you're putting the most selective elements of the where clause first. Then the rest of the conditions will have fewer rows to pick through.
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-01-24 : 10:42:04
This is an Microsoft Access query too...



E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page

smorty44
Yak Posting Veteran

93 Posts

Posted - 2008-01-24 : 10:46:09
yes, this is coming from access and it's running way too slow
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-01-24 : 10:50:52
In SQL Server, the query might look like
SELECT DISTINCT	'JBC' AS ClientCode,
PaymentDetail.PatientNumber,
Procedures.CaseNumber,
COALESCE(Procedures.TicketNumber, Procedures.PatientNumber + CONVERT(CHAR(8), Procedures.DateOfService, 112) AS ClaimNumber,
PaymentDetail.PaymentCounter,
PaymentDetail.TransAmount,
Payments.PaymentDate,
PaymentDetail.AccountingDate,
PaymentDetail.TransDate,
payments.PaymentType + '-' + Adjustments.AdjustmentCode AS CombinedPmtType,
Payments.PaymentType,
Payments.PaymentCode,
Adjustments.AdjustmentCode,
PaymentCodes.BriefDescription,
PaymentCodes.LongDescription,
Payments.CarrierCode,
Payments.Remarks,
Procedures.ProcedureCode,
Procedures.DateOfService,
PaymentDetail.DetailCounter
FROM PaymentDetail
LEFT JOIN Procedures ON Procedures.Counter = PaymentDetail.PaymentCounter
AND Procedures.AccountingDate = PaymentDetail.AccountingDate
AND Procedures.PatientNumber = PaymentDetail.PatientNumber
AND Procedures.CaseNumber = 0
LEFT JOIN Payments ON Payments.AccountingDate = PaymentDetail.TransDate
AND Payments.Counter = PaymentDetail.TransCounter
AND Payments.PatientNumber = PaymentDetail.PatientNumber
LEFT JOIN PaymentCodes ON PaymentCodes.PaymentCode = Payments.PaymentCode
LEFT JOIN Adjustments ON Adjustments.AccountingDate = PaymentDetail.TransDate
AND Adjustments.PatientNumber = PaymentDetail.PatientNumber
LEFT JOIN AdjustmentCodes ON AdjustmentCodes.AdjustmentCode = Adjustments.AdjustmentCode
WHERE PaymentDetail.TransAmount <> 0
AND Payments.PaymentType IS NULL
AND (
PaymentDetail.DetailCounter = 2
OR
PaymentDetail.DetailCounter = 5
AND AdjustmentCodes.InsuranceIndicator = 'N'
)



E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page

LoztInSpace
Aged Yak Warrior

940 Posts

Posted - 2008-01-24 : 18:30:06
quote:
Originally posted by crever

The only advice I can offer you on this is make sure you're putting the most selective elements of the where clause first. Then the rest of the conditions will have fewer rows to pick through.


Not true in SQL Server. The optimiser uses statistics about the tables in the query to determine an efficient plan. The order of your joins or predicates does not influence it.
Go to Top of Page
   

- Advertisement -