Performance Issue - Join/Index - performance

I have this query:
SELECT *
FROM (SELECT *
FROM jmr_bbi_dife_vendas_neg2 dife
WHERE dife.processado = 'N'
AND dife.removido = 'N'
AND dife.revertido = 'N'
AND TO_DATE(LPAD(dife.DAY, 2, '0') || LPAD(dife.MONTH, 2, '0') || dife.YEAR, 'DDMMYYYY') <= to_date('20160331','YYYYMMDD')
) dife
LEFT JOIN jmr_bbi_dade_vendas dade
ON dade.codins = 1
AND dade.codneg = 2
AND dade.tipdoc_vnd = dife.doc_type
AND dade.numdoc_vnd = dife.doc_no
AND dade.codart = dife.item
AND dade.anoper = dife.year
AND dade.mesper = dife.month;
and i create index on DADE.
Index
But when i execute the explain plan on this query, the index is not used:
Explain Plan
I already execute the gather statistics of full table, but don't work either.
Can help me please?
This two tables: DIFE and DADE are 2 tables example.
My original table of each other have millions of rows.
Thank you!

Create index on columns of both table which are being used in join-condition.
Also move dade.codins = 1 AND dade.codneg = 2 into where condition

Related

Why isn't it using the index?

Hello kind people of the internet.
I am wrecking my head trying to figure out why the optimiser isn't using my index for my query on Amazon Aurora. The query is dynamically created based on a report users have created through an applications UI, so I can't change the query per se.
The query uses these qualifiers
WHERE
table_in_question.deleted = 0
ORDER BY
table_in_question.date_modified DESC,
table_in_question.id DESC
I have an index, "my_index", which indexes these specific fields (table_in_question fields deleted, date_modified, ID) but MySQL doesn't use it.
The query takes approx 1200 ms to run. If I add FORCE INDEX (my_index) it takes about 120ms. Arguably about 10x faster - but unless I use force index, it doesn't use it.
Around 1 million rows are returned according to EXPLAIN, so I don't think it's a case of not using the index because of a low amount of rows being returned is the case.
The full query is
SELECT
case when some_table.id IS NOT NULL then some_table.id else "" end my_favorite,
table_in_question.date_entered,
table_in_question.name,
table_in_question.description,
table_in_question.pr_is_read,
table_in_question.pr_is_approved,
table_in_question.parent_type,
table_in_question.parent_id,
table_in_question.id,
table_in_question.date_modified,
table_in_question.assigned_user_id,
table_in_question.created_by
FROM
table_in_question
INNER JOIN (
SELECT
tst.team_user_is_member_of
FROM
team_sets_teams tst
INNER JOIN team_memberships team_membershipstable_in_question ON (
team_membershipstable_in_question.team_id = tst.team_id
)
AND (team_membershipstable_in_question.user_id = 'UUID')
AND (team_membershipstable_in_question.deleted = 0)
GROUP BY
tst.team_user_is_member_of
) table_in_question_tf ON table_in_question_tf.team_user_is_member_of = table_in_question.team_user_is_member_of
LEFT JOIN systemfavourites sf_table_in_question ON (sf_table_in_question.module = 'table_in_question')
AND (sf_table_in_question.record_id = table_in_question.id)
AND (sf_table_in_question.assigned_user_id = 'UUID')
AND (sf_table_in_question.deleted = '0')
INNER JOIN opportunities jt1_table_in_question ON (table_in_question.opportunity_id = jt1_table_in_question.id)
AND (jt1_table_in_question.deleted = 0)
LEFT JOIN another_table jt1_table_in_question_cstm ON jt1_table_in_question_cstm.id_c = jt1_table_in_question.id
LEFT JOIN systemfavourites table_in_question_favorite ON (table_in_question.id = table_in_question_favorite.record_id)
AND (table_in_question_favorite.deleted = '0')
AND (table_in_question_favorite.module = 'table_in_question')
AND (table_in_question_favorite.created_by = 'UUID')
LEFT JOIN users some_table ON (
some_table.id = table_in_question_favorite.modified_user_id
)
AND (some_table.deleted = 0)
WHERE
table_in_question.deleted = 0
ORDER BY
table_in_question.date_modified DESC,
table_in_question.id DESC
;
EXPLAIN shows this
id
select_type
table
partitions
type
possible_keys
key
key_len
ref
rows
filtered
Extra
1
PRIMARY
table_in_question
ALL
idx_table_in_question_tmst_id
968234
10.0
Using where; Using temporary; Using filesort
Can anyone help explain how I make an index it will actually use by default?
Thanks.

How to combine two query's results into one cell?

I have two different queries in oracle, I was able to show these queries side by side with cross join, but I want to see them in the same cell.
First query :
SELECT
pa.attrb_an_value
FROM
piece_attrb pa
WHERE
pa.piece_num_id = 1056436 AND
pa.attrb_code = 'PSP')
result : MCXTS
Second Query :
SELECT max(CAOSL.ATTRB_AN_VALUE)
FROM config_attrb_of_so_line caosl,
so_piece sp
WHERE sp.piece_num_id = 1056436
AND sp.so_id = caosl.so_id
AND sp.so_line_id = caosl.so_line_id
and SP.IS_ACTIVE_FLAG = 'Y'
AND CAOSL.ATTRB_CODE = 'GRS'
Result : DC0
I want to see like that in cell:
MCXTS - DC0
Concatenate them, then.
WITH
tab_1 (retval)
AS
-- first query
(SELECT pa.attrb_an_value
FROM piece_attrb pa
WHERE pa.piece_num_id = 1056436
AND pa.attrb_code = 'PSP'),
tab_2 (retval)
AS
-- second query
(SELECT MAX (caosl.attrb_an_value)
FROM config_attrb_of_so_line caosl, so_piece sp
WHERE sp.piece_num_id = 1056436
AND sp.so_id = caosl.so_id
AND sp.so_line_id = caosl.so_line_id
AND sp.is_active_flag = 'Y'
AND caosl.attrb_code = 'GRS')
-- final result
SELECT a.retval || ' - ' || b.retval as final_result
FROM tab_1 a CROSS JOIN tab_2 b

