SQL query tuning issue - sql-tuning

I created a functional index on "sum(coalesce(INDICATED_REVENUE,0) - coalesce(ACTUAL_REVENUE,0)) " in query below query but still its very slow , what else can I try ?
the stats are current
select
sum(coalesce(INDICATED_REVENUE,0) - coalesce(ACTUAL_REVENUE,0))
from TAB1 t
where t.tid = '925914455'
and t. PM = '7'
and t.Token is not null

Related

Executing select statements with union all and prepare statements on Oracle

I need to execute the following UNION ALL Select statements, I am using bind variables for the WHERE conditions. I am using PyODBC for Oracle. While the code executes and returns results, it takes a lot of time if compared to running it from TOAD directly. Any Ideas what is the problem?
query = """
SELECT concat('Abstract ',display_order), abstract_text as field1,
abstract_source_code, display_order
From abstract
WHERE control_number = ? and control_level = ?
UNION ALL
SELECT 'cTitle', chapter_title, null as col2, null as col3
From chapter
WHERE control_number = ? and control_level = ?
"""
result = cursor.execute(query, ['201728871', '005', '201728871', '005']).fetchall()
print(result)

Same Query, Different Peformance

There is a query that takes more than two minutes. that query's result are 960000 row.
so i use hint. then it takes just 30 seconds. so i applied query in mybatis and run application. but that query takes more over two minute. so i copied query in log and paste local developer. and excute query. but it takes just 30 seconds.
i don't know........
environment : spring, mybatis, oracle
SELECT *
FROM (
SELECT /*+ USE_HASH(a b c) */ ROW_NUMBER() OVER(ORDER BY A.TRANS_DT DESC, A.TRANS_TM DESC) AS RNUM
,A.PAY_METHOD
,B.BRAND_NM
,A.I_MID
,C.TRANS_DT
,C.TRANS_TM
,C.TRANS_DT || C.TRANS_TM AS TRANS_DTM
,C.VACCT_VALID_DT
,C.VACCT_VALID_TM
,C.VACCT_VALID_DT||C.VACCT_VALID_TM AS VACCT_VALID_DTM
,NVL(C.DEPOSIT_DT, ' ') DEPOSIT_DT
,NVL(C.DEPOSIT_TM, ' ') DEPOSIT_TM
,NVL(C.DEPOSIT_DT, ' ')||NVL(C.DEPOSIT_TM, ' ') AS DEPOSIT_DTM
,NVL(C.DEPOSIT_AMT, 0) AS AMT
,NVL(A.AMT, 0) AS INPUT_AMT
,A.BANK_CD
,IONPAY.UF_GET_BANK_NAME(A.BANK_CD) AS BANK_CD_NM
,C.VACCT_NO
,A.BILLING_NM
,A.REFERENCE_NO
,A.TXID
,A.STATUS
,NVL(A.STATUS, ' ') AS INPUT_STATUS
,IONPAY.UF_GET_TRANS_VACCT_STATUS_NAME(C.STATUS) AS INPUT_STATUS_NAME
,C.STATUS AS TRANS_STATUS
,IONPAY.UF_GET_TRANS_VACCT_STA_NAME_2(C.STATUS, C.MATCH_CL, C.VACCT_VALID_DT, C.VACCT_VALID_TM) AS STATUS_NAME
,A.ACQU_STATUS
,A.CANCEL_DT||A.CANCEL_TM AS REVERSAL_DATE
,NVL((SELECT DESC2
FROM TB_CODE
WHERE CODE_CL = 'CHNL'
AND CODE1 = C.BANK_CD
AND CODE2 = C.CHANNEL_TYPE), ' ') AS channel
FROM TB_TRANS_HISTORY A, TB_BO_MER_MGMT B, TB_VACCT_TRANS C
WHERE A.I_MID = B.I_MID
AND A.TXID = C.TXID
AND A.I_MID = C.I_MID
AND B.I_MID = C.I_MID
AND A.PAY_METHOD IN ('02')
AND A.TRANS_DT BETWEEN '20161016' AND '20161116'
AND A.TRANS_DT||A.TRANS_TM BETWEEN '2016101600%3A0000' AND '2016111624%3A0000'
AND B.TAX_NO != 'NICEPAY'
AND C.SIMULATION_FLG = '0'
AND C.STATUS IN ('0', '1', '2', '3', '4')
) TBL
WHERE RNUM BETWEEN 210000 AND 220000
I have already met such situation: the time difference is not in SQL query execution, but on fetching results.
Oracle JDBC driver default fetch size is 10, that means the ResultSet is fed 10 rows by 10 rows, which make a lot of round-trips to DB when there are 1 million records. The Fetch size must be increased.
With PostgreSql, the default fetch size is unlimited: the ResultSet is fed with the whole result (or until OutOfMemory). The Fetch size could need to be decreased.
To specify the fetch size value in Mybatis:
Use fetchSize attribute in XML select statement:
<select id="listItems" fetchSize="300">SELECT ...</select>
Or use fetchSize option in annotations:
#Select("SELECT ...")
#Options(fetchSize=300)
A value in range 200-500 is often a good compromise.

