Need help to optimize this Sql Query - performance

I want some help to optimize this SQL Query.
This is working completely fine. I just want to reduce the runtime of this query
select distinct
o.usrp_order_number,t.*
from ms_bvoip_order_extension oe
inner join ms_order o on oe.ms_order_id = o.ms_order_id
inner join ms_sub_order so on so.ms_order_id = o.ms_order_id
inner join ms_job j on j.entity_id = so.ms_sub_order_id
left join mstask t ON t.wf_job_id = j.wf_job_id
where
o.order_type = 900
and o.entered_date between date_sub(current_date(),53) and
date_sub(current_date(),3)
and j.entity_type = 5 and t.name RLIKE 'Error|Correct|Create AOTS Ticket' and t.wf_job_id is not null
order by
o.usrp_order_number

The WHERE conditions are being executed after joins in Hive (though CBO and PPD may change this behavior), better study the EXPLAIN output for both queries. You can move conditions like this: o.order_type = 900 to the join ON clause to reduce rows on join. Only non-equi conditions involving both tables columns are not allowed in the join ON clause in Hive. Also table t is left-joined, but conditions in the where: t.name RLIKE 'Error|Correct|Create AOTS Ticket' and t.wf_job_id is null and t.ORIGINAL_START_DATE is not null transform left join to the inner join. Check do you need INNER or LEFT JOIN
select distinct
o.usrp_order_number,t.*
from ms_bvoip_order_extension oe
inner join ms_order o
on oe.ms_order_id = o.ms_order_id
and o.order_type = 900
and and o.entered_date between date_sub(current_date(),53) and date_sub(current_date(),3)
inner join ms_sub_order so on so.ms_order_id = o.ms_order_id
inner join ms_job j on j.entity_id = so.ms_sub_order_id
and j.entity_type = 5
left join mstask t on t.wf_job_id = j.wf_job_id
and t.name RLIKE 'Error|Correct|Create AOTS Ticket'
and t.wf_job_id is null
and t.ORIGINAL_START_DATE is not null
order by o.usrp_order_number
Also read this answer about configuration settings: https://stackoverflow.com/a/48487306/2700344

be sure you have proper index on
table ms_order a composite index on columns entered_date, order_type, ms_order_id
table ms_job a composite index on columns entity_type, entity_id
table mstask a composite index on columns wf_job_id, ORIGINAL_START_DATE
table ms_sub_order an index on column ms_order_id
table ms_bvoip_order_extension and index on column ms_order_id

You will need to add indexes for the columns that you are filtering by.
We do not know how many records each of your tables are holding, but the t.name RLIKE criteria should be evaluated as the very last item. I would rewrite your query based on the following idea:
select ...
from
(
select ...
inner join ...
inner join ...
inner join ...
left join ...
where ...
) temporary
where temporary.somename RLIKE 'Error|Correct|Create AOTS Ticket'
o.usrp_order_number
If the query is not very dynamic, then you can even cache the results for a while.

Related

How to tuning is not null to range scan index in Oracle explain plan?

I m building a search query right now. I used the Oracle 19c database.
sample query:
select * from t1
inner join t2 on t2.id = t1.id and t2.name <> 'AKA'
left outer join t3 Buyer on Buyer.id = t1.id and Buyer.userId is null
left outer join t3 Seller on Seller.id = t1.id and Seller.userId = t1.userId
where rownum < 500;
This query runs super slow without indexes. It took like 14s to feed the result.
I checked the explain plan and give some indexes to the database. The query time down to 3s! Yeah. BUT,BUT,BUTBBBBBUT. It's not enough. I need it to react even faster like 1s.
I rechecked the autotrace. There still two place use full scan event I gave them the index.
The first is
INDEX t2.name
- Filter Predicates
- t2.NAME<>'AKA'
Is there a way to do <> index in Oracle? I tried build index in multiple ways. For example,
create index t2_name_idx on t2 (case when name <> 'AKA' then name end);, not work. Also the sample way like create index t2_name_idx on t2 (name); not works too.
The second is
HASH JOIN RIGHT OUTER 890639 556473 400400 2906406
Access Predicates
AND
T3.ID=T1.ID
T3.USERID=T1.USERID
TABLE ACCESS
ADDRESSES BY INDEX ROWID BATCHED 1225069 314696 384117 596570
INDEX
T3_UERID FULL SCAN 1225069 4708 4678 121216
Filter Predicates
T3.USERID IS NOT NULL
Above means that when doing left outer join t3 Seller on Seller.id = t1.id and Seller.userId = t1.userId it will check the t3.userid is not null first. The index T3_USERID is just a simple non-unique index on table T3 col T3.userid.
Any help or hint will be appreciated.