i have sql script which uses two query to get the result that i expect , how can i achieve this with the single query ,example below

query1 where condition = "condition1" ; queryresult1 = number1
query2 where condition = "condition2" ; queryresult2 = number2
I want number1-number2 , how can i make this possible with just a single query
Assuming i understand your question correctly, easiest way is joining two table and perform your whatever calculation.
select valueone, valuetwo, valueone - valuetwo as finalresult
from (select 18 as valueone from dual where 1 = 1) -- query one)
inner join (select 6 as valuetwo from dual where 1 = 1) -- query two)
on 1 = 1 -- join condition
where 1 = 1; -- some condition

performance of a self join on oracle db

I have this self join that is very slow on oracle DB. I have put indexes on all fields concerned. Does anybody have advice on how to increase performance?
select count(tNew.idtariffa) CONT
from tariffe tAtt
join tariffe tNew on tAtt.idtariffa = tNew.idtariffa
where (tAtt.stato_attivo = 't')
and (tNew.stato_attivo = 'f')
and (tAtt.validity_date < tNew.validity_date)
and (tAtt.dataimport < tNew.dataimport)
and (tNew.validity_date < to_date('2017-6-26','YYYY-MM-DD'))
Try PUSH_PRED hint :
select /*+ NO_MERGE(tNew) PUSH_PRED(tNew) */
count(tNew.idtariffa) CONT
from tariffe tAtt
join tariffe tNew on tAtt.idtariffa = tNew.idtariffa
where (tAtt.stato_attivo = 't')
and (tNew.stato_attivo = 'f')
and (tAtt.validity_date < tNew.validity_date)
and (tAtt.dataimport < tNew.dataimport)
and (tNew.validity_date < to_date('2017-6-26','YYYY-MM-DD'))
Exists version is worth of try:
select count(1) cont
from tariffe n
where stato_attivo = 'f'
and validity_date < date '2017-06-26'
and exists ( select null
from tariffe
where idtariffa = n.idtariffa
and stato_attivo = 't'
and validity_date < n.validity_date
and dataimport < n.dataimport )
Performance tuning without details like data volumes, data skew, index defintions, explain plan, etc is just guessing.
So here are some more guesses :)
Your driving table should be tariffe tNew as that's the one you use to top the result set.
tNew.validity_date < to_date('2017-6-26','YYYY-MM-DD'))
Now, unless tNew.stato_attivo = 'f' is extremely selective you're going to be retrieving a large chunk of all the rows in the table (depending on how far back the records go) so a Full Table Scan would be the most efficient way of grabbing those records.
The join on tariffe tAtt is problematic because idtariffa is not a unique column. So the join is a set of tNew records against a set of tAtt records. These will be filtered in memory using the criteria in the WHERE clause.
" I have put indexes on all fields concerned"
Single column indexes won't help here. You might get some joy from a compound index on all the pertinent columns:
tariffe (stato_attivo , validity_date, idtariffa, dataimport)
This would be worth building if you run this query very often.
Any other guesses? Subquery factoring to hit the main table once. Doing a Full Table Scan just once would speed things up if tariffe has a lot of columns.
with cte as (
select stato_attivo , validity_date, idtariffa, dataimport
from tariffe
where validity_date < to_date('2017-6-26','YYYY-MM-DD'
)
select count(tNew.idtariffa) CONT
from cte tNew
join cte tAtt on tAtt.idtariffa = tNew.idtariffa
where (tAtt.stato_attivo = 't')
and (tNew.stato_attivo = 'f')
and (tAtt.validity_date < tNew.validity_date)
and (tAtt.dataimport < tNew.dataimport)

Conversion of Oracle query to teradata query

How to convert this below query to equivalent teradate query. I tried but results varies a lot.
select il.domainN as listname, il.SourceID, ns.sourcename, cbo.customerid, cu.username, hv.domainN as HTname
, nvl((select 1
from mydb.customerPP cpp
where cbo.customerid = cpp.customerid
and NOT EXISTS (select 1 from mydb.customerPI cpt where cpp.customerid = cpt.customerid)
and trunc(cpp.startdate) <= sysdate
group by cpp.customerid),0) as BBID
from mydb.customerBO cbo
join mydb.customers cu on cbo.customerid = cu.customerid
join mydb.inv il on cbo.domainN = il.domainN
join mydb.Sources ns on il.SourceID = ns.SourceID
left join mydb2.HT hv on (il.domainN = hv.domainN
and hv.sDate+1 >= il.dDate
and il.dDate+1 >= hv.sDate)
where cbo.customerBOID = 1
and cu.statusid = 1
and il.sourceTID = 2
and il.joinbydate >= cbo.cDate
and trunc(il.dDate) = trunc(sysdate)
Thanks.
Use coalesce instead nvl and compare explain statements to see whether you have left outer joins converted to inner joins for nvl on Teradata.

Resources