Link to home
Start Free TrialLog in
Avatar of Nakuru1234
Nakuru1234

asked on

SQL Query Performance

Hello:

Below is a sql code that has been timing out. When I execute, it takes about 20 mins. The database expert links might help...I'll attach them shortly...

The report uses multiple versions of the same tables as it searches/looks or returns to the ED within 72 hours of the original arrival. Please can you advice/help on tuning the SQL Query or what I should do to solve the time out issue? TIA.


SELECT DISTINCT "PAT_ENC_HSP"."ED_EPISODE_ID", "PAT_ENC_HSP"."ADMIT_CONF_STAT_C", "PAT_ENC_HSP_past"."ADMIT_CONF_STAT_C", "CLARITY_ADT"."EVENT_ID", "CLARITY_ADT"."EVENT_TYPE_C", "CLARITY_ADT"."EVENT_SUBTYPE_C", "CLARITY_ADT_past"."EVENT_ID", "CLARITY_ADT_past"."EVENT_TYPE_C", "CLARITY_ADT_past"."EVENT_SUBTYPE_C", "PATIENT"."PAT_NAME", "PAT_ENC_HSP"."ADT_ARRIVAL_TIME", "PAT_ENC_HSP_past"."ADT_ARRIVAL_TIME", "PAT_ENC_HSP_past"."ED_EPISODE_ID", "PAT_ENC_HSP"."SSM_TEST_TYPE", "CLARITY_LOC"."LOC_ID", "IDENTITY_ID"."IDENTITY_TYPE_ID", "IDENTITY_ID"."IDENTITY_ID", "PATIENT"."BIRTH_DATE", "PAT_ENC_HSP"."BILL_NUM", "PAT_ENC_HSP_past"."BILL_NUM", "SSM_DIAGNOSIS"."PRIMARY_DX", "SSM_DIAGNOSIS"."COMBINED_DX", "SSM_DIAGNOSIS_past"."PRIMARY_DX", "SSM_DIAGNOSIS_past"."COMBINED_DX", "ZC_ED_DISPOSITION_past"."NAME", "ZC_ED_DISPOSITION"."NAME", "CLARITY_LOC_past"."LOC_ID"
 FROM   (((((((((((("Clarity"."dbo"."PAT_ENC_HSP" "PAT_ENC_HSP" LEFT OUTER JOIN "Clarity"."dbo"."PAT_ENC_HSP" "PAT_ENC_HSP_past" ON "PAT_ENC_HSP"."PAT_ID"="PAT_ENC_HSP_past"."PAT_ID") LEFT OUTER JOIN "Clarity"."dbo"."PATIENT" "PATIENT" ON "PAT_ENC_HSP"."PAT_ID"="PATIENT"."PAT_ID") LEFT OUTER JOIN "Clarity"."dbo"."CLARITY_ADT" "CLARITY_ADT" ON ("PAT_ENC_HSP"."PAT_ID"="CLARITY_ADT"."PAT_ID") AND ("PAT_ENC_HSP"."PAT_ENC_DATE_REAL"="CLARITY_ADT"."PAT_ENC_DATE_REAL")) LEFT OUTER JOIN "Clarity"."dbo"."SSM_DIAGNOSIS" "SSM_DIAGNOSIS" ON "PAT_ENC_HSP"."PAT_ENC_CSN_ID"="SSM_DIAGNOSIS"."PAT_ENC_CSN_ID") LEFT OUTER JOIN "Clarity"."dbo"."ZC_ED_DISPOSITION" "ZC_ED_DISPOSITION" ON "PAT_ENC_HSP"."ED_DISPOSITION_C"="ZC_ED_DISPOSITION"."ED_DISPOSITION_C") LEFT OUTER JOIN "Clarity"."dbo"."CLARITY_ADT" "CLARITY_ADT_past" ON ("PAT_ENC_HSP_past"."PAT_ID"="CLARITY_ADT_past"."PAT_ID") AND ("PAT_ENC_HSP_past"."PAT_ENC_DATE_REAL"="CLARITY_ADT_past"."PAT_ENC_DATE_REAL")) LEFT OUTER JOIN "Clarity"."dbo"."SSM_DIAGNOSIS" "SSM_DIAGNOSIS_past" ON "PAT_ENC_HSP_past"."PAT_ENC_CSN_ID"="SSM_DIAGNOSIS_past"."PAT_ENC_CSN_ID") LEFT OUTER JOIN "Clarity"."dbo"."ZC_ED_DISPOSITION" "ZC_ED_DISPOSITION_past" ON "PAT_ENC_HSP_past"."ED_DISPOSITION_C"="ZC_ED_DISPOSITION_past"."ED_DISPOSITION_C") LEFT OUTER JOIN "Clarity"."dbo"."CLARITY_DEP" "CLARITY_DEP_past" ON "CLARITY_ADT_past"."DEPARTMENT_ID"="CLARITY_DEP_past"."DEPARTMENT_ID") LEFT OUTER JOIN "Clarity"."dbo"."CLARITY_LOC" "CLARITY_LOC_past" ON "CLARITY_DEP_past"."REV_LOC_ID"="CLARITY_LOC_past"."LOC_ID") LEFT OUTER JOIN "Clarity"."dbo"."CLARITY_DEP" "CLARITY_DEP" ON "CLARITY_ADT"."DEPARTMENT_ID"="CLARITY_DEP"."DEPARTMENT_ID") LEFT OUTER JOIN "Clarity"."dbo"."CLARITY_LOC" "CLARITY_LOC" ON "CLARITY_DEP"."REV_LOC_ID"="CLARITY_LOC"."LOC_ID") LEFT OUTER JOIN "Clarity"."dbo"."IDENTITY_ID" "IDENTITY_ID" ON "PATIENT"."PAT_ID"="IDENTITY_ID"."PAT_ID"
 WHERE  "PAT_ENC_HSP"."SSM_TEST_TYPE" IS  NULL  AND "PAT_ENC_HSP_past"."ED_EPISODE_ID" IS  NOT  NULL  AND ("PAT_ENC_HSP_past"."ADT_ARRIVAL_TIME">={ts '2010-08-01 00:00:00'} AND "PAT_ENC_HSP_past"."ADT_ARRIVAL_TIME"<{ts '2010-09-01 00:00:00'}) AND ("CLARITY_ADT_past"."EVENT_ID" IS  NULL  OR "CLARITY_ADT_past"."EVENT_TYPE_C"=1 AND "CLARITY_ADT_past"."EVENT_SUBTYPE_C"<>2) AND ("CLARITY_ADT"."EVENT_ID" IS  NULL  OR "CLARITY_ADT"."EVENT_TYPE_C"=1 AND "CLARITY_ADT"."EVENT_SUBTYPE_C"<>2) AND ("PAT_ENC_HSP_past"."ADMIT_CONF_STAT_C" IS  NULL  OR  NOT ("PAT_ENC_HSP_past"."ADMIT_CONF_STAT_C"='2' OR "PAT_ENC_HSP_past"."ADMIT_CONF_STAT_C"='3')) AND ("PAT_ENC_HSP"."ADMIT_CONF_STAT_C" IS  NULL  OR  NOT ("PAT_ENC_HSP"."ADMIT_CONF_STAT_C"='2' OR "PAT_ENC_HSP"."ADMIT_CONF_STAT_C"='3')) AND "PAT_ENC_HSP"."ED_EPISODE_ID" IS  NOT  NULL  AND "PAT_ENC_HSP"."ED_EPISODE_ID">"PAT_ENC_HSP_past"."ED_EPISODE_ID" AND "CLARITY_LOC_past"."LOC_ID"=130992023 AND "CLARITY_LOC"."LOC_ID"=130992023 AND "IDENTITY_ID"."IDENTITY_TYPE_ID"=7

