Solved

Query Taking long time

Posted on 2013-11-04
7
481 Views
Last Modified: 2013-11-06
If I run the query , the data comes back fast but removing the comment, it takes forever
 takes forever when the commented lines dealing with TxOrganStatus are included?
Can anyone let me know how to optimize this?

select
   pl.patientid
  ,txe.Txeventid
from (    select distinct patientid
              from trntb_LabResult
             where LabResultId between 43699849
                                            and 43781505    
       ) pl
join trntb_TxEvent txe
on txe.PatientId  = pl.patientid  
join trntb_RecipientTxEntity rent
  on rent.TxEventId = txe.TxEventId
--join trntb_TxOrganStatus os
--  on os.TxeventId = txe.TxEventId  
--  and os.TxEntityTypeCode = rent.TxEntityTypeCode
where rent.TxDate is not null
   and rent.TxEntityTypeCode = 5
--   and os.OrganStatus = 19071 -- Active


Trntb_TxOrganStatus has the following Index:
CREATE NONCLUSTERED INDEX EventEntityCode
    ON dbo.trntb_TxOrganStatus(TxEventId,TxEntityTypeCode)
0
Comment
Question by:rbhargaw
  • 3
  • 3
7 Comments
 

Author Comment

by:rbhargaw
ID: 39622854
If I run  on os.TxeventId = rent.TxEventId , then the query runs faster.

Any reason why?
0
 
LVL 34

Expert Comment

by:Brian Crowe
ID: 39623025
Run the query with the "Include Actual Execution Plan" option.  Do you have any table scans or key lookups?  Is there a suggested index change?
0
 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 39625616
You'll probably need this index:

TxOrganStatus ( TxeventId, TxEntityTypeCode ) INCLUDE ( OrganStatus )
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

Author Comment

by:rbhargaw
ID: 39625652
The Slow.txt is the execuation for query which takes forever
Fast txt is for query running faster
Slow.txt
Fast-Rent.txeventid.txt
0
 
LVL 69

Accepted Solution

by:
Scott Pletcher earned 500 total points
ID: 39625752
>> If I run  on os.TxeventId = rent.TxEventId , then the query runs faster.
Any reason why? <<

Likely there's an index that includes that column but one or both of the other columns referenced in the query.  When you use a column that's not in the index, SQL must then go back to the main table, either via a full table scan or from an index lookup.

Change the existing index, if there is one, to include the other two columns.
0
 

Author Closing Comment

by:rbhargaw
ID: 39628374
Satisfied with the explanation.
0
 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 39628468
CORRECTION, I left out "NOT", which makes it much more clear:
"
Likely there's an index that includes that column but NOT one or both of the other columns referenced in the query.  When you ...
"
0

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
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 setup several different housekeeping processes for a SQL Server.

828 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