I have a part of a query which I am trying to optimize. My tables have a lot of info and it would be good for us to know if we could optimize it a little bit.
This part is the one that is taking longer:
CASE
WHEN {SPS_FACTURAS}.[TipoFactura] = 'F'
AND {SPS_FACTURAS}.[IsPurged] = 0 THEN (SELECT COUNT(DISTINCT NumRec)
FROM {SPS_LINFARMA} linfarma
WHERE linfarma.[IdCodFact] = {SPS_FACTURAS}.[IdCodFact])
WHEN {SPS_FACTURAS}.[TipoFactura] = 'F'
AND {SPS_FACTURAS}.[IsPurged] = 1 THEN (SELECT COUNT(DISTINCT NumRec)
FROM {SPS_HISTLINFARMA} histfarm
WHERE histfarm.[IdCodFact] = {SPS_FACTURAS}.[IdCodFact])
WHEN {SPS_FACTURAS}.[TipoFactura] = 'C'
AND {SPS_FACTURAS}.[NomUsrIns] <> 'WS_Faturacao'
AND {SPS_FACTURAS}.[IsPurged] = 0 THEN (SELECT COUNT(DISTINCT NUMFICH)
FROM {SPS_LINFACT2} linha
WHERE linha.[IdCodFact] = {SPS_FACTURAS}.[IdCodFact])
WHEN {SPS_FACTURAS}.[TipoFactura] = 'C'
AND {SPS_FACTURAS}.[NomUsrIns] <> 'WS_Faturacao'
AND {SPS_FACTURAS}.[IsPurged] = 1 THEN (SELECT COUNT(DISTINCT NUMFICH)
FROM {SPS_HISTLINFACT} histlin
WHERE histlin.[IdCodFact] = {SPS_FACTURAS}.[IdCodFact])
WHEN {SPS_FACTURAS}.[TipoFactura] = 'C'
AND {SPS_FACTURAS}.[NomUsrIns] = 'WS_Faturacao'
THEN (SELECT COUNT(DISTINCT NUMFICH)
FROM {SPS_LINFACTWS} linWS
WHERE linWS.[IdCodFact] = {SPS_FACTURAS}.[IdCodFact])
END
Is there a way I can optimize it?
Thanks a lot in advance,
Vincent Colpa
Related
I have been asked to tune the below query and would like to know if there is any better way to tune it?
SELECT req_dtl.lab_ord_occ_test_id ,
req_dtl.order_ref_no ,
req_dtl.accession_no ,
req_dtl.test_code ,
req_dtl.test_name ,
req_dtl.test_id ,
req_dtl.schedule_id ,
req_dtl.lab_ord_occ_id ,
req_dtl.order_type ,
lab_occ.facility_id ,
lab_occ.patient_id ,
lab_occ.order_draw_dt ,
hdr.source_system ,
(SELECT CORPORATION_ACRONYM
FROM corporation c,
facility f
WHERE c.corporation_id = f.corporation_id
AND f.facility_id = lab_occ.facility_id) AS corporation_acronym,
tst.container ,
lab_occ.order_duration_type ,
occ_test.mnc_yn
FROM ORDER_REQUISITION_HEADER hdr ,
ORDER_REQUISITION_DETAIL req_dtl ,
LAB_ORDER_OCC_TEST occ_test ,
LAB_ORDER_OCC lab_occ ,
TEST tst
WHERE hdr.requisition_hdr_id = in_requisition_hdr_id
AND hdr.msg_sent_to_lab_yn = 'Y'
AND req_dtl.requisition_hdr_id = hdr.requisition_hdr_id
AND occ_test.lab_order_occ_test_id = req_dtl.lab_ord_occ_test_id
AND req_dtl.test_id = tst.test_id
AND tst.accession_type NOT LIKE 'CMP%'
AND occ_test.status != 'R'
AND occ_test.lab_order_occ_id = lab_occ.lab_order_occ_id
AND lab_occ.status = 'A'
AND occ_test.created_dt >= hdr.msg_sent_to_lab_dt
AND NVL(occ_test.test_sent_to_lab_yn,'N') = 'N'
AND NOT EXISTS
(SELECT orddata.*
FROM MISSING_ORDER_DATA orddata,
TEST_CONFIG_HOLD_AOE tcha
WHERE orddata.test_id = tcha.test_id
AND tcha.active_yn = 'Y'
AND orddata.status_flag = 'A'
AND orddata.answer IS NULL
AND orddata.msg_sent_to_lab_yn = 'N'
AND orddata.lab_order_occ_test_id=occ_test.lab_order_occ_test_id
)
ORDER BY req_dtl.accession_no;
In the execution plan no tables are going for full table scan.Only nested loops are more.
*Suggest better way to tune this query *
AND NOT EXISTS
(SELECT orddata.*
FROM MISSING_ORDER_DATA orddata,
TEST_CONFIG_HOLD_AOE tcha
WHERE orddata.test_id = tcha.test_id
AND tcha.active_yn = 'Y'
AND orddata.status_flag = 'A'
AND orddata.answer IS NULL
AND orddata.msg_sent_to_lab_yn = 'N'
AND orddata.lab_order_occ_test_id=occ_test.lab_order_occ_test_id
)
could be moved to FROM
FROM
...
LEFT JOIN (SELECT DISTINCT orddata.lab_order_occ_test_id
FROM MISSING_ORDER_DATA orddata,
TEST_CONFIG_HOLD_AOE tcha
WHERE orddata.test_id = tcha.test_id
AND tcha.active_yn = 'Y'
AND orddata.status_flag = 'A'
AND orddata.answer IS NULL
AND orddata.msg_sent_to_lab_yn = 'N'
) missing ON missing.lab_order_occ_test_id = occ_test.lab_order_occ_test_id
WHERE missing.lab_order_occ_test_id IS NULL
Also you should move the acronym
FROM
...
INNER JOIN (SELECT CORPORATION_ACRONYM, f.facility_id
FROM corporation c,
facility f
WHERE c.corporation_id = f.corporation_id) acr ON
acr.facility_id = lab_occ.facility_id)
...Additionally, the TEST object must have an index on accession_type otherwise the tst.accession_type not like 'CMP%' clause will be slower than necessary.
Also, the clause: NVL(occ_test.test_sent_to_lab_yn,'N') = 'N' is essentially an outer join on partially-validated data. Does the test_sent_to_lab_yn column in occ_test contain nulls? If not, consider using an IN clause along with a valid list. [it looks like a yes/no column, maybe this should be equality on 'Y' and get someone to clean up the nulls?]
Please post the explain plan so we can suggest a re-ordering of the predicates in order to minimize the row-returns in first clause....and make HINT suggestions.
I was trying to do a inner join with 3 tables in a update query. I tried to find the solution in multiple sites but didn't get the solution.
Following a sample query I am trying:
UPDATE TGT
SET C1 = CASE WHEN TGT.c2 = SRC.c2 AND SRC.C3 = 'P' THEN SRC.C1 ELSE NULL END,
C2 = CASE WHEN TGT.c2 = SRC.c2 AND SRC.C3 = 'D' THEN SRC.C1 ELSE NULL END
FROM SRC
INNER JOIN SRC1
ON SRC.C9 = SRC1.C9
AND SRC.C9 = TGT.C9;
Thanks in Advance!!
I would expect your syntax to work. (I don't have Vertica handy but its query parser is based on Postgres.)
Perhaps -- unlike Postgres -- JOIN is not allowed in the FROM. Then you can put the join conditions in the WHERE clause:
UPDATE TGT
SET C1 = (CASE WHEN TGT.c2 = SRC.c2 AND SRC.C3 = 'P' THEN SRC.C1 END)
C2 = (CASE WHEN TGT.c2 = SRC.c2 AND SRC.C3 = 'D' THEN SRC.C1 END)
FROM SRC, SRC1
WHERE SRC.C9 = SRC1.C9 AND SRC.C9 = TGT.C9;
I am trying to optimize the query below
if exists (select 1
from GHUB_DISCREPANCY_REPORT (NOLOCK)
where PARTNO = #currentpn and orderID = #oldorderid + 1
and (Discr_Fox_Available = 'Y'
or Discr_Fox_NC = 'Y' or Discr_FOC_Available = 'Y'
or Discr_FOC_NC = 'Y' or Discr_Cpa_Available = 'Y'
or Discr_Cpa_NC = 'Y' or Discr_Fox_Tot = 'Y'
or Discr_FOC_Tot = 'Y' or Discr_Cpa_Tot = 'Y'))
I indexed the primary key, PartNo, Aging and OrderID columns.
Is there any other way I can optimize this query ?
Please suggest!
First, try an index on GHUB_DISCREPANCY_REPORT(PARTNO, orderId). This may be a big help for you query.
If you still have performance problems, one method is to use to separate queries, each of which can be optimized with a separate index.
if exists (select 1
from GHUB_DISCREPANCY_REPORT (NOLOCK)
where PARTNO = #currentpn and orderID = #oldorderid and Discr_Fox_Available = 'Y'
) or
. . .
And then having a separate composite index for each combination: GHUB_DISCREPANCY_REPORT(PARTNO, orderId, Discr_Fox_Available). This is a lot of index overhead, but could be worth it.
Another idea is to combine all the flags into one:
alter table GHUB_DISCREPANCY_REPORT
add Any_Flags as (case when (Discr_Fox_Available = 'Y'
or Discr_Fox_NC = 'Y' or Discr_FOC_Available = 'Y'
or Discr_FOC_NC = 'Y' or Discr_Cpa_Available = 'Y'
or Discr_Cpa_NC = 'Y' or Discr_Fox_Tot = 'Y'
or Discr_FOC_Tot = 'Y' or Discr_Cpa_Tot = 'Y' then 'Y' else 'N' end);
You can add an index on a computed column and then use the value in your query:
create index idx_GHUB_DISCREPANCY_REPORT_anyflags on GHUB_DISCREPANCY_REPORT(PARTNO, OrderId, AnyFlags);
if exists (select 1
from GHUB_DISCREPANCY_REPORT (NOLOCK)
where PARTNO = #currentpn and orderID = #oldorderid and AnyFlags = 'Y'
)
"top 1 1 from tbl_name" gives much better performance than " select 1" in the "if exists" (sub query) part.
Refer to this discussion
After using "top 1 1" estimated number of rows is reduce to 1 from 135765 in my case.
i try to combine these two statements to one, but all my tries failed!
Is it possible to merge them?
-- Is there a open answer?
SELECT CASE COUNT(tbl_Communication.pk_Communication) WHEN 0
THEN 0 ELSE 1 END AS hasAnsweredCom
FROM tbl_Communication
JOIN tbl_CommunicationElements ON tbl_CommunicationElements.pk_Communication = tbl_Communication.pk_Communication
WHERE tbl_Communication.pk_Ticket = #pk_Ticket
AND tbl_Communication.isClosed = 0
AND tbl_Communication.pk_CommunicationType = (SELECT pk_CommunicationType
FROM tbl_CommunicationType
WHERE name = 'query')
-- Get the answer text
SELECT TOP 1 tbl_Communication.subject AS hasAnsweredComStepName
FROM tbl_Communication
JOIN tbl_CommunicationElements ON tbl_CommunicationElements.pk_Communication = tbl_Communication.pk_Communication
WHERE tbl_Communication.pk_Ticket = #pk_Ticket
AND tbl_Communication.isClosed = 0
AND tbl_Communication.pk_CommunicationType = (SELECT pk_CommunicationType
FROM tbl_CommunicationType
WHERE name = 'query')
ORDER BY tbl_Communication.pk_Communication
Right join trick.
SELECT TOP 1
CASE WHEN tbl_CommunicationElements.pk_Communication IS NULL THEN 0 ELSE 1 END hasAnsweredCom
, tbl_Communication.subject AS hasAnsweredComStepName
FROM tbl_Communication
JOIN tbl_CommunicationElements ON tbl_CommunicationElements.pk_Communication = tbl_Communication.pk_Communication
RIGHT JOIN (VALUES(1)) AS Ext(x) ON (
tbl_Communication.pk_Ticket = #pk_Ticket
AND tbl_Communication.isClosed = 0
AND tbl_Communication.pk_CommunicationType = (SELECT pk_CommunicationType
FROM tbl_CommunicationType
WHERE name = 'query')
)
If you are willing to put the two results on one line, the following works:
select (CASE count(*) WHEN 0 THEN 0 ELSE 1 END) AS hasAnsweredCom,
MAX(case when seqnum = 1 then subject end) as hasAnsweredComStepName
from (SELECT tbl_Communication.pk_Communication, tbl_Communication.subject,
ROW_NUMBER() over (order by pk_communication) as seqnum
FROM tbl_Communication
JOIN tbl_CommunicationElements ON tbl_CommunicationElements.pk_Communication = tbl_Communication.pk_Communication
WHERE tbl_Communication.pk_Ticket = #pk_Ticket
AND tbl_Communication.isClosed = 0
AND tbl_Communication.pk_CommunicationType = (SELECT pk_CommunicationType
FROM tbl_CommunicationType
WHERE name = 'query')
) t
The second value will be NULL if there are no answers.
As for returning two rows. My guess is that subject is a string whereas hasAnsweredCom is an integer. The types conflict, so any sort of union or bringing the results together will probably result in a type conflict on the second row.
I cannot seem to find via searching how I would be able to return some of the columns from the the below sub queries. Specifically B.TAP_STAT_HSL/C.TAP_STAT_HSL. I'm not sure if I should be joining instead, but any help would be greatly appreciated.
SELECT
A.HSE_KEY_HSE AS HOUSEKEY,
A.DROP_STAT_HSE AS DROPSTATUS
A.TAP_STAT_HSL AS ITAPSTAT
FROM OPS$SEA.HSE_BASE,OPS$SEA.HSL_LOB,OPS$SEA.OOR_ORDER_OPEN A
WHERE A.HSE_KEY_HSE = A.HSE_KEY_HSL
AND A.HSE_KEY_HSL = A.HSE_KEY_OOR
AND A.DROP_STAT_HSE = '1'
AND A.LOB_IND_HSL = 'I'
AND A.TAP_STAT_HSL IN ('0','2')
AND A.ORD_STAT_OOR <> 'O'
AND EXISTS (SELECT 1
FROM OPS$SEA.HSE_BASE B,OPS$SEA.HSL_LOB B, OPS$SEA.OOR_ORDER_OPEN B
WHERE A.HSE_KEY_HSE = B.HSE_KEY_HSE
AND B.HSE_KEY_HSE = B.HSE_KEY_HSL
AND B.HSE_KEY_HSL = B.HSE_KEY_OOR
AND B.DROP_STAT_HSE = '1'
AND B.LOB_IND_HSL = 'C'
AND B.TAP_STAT_HSL IN ('0','2')
AND B.ORD_STAT_OOR <> 'O')
AND EXISTS (
SELECT 1
FROM OPS$SEA.HSE_BASE C,OPS$SEA.HSL_LOB C, OPS$SEA.OOR_ORDER_OPEN C
WHERE A.HSE_KEY_HSE = C.HSE_KEY_HSE
AND C.HSE_KEY_HSE = C.HSE_KEY_HSL
AND C.HSE_KEY_HSL = C.HSE_KEY_OOR
AND C.DROP_STAT_HSE = '1'
AND C.LOB_IND_HSL = 'T'
AND C.TAP_STAT_HSL IN ('0','2')
AND C.ORD_STAT_OOR <> 'O')}
Hmmm....
I believe your query can be re-written as follows:
WITH Allowed_Rows (houseKey, dropStatus, ipApStat, indicator)
as (SELECT a.HSE_KEY_HSE, a.DROP_STAT_HSE,
b.TAP_STAT_HSL, b.LOB_IND_HSL
FROM OPS$SEA.HSE_BASE as a
JOIN OPS$SEA.HSL_LOB as b
ON b.HSE_KEY_HSL = a.HSE_KEY_HSE
AND b.LOB_IND_HSL IN ('I', 'C', 'T')
AND b.TAB_STAT_HSL IN ('0', '2')
JOIN OPS$SEA.OOR_Order_Open as c
ON c.HSE_KEY_OOR = a.HSE_KEY_HSE
AND c.ORD_STAT_OOR <> '0'
WHERE a.DROP_STAT_HSE = '1')
SELECT houseKey, dropStatus, ipApStat
FROM Allowed_Rows as a
WHERE a.indicator = 'I'
AND EXISTS (SELECT '1'
FROM Allowed_Rows as b
WHERE b.houseKey = a.houseKey
AND b.indicator = 'C')
AND EXISTS (SELECT '1'
FROM Allowed_Rows as b
WHERE b.houseKey = a.houseKey
AND b.indicator = 'T')
You didn't properly qualify some of your tables, and used the same alias for multiple tables (which I'm surprised didn't generate a syntax error), so I had to make my best guess as to where things actually belong. There are a couple of other variations possible, depending on the other (unlisted) requirements and constraints.
And why and how do you need to return the 'other' values of TAP_STAT_HSL? DO you need all possible combinations? The value of the row for B or C instead of A? What?