Avatar of Nakuru1234
Nakuru1234

ASKER

Here is additional information on the Database links in case they might help...

TIA,
N
Database-Table-links-09022010.doc
ASKER CERTIFIED SOLUTION
Avatar of David Todd
David Todd
Flag of New Zealand image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi David:

I understand. What do you mean by "I'd get that join performing first, and then worry about the related information" Please can you elaborate or give an example?

Please see the FULL table definition for PAT_ENC_HSP in the attachment...TIA

N.
PAT-ENC-HSP-Table-Definition-090.doc
Without real data it's hard to help you but there's some rules that you can always use in queries:
- Avoid OR operator
- Create indexes on foreign keys columns to speed up joins

If you can post the query plan will be helpfull.

Greetings
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Think the idea for PAT_ENC_HSP_past is to get the previous EPISODE within a specific date range (seems to be prior month), and getting that, he then needs the associated data for that previous ed_episode_id hence the other _past table aliases as well.

But it could be an inner join because of that where clause, and maybe that where clause needs to still exist because possibly ed_episode_id is not mandatory.

So, can see why the _past might be needed, and why the is not null is also needed, and can even see why the left joins are being used for everything other than PAT_ENC_HSP_past where it looks like it could be an inner join (and would even move the check for episode_id being less than up there).