Oracle - What is the order of statement when using functions for filter in where clause

Refer Table WORKQUEUELOG with columns (ID,QueueTypeId,CreateDateTime,WorkId,InQ,OutQ)
There is an index available for (QueueTypeId,CreateDateTime,Id)
When trying to read the first X number of records with following query it is going for a full table search.
OPEN a_CursorHandle FOR
SELECT * FROM (
SELECT * FROM WORKQUEUELOG tbl
WHERE EX_CLS_WQLOG.FILTER(
tbl.QueueTypeId,
tbl.CreateDateTime,
NULL) = 1
AND EX_CLS_WQLOG.VALIDATION(
tbl.ID ,
tbl.WorkId ,
tbl.InQ ) = 1
ORDER BY tbl.QueueTypeId ASC,tbl.CreateDateTime ASC,tbl.Id ASC)
WHERE ROWNUM <= a_MaxCount;
We changed the query as following and started using the index for reading.
ie Validaiton function first and filter function as second.
OPEN a_CursorHandle FOR
SELECT * FROM (
SELECT * FROM WORKQUEUELOG tbl
WHERE EX_CLS_WQLOG.VALIDATION(
tbl.ID ,
tbl.WorkId,
tbl.InQ ) = 1
AND EX_CLS_WQLOG.FILTER(
tbl.QueueTypeId,
tbl.CreateDateTime,
NULL) = 1
ORDER BY tbl.QueueTypeId ASC,tbl.CreateDateTime ASC,tbl.Id ASC)
WHERE ROWNUM <= a_MaxCount;
What is the order of processing the SQL statement in oracle .? left to right or right to left.
From the above example this seems to be right to left . is this a consistent behavior. Is there any other efficient way to write this query.?

oracle with clause error in adodb

I am not able to use the with clause in oracle adodb connection. I have tried both wrapping the qry is a select * from (myquery) and adding a , before my with clause. All create errors.
Here is the query
`WITH TEMPa AS (select _nbr, sum(ending) as Cost from t1 group by _nbr),
TEMPB AS (select _nbr, cost,entity
from t2
where end_tms is null and sale_date is null and entity = 110)
SELECT
TEMPA._nbr
,TEMPA.cost
,TEMPb._nbr
,TEMPB._cost
,tempb.entity
,(tempb._cost - tempa.cost) as Difference
FROM TEMPA, TEMPB
WHERE TEMPA._nbr = TEMPB._nbr(+)
and tempa.cost <> tempb.cost`
Any help would be awesome!

Error in creating query in openbravo through hql

This question might have answer ... But not for openbravo with postgresql database.
I have openbravo 3.0 framework. In my window i have two date fields namely fromdate and todate. The requirement is i have to write a hql where clause to filter the records on the basis of current date.The date field is of timestamp without timezone.
Means fromdate < currentdate
and todate > currentdate .
I went through this link and wrote the hql where clause as
e.id in(select s.id from Tablename as s where s.fromdate < current_Date and s.todate>current_date)
when i open this window i get this error as
Exception when creating query select e from Tablename as e
where ( e.id in(select s.Tablename_ID from Tablename as s where s.fromdate < (current_date) and s.todate < (current_date)
however if i remove the current date conditions as
e.id in(select s.id from Tablename as s).. It is working fine.
Is it because of current_Date function ? .I tried even with now function .. but i get the same error.
!!! Got The Error.
There is a problem in the query i wrote , in the where clause i am selecting the id's which is not correct,hence when i gave the below query it was running correctly.
(Tablename.fromdate < currrent_date and TableName.todate>current_date) There was no problem with the current_date function.
I thought may be it would help some one!!!
Tip: If you want to write hql query correctly in openbravo , pls install the Hql query tool module that is available freely for community edition of openbravo.
Happy Coding

Resources