Error when running a sub query in Oracle SQL

I am trying to join three tables using a sub query.
The result of the first left outer join is to be used with another table to get a composite view with all attributes.
I am getting an error where the compile says, Unknown Command for the table in the second join clause.
When I create two independent views and then join then it works fine.
(select
l.ENROLLED_CONTENT,
l.LEARNING_ENROLLMENT_LEARNER,
l.EMPLOYEE_ID,
l.JOB_FAMILY_GROUP,
l.EMPLOYEE_TYPE,
l.JOB_FAMILY,
l.LEARNING_ENROLLMENT,
l.COMPLETION_STATUS,
l.COMPLETION_DATE,
l.EXPIRATION_DATE,
l.CF_LRV_LEARNING_CONTENT_NUMBER,
l.LEARNING_CONTENT_DETAIL,
l.LEARNING_CONTENT_TYPE,
l.LESSON_TYPE,
e.id# "WK_WORKER_ID"
from tgt_workday.learning l
left outer join ods_hrmaster.employee e
on l.EMPLOYEE_ID = e.employee#) t1
left outer join ( select
per_ids_id,
per_id,
id_pureid from
ods_pure.person_ids
) t2 on t1.wk_worker_id = t2.value where t2.type = 'Employee ID';
You can write it in a simple way. There is no need to make sub-queries as:
SELECT L.ENROLLED_CONTENT,
L.LEARNING_ENROLLMENT_LEARNER,
L.EMPLOYEE_ID,
L.JOB_FAMILY_GROUP,
L.EMPLOYEE_TYPE,
L.JOB_FAMILY,
L.LEARNING_ENROLLMENT,
L.COMPLETION_STATUS,
L.COMPLETION_DATE,
L.EXPIRATION_DATE,
L.CF_LRV_LEARNING_CONTENT_NUMBER,
L.LEARNING_CONTENT_DETAIL,
L.LEARNING_CONTENT_TYPE,
L.LESSON_TYPE,
E.ID# "WK_WORKER_ID"
FROM TGT_WORKDAY.LEARNING L
LEFT OUTER JOIN ODS_HRMASTER.EMPLOYEE E
ON L.EMPLOYEE_ID = E.EMPLOYEE#
LEFT OUTER JOIN ODS_PURE.PERSON_IDS T2
ON E.ID# = T2.VALUE
AND T2.TYPE = 'Employee ID';
Once you use the outer joined table's column in WHERE clause, It will result in the same result as inner join(there is another ways to use it in WHERE clause though). So it is better to avoid using outer joined table's column in the WHERE clause.
Try as
SELECT *
FROM ( (SELECT l.ENROLLED_CONTENT,
l.LEARNING_ENROLLMENT_LEARNER,
l.EMPLOYEE_ID,
l.JOB_FAMILY_GROUP,
l.EMPLOYEE_TYPE,
l.JOB_FAMILY,
l.LEARNING_ENROLLMENT,
l.COMPLETION_STATUS,
l.COMPLETION_DATE,
l.EXPIRATION_DATE,
l.CF_LRV_LEARNING_CONTENT_NUMBER,
l.LEARNING_CONTENT_DETAIL,
l.LEARNING_CONTENT_TYPE,
l.LESSON_TYPE,
e.id# "WK_WORKER_ID"
FROM tgt_workday.learning l
LEFT OUTER JOIN ods_hrmaster.employee e
ON l.EMPLOYEE_ID = e.employee) t1
LEFT OUTER JOIN
(SELECT per_ids_id, per_id, id_pureid FROM ods_pure.person_ids) t2
ON t1.wk_worker_id = t2.VAL AND t2.TYPE = 'Employee ID')

Join to another table when the join column is null

I have a sql that is selecting many things from the database however I would like that data to only comeback which is matched to a personal table I have.
I would like to join a column [vin_code] from my table [population] however there are nulls in here and were there are nulls I would like to join another column from my table to another table in the database.
I will give an example sql below:
Select distinct v.kegal_rntity_id
From vin v
Inner join ops$dami.population pop
On v.vin_code = pop.vin_code
Then were pop.vin_code is null I would like to join pop.vis_code on a table in the database called zegal_rentity z column z.vis_code
So something like
join zegal_rentity z
On pop.vis_code = z.vis_code
But I only want to do this were pop.vin_code is null
As sample data is not available, I am unable to test the solution but try the following query with condition based outer join.
Select distinct v.kegal_rntity_id
From ops$dami.population pop
Left join vin v
On v.vin_code = pop.vin_code
Left join zegal_rentity z
On (case when pop.vin_code is null and
pop.vis_code = z.vis_code then 1 end = 1);
Cheers!!

Oracle query select data in multi tables

I have 2 tables.
This is tableA
(invoice,D/O, cost..) and
Table B
(D/O, GRN, Qty)
Now how to use query to show table A include GRN,Qty
See
You need a LEFT OUTER JOIN to retrieve all the records from table A with matched records from table B.
Guessing at the join criteria because your question doesn't say what they are:
select a.*
, b.grn
, b.grn_line
, b.qty_grn
from a
left outer join b
on a.do = b.do
and a.do_line = b.do_line
and a.invoice_line = b.grn_line

PL SQL - Join 2 tables and return max from right table

Trying to retrive the MAX doc in the right table.
SELECT F43.PDDOCO,
F43.PDSFXO,
F43.PDLNID,
F43.PDAREC/100 As Received,
F431.PRAREC/100,
max(F431.PRDOC)
FROM PRODDTA.F43121 F431
LEFT OUTER JOIN PRODDTA.F4311 F43
ON
F43.PDKCOO=F431.PRKCOO
AND F43.PDDOCO=F431.PRDOCO
AND F43.PDDCTO=F431.PRDCTO
AND F43.PDSFXO=F431.PRSFXO
AND F43.PDLNID=F431.PRLNID
WHERE F431.PRDOCO = 401531
and F431.PRMATC = 2
and F43.PDLNTY = 'DC'
Group by
F43.PDDOCO,
F43.PDSFXO,
F43.PDLNID,
F43.PDAREC,
F431.PRAREC/100
This query is still returning the two rows in the right table. Fairly new to SQL and struggling with the statement. Any help would be appreciated.
Without seeing your data it is difficult to tell where the problem might so I will offer a few suggestions that could help.
First, you are joining with a LEFT JOIN on the PRODDTA.F4311 but you have in the WHERE clause a filter for that table. You should move the F43.PDLNTY = 'DC' to the JOIN condition. This is causing the query to act like an INNER JOIN.
Second, you can try using a subquery to get the MAX(PRDOC) value. Then you can limit the columns that you are grouping on which could eliminate the duplicates. The query would them be similar to the following:
SELECT F43.PDDOCO,
F43.PDSFXO,
F43.PDLNID,
F43.PDAREC/100 As Received,
F431.PRAREC/100,
F431.PRDOC
FROM PRODDTA.F43121 F431
INNER JOIN
(
-- subquery to get the max
-- then group by the distinct columns
SELECT PDKCOO, max(PRDOC) MaxPRDOC
FROM PRODDTA.F43121
WHERE PRDOCO = 401531
and PRMATC = 2
GROUP BY PDKCOO
) f2
-- join the subquery result back to the PRODDTA.F43121 table
on F431.PRDOC = f2.MaxPRDOC
AND F431.PDKCOO = f2.PDKCOO
LEFT OUTER JOIN PRODDTA.F4311 F43
ON F43.PDKCOO=F431.PRKCOO
AND F43.PDDOCO=F431.PRDOCO
AND F43.PDDCTO=F431.PRDCTO
AND F43.PDSFXO=F431.PRSFXO
AND F43.PDLNID=F431.PRLNID
AND F43.PDLNTY = 'DC' -- move this filter to the join instead of the WHERE
WHERE F431.PRDOCO = 401531
and F431.PRMATC = 2
If you provide your table structures and some sample data, it will be easier to determine the issue.

Resources