But this is being constructed in Access and there are a few gotchas when doing complex joins...

Let me put it this way as I obviously was not clear the first time, if the author understands the concept of a LEFT JOIN and really needs it than the conditions in the WHERE clause are simply not going to produce the right result.  Period.
And an inner join might not negate the need to check for NULLS either :)
Not sure it was considered that way.
Besides which I do agree that PAT_ENC_HSP_past could be an inner join...

>>And an inner join might not negate the need to check for NULLS either<<
Absolutely.

If the LEFT JOIN is really needed than the following conditions should not be in the WHERE clause but rather on the OUTER JOIN:
CLARITY_LOC_past.LOC_ID = 130992023
CLARITY_LOC.LOC_ID = 130992023
IDENTITY_ID.IDENTITY_TYPE_ID = 7
Hi Mark Wills:

Why are you saying this is being constructed in Access? TIA.

N.
Hi Zberteoc:

I totally agree, the _past JOIN to itself is not necessary. TIA.

N.

mark_wills:

You said, was hard to read, so, made it suitable for the above task, and still unsure about all those double ups (yes, can see some distinctions in the where clause and will take some more work to figure it all out)
// Have you figured out anything? TIA.

N.
Would simply run it up in a query window in SSMS, and then (under the "query" menu option) analyse the query in database engine query analyser.

See what it says about indexing... And probably should ask, are you in a position to make changes on the back end SQL server ?

Would also be making a Stored Procedure out of the query (if possible) and passing a few parameters to the stored procedure.

If we can do that, then we can reconstruct some of those joins into a more SQL Server profficient way. Access will complain about a couple of the techniques we have been discussing.
The main problem here is the query itself and the indexes. What needs to be done is to make sure the indexes are how are supposed to be and then maybe rewriting the code a little bit to optimize it. Last concern here is encapsulate the query in a stored procedure. That easily can be done after the main problem is solved.
Ah well, I guess we will never know whether the author intended to use a LEFT JOIN or not.  Perhaps they do not know the difference, because as written they are certainly not OUTER JOINs.

In any case, it looks like they are in capable hands so I will bow out.

Good luck.
Dont bow out just yet - the fun is yet to happen :)
I am going through all the experts comments/advice as we speak before I close the question...just in case I might have one last question to ask. TIA.

N.
Great, will be good to hear some feedback.

And just noticed a question you asked me...

>> Why are you saying this is being constructed in Access

An assumption because of all the brackets that access insists on doing, and the word / line wraps, and thought I saw it mentioned somewhere.
To all Experts:

You were very helpful in all your comments/advice. I really appreciate even though it took some time for me to close the question. Again, kudos to everybody who contributed. TIA.

N.
All the Experts did an excellent job to help me understand the problem and the solution!