Improving a SELECT query
I have an interactive application that periodically feeds a legacy batch transaction application with data to keep it in sync with my app. For demonstrating my problem I am using a similar but much simpler example here, using two tables only: CUST and ORD. The CUST table contains customer specific data and the ORD table contains orders placed by each customer. The primary key of CUST table is the CU_CODE that is also a foreign key (OR_CU_CODE) in the ORD table. The PK of ORD table is the OR_CODE column. See their description below:
Table: CUST
Name Null? Type
CU_CODE NOT NULL VARCHAR2(6)
CU_TYPE NOT NULL VARCHAR2(2)
CU_NAME NOT NULL VARCHAR2(80)
CU_ADDR VARCHAR2(120)
Table: ORD
Name Null? Type
OR_CODE NOT NULL VARCHAR2(8)
OR_CU_CODE NOT NULL VARCHAR2(6)
OR_DATE NOT NULL DATE
OR_TERMS VARCHAR2(40)
The legacy app needs to get both a customer and an order transaction for any order whose date is more recent than a given date. To create those transactions, I run a SELECT query like this:
SELECT 'UPD,CUST='||CU_CODE||',TYPE='||CU_TYPE||',NAME='||CU_NAME
||',ADDR='||CU_ADDR
FROM (SELECT DISTINCT CU_CODE, CU_TYPE, CU_NAME, CU_ADDR
FROM CUST, ORD WHERE OR_CU_CODE = CU_CODE
AND OR_DATE > SOME_DATE
ORDER BY 1)
UNION ALL
SELECT 'UPD,ORD='||OR_CODE||',CUST='||OR_CU_CODE||',DATE='
||TO_CHAR(OR_DATE, 'YYYYMMDD')||',TERMS='||OR_TERMS
FROM (SELECT OR_CODE, OR_CU_CODE, OR_DATE, OR_TERMS
FROM ORD WHERE OR_DATE > SOME_DATE
ORDER BY 2, 1);
This produces a batch update transaction file similar to the following:
UPD,CUST=CU1111,TYPE=COM,NAME=ACME,ADDR=ACME Address
UPD,CUST=CU2222,TYPE=RES,NAME=John Q,ADDR=John Q Address
UPD,CUST=CU3333,TYPE=GOV,NAME=IRS,ADDR=IRS Address
UPD,ORD=OR001234,CUST=CU1111,DATE=20061204,TERMS=Term1
UPD,ORD=OR001235,CUST=CU1111,DATE=20061206,TERMS=Term1
UPD,ORD=OR001236,CUST=CU1111,DATE=20061207,TERMS=Term1
UPD,ORD=OR002234,CUST=CU2222,DATE=20061205,TERMS=Term2
UPD,ORD=OR002235,CUST=CU2222,DATE=20061207,TERMS=Term2
UPD,ORD=OR003237,CUST=CU3333,DATE=20070102,TERMS=Term3
Both transaction types are sorted by the customer code first but customer update transactions also need a DISTINCT clause because we only want to send one custumer update per customer even if there were more orders placed by that customer.
This thing has been working fine in most cases except when a large transaction file is generated because the legacy app can only handle input files of only certain max. size. In such cases the operators need to manually split up the file into several smaller ones but still making sure that each file segment has customer and order transactions belonging to the same customers only. This can be really tedious with hundreads of customers and their orders. The file splitting would be much easier if I could rewrite the original SELECT statement to produce the transaction data in the following order:
UPD,CUST=CU1111,TYPE=COM,NAME=ACME,ADDR=ACME Address
UPD,ORD=OR001234,CUST=CU1111,DATE=20061204,TERMS=Term1
UPD,ORD=OR001235,CUST=CU1111,DATE=20061206,TERMS=Term1
UPD,ORD=OR001236,CUST=CU1111,DATE=20061207,TERMS=Term1
UPD,CUST=CU2222,TYPE=RES,NAME=John Q,ADDR=John Q Address
UPD,ORD=OR002234,CUST=CU2222,DATE=20061205,TERMS=Term2
UPD,ORD=OR002235,CUST=CU2222,DATE=20061207,TERMS=Term2
UPD,CUST=CU3333,TYPE=GOV,NAME=IRS,ADDR=IRS Address
UPD,ORD=OR003237,CUST=CU3333,DATE=20070102,TERMS=Term3
Here a given customer update transaction would be followed by all orders for that customer and so on.
I want the new query also in pure SQL, not in PL SQL. As dealing with DB and Oracle is only a small part of my job, I just can't figure out a solution to this problem. I suspect many of the regulars here can help me.
Thanks,
Joe
PS: Sorry, the table descriptions looked much better in the edit screen but I hope you can still figure it out from the display window.
Try something like the following (not tested)
SELECT transaction FROM (
SELECT CU_CODE,1,'UPD,CUST='||CU_CODE||',TYPE='||CU_TYPE||',NAME='||CU_NAME
||',ADDR='||CU_ADDR transaction
FROM (SELECT DISTINCT CU_CODE, CU_TYPE, CU_NAME, CU_ADDR
FROM CUST, ORD WHERE OR_CU_CODE = CU_CODE
AND OR_DATE > SOME_DATE)
UNION ALL
SELECT OR_CU_CODE,2,'UPD,ORD='||OR_CODE||',CUST='||OR_CU_CODE||',DATE='
||TO_CHAR(OR_DATE, 'YYYYMMDD')||',TERMS='||OR_TERMS transaction
FROM (SELECT OR_CODE, OR_CU_CODE, OR_DATE, OR_TERMS
FROM ORD WHERE OR_DATE > SOME_DATE)
ORDER BY 1,2;Basically, I added some columns to your query for sorting purposes then used that query as an inline view to only get the data you are interested in. Hope it runs. If not, let me know and I'll create the table and actually test it.
Similar Messages
-
Performance improvement for select query
Hi all,
need to improve performace for the below select query as it is taking long time
SELECT vbeln pdstk
FROM vbuk INTO TABLE it_vbuk1 FOR ALL ENTRIES IN it_likp
WHERE vbeln = it_likp-vbeln AND
wbstk = 'C' AND "pdstk = ' ' AND
vbtyp IN gr_delivery AND
( fkstk = 'A' OR fkstk = 'B' ) OR
( fkivk = 'A' OR fkivk = 'B' ).
Regards,
KumarHi,
Check if it_likp is sorted on vbeln.
SELECT vbeln pdstk
FROM vbuk INTO TABLE it_vbuk1 FOR ALL ENTRIES IN it_likp
WHERE vbeln = it_likp-vbeln AND
wbstk = 'C' AND
vbtyp IN gr_delivery AND
( ( fkstk = 'A' OR fkstk = 'B' ) OR <-- check this condition , if ( ) is needed ...
( fkivk = 'A' OR fkivk = 'B' ) ) .
Regards,
Srini. -
Performance improvement of Select Query
Hi All,
Please suggest me to fine tune below query with example. It is causing DUMP when I am executing it because of low memory..
select * from vbap into table it_vbap where vbeln = p_vbeln
or vgbel = p_vbeln
or vbeln = j_3avbfae-vbelv
or vgbel = j_3avbfae-vbelv
or vbeln = p_vgbel
or vgbel = p_vgbel.
Thanks in advance.
Regards,
ChandravadanCheck out this way...
declare 2 ranges it_vbeln and it_vgbel.
move values of p_vbeln, j_3avbfae-vbelv, p_vgbel to it_vbeln.
move values of p_vbeln, j_3avbfae-vbelv, p_vgbel to it_vgbel.
select * from vbap into table it_vbap
where vbeln in it_vbeln.
select * from vbap appending table it_vbap
where vbeln in it_vgbel. -
Performance improvement in select query
Hi,
I have to improve the performance of the one custome program. I applied SQL trace & found out that below extract of code is taking maximum time
SELECT a~znumdos
a~zdatctx
a~zmotctx
a~gpart
b~vkont
d~zidbarcly
dzligne dzstatulig
b~zmontdact
c~zrmot_statut
d~zcanal
g~crdat
p~schedpid
f~ktokl
FROM zr025sar AS a
INNER JOIN zr026sar AS b ON bznumdos = aznumdos
INNER JOIN zr027sar AS c ON cznumdos = aznumdos
AND cvkont = bvkont
INNER JOIN zr021sat AS d ON czcanal = dzcanal
AND czcontrat = dzcontrat
AND czligne = dzligne
INNER JOIN but000 AS g ON agpart = gpartner
INNER JOIN fkkvkp AS f ON fvkont = bvkont
AND fgpart = gpartner
LEFT OUTER JOIN zr136sar AS p ON pznumdos = aznumdos
APPENDING TABLE t_ctr_data_2
WHERE a~zcanal IN r_canal
AND a~zdatctx NE c_null_date
AND a~zdatctx < l_date_act
AND a~zmotctx IN r_motife
AND a~zetadoss IN r_dossier
AND b~zdateevent < l_date_his
AND d~zstatulig IN r_ligne
AND d~zdatstatulig NE c_null_date
AND d~zdatstatulig LE l_date_mdf
AND d~zplantarif IN r_zptarif
AND d~zstatugcr IN r_statutgcr
AND f~ktokl IN r_categ.
How can i use the for all entries in above code.. Will it enhance the performance of the code?unfortunately nobody can help you here.
Your task depends on the actually used select-options (IN), on the actual distribution of the data and the available indexes on the z-tables.
I would neither recommend you a view, which is just a dictionary stored join and also not a FOR ALL ENTRIES, which will not increase the performance but reduce duplicates at the end, whcih can change even the result.
You must analyse the statement in detail and I would recommend you to get some support by somebody more experienced.
Siegfried -
Select query performance improvement - Index on EDIDC table
Hi Experts,
I have a scenario where in I have to select data from the table EDIDC. The select query being used is given below.
SELECT docnum
direct
mestyp
mescod
rcvprn
sndprn
upddat
updtim
INTO CORRESPONDING FIELDS OF TABLE t_edidc
FROM edidc
FOR ALL ENTRIES IN t_error_idoc
WHERE
upddat GE gv_date1 AND
upddat LE gv_date2 AND
updtim GE p_time AND
status EQ t_error_idoc-status.
As the volume of the data is very high, our client requested to put up some index or use an existing one to improve the performance of the data selection query.
Question:
4. How do we identify the index to be used.
5. On which fields should the indexing be done to improve the performance (if available indexes donu2019t cater to our case).
6. What will be the impact on the table performance if we create a new index.
Regards ,
RaghavQuestion:
1. How do we identify the index to be used.
Generally the index is automatically selected by SAP (DB Optimizer ) ( You can still mention the index name in your select query by changing the syntax)
For your select Query the second Index will be called automatically by the Optimizer, ( Because the select query has u2018Updatu2019 , u2018uptimu2019 in the sequence before the u2018statusu2019 ) .
2. On which fields should the indexing be done to improve the performance (if available indexes donu2019t cater to our case).
(Create a new Index with MANDT and the 4 fields which are in the where clause in sequence )
3. What will be the impact on the table performance if we create a new index.
( Since the index which will be newly created is only the 4th index for the table, there shouldnu2019t be any side affects)
After creation of index , Check the change in performance of the current program and also some other programs which are having the select queries on EDIDC ( Various types of where clauses preferably ) to verify that the newly created index is not having the negative impact on the performance. Additionally, if possible , check if you can avoid into corresponding fields .
Regards ,
Seth -
How to improve the performance of one program in one select query
Hi,
I am facing performance issue in one program. I have given some part of the code of the program.
it is taking much time below select query. How to improve the performance.
Quick response is highly appreciated.
Program code
DATA: BEGIN OF t_dels_tvpod OCCURS 100,
vbeln LIKE tvpod-vbeln,
posnr LIKE tvpod-posnr,
lfimg_diff LIKE tvpod-lfimg_diff,
calcu LIKE tvpod-calcu,
podmg LIKE tvpod-podmg,
uecha LIKE lips-uecha,
pstyv LIKE lips-pstyv,
xchar LIKE lips-xchar,
grund LIKE tvpod-grund,
END OF t_dels_tvpod,
DATA: l_tabix LIKE sy-tabix,
lt_dels_tvpod LIKE t_dels_tvpod OCCURS 10 WITH HEADER LINE,
ls_dels_tvpod LIKE t_dels_tvpod.
SELECT vbeln INTO TABLE lt_dels_tvpod FROM likp
FOR ALL ENTRIES IN t_dels_tvpod
WHERE vbeln = t_dels_tvpod-vbeln
AND erdat IN s_erdat
AND bldat IN s_bldat
AND podat IN s_podat
AND ernam IN s_ernam
AND kunnr IN s_kunnr
AND vkorg IN s_vkorg
AND vstel IN s_vstel
AND lfart NOT IN r_del_types_exclude.
Waiting for quick response.
Best regards,
BDPBansidhar,
1) You need to add a check to make sure that internal table t_dels_tvpod (used in the FOR ALL ENTRIES clause) is not blank. If it is blank skip the SELECt statement.
2) Check the performance with and without clause 'AND lfart NOT IN r_del_types_exclude'. Sometimes NOT causes the select statement to not use the index. Instead of 'lfart NOT IN r_del_types_exclude' use 'lfart IN r_del_types_exclude' and build r_del_types_exclude by using r_del_types_exclude-sign = 'E' instead of 'I'.
3) Make sure that the table used in the FOR ALL ENTRIES clause has unique delivery numbers.
Try doing something like this.
TYPES: BEGIN OF ty_del_types_exclude,
sign(1) TYPE c,
option(2) TYPE c,
low TYPE likp-lfart,
high TYPE likp-lfart,
END OF ty_del_types_exclude.
DATA: w_del_types_exclude TYPE ty_del_types_exclude,
t_del_types_exclude TYPE TABLE OF ty_del_types_exclude,
t_dels_tvpod_tmp LIKE TABLE OF t_dels_tvpod .
IF NOT t_dels_tvpod[] IS INITIAL.
* Assuming that I would like to exclude delivery types 'LP' and 'LPP'
CLEAR w_del_types_exclude.
REFRESH t_del_types_exclude.
w_del_types_exclude-sign = 'E'.
w_del_types_exclude-option = 'EQ'.
w_del_types_exclude-low = 'LP'.
APPEND w_del_types_exclude TO t_del_types_exclude.
w_del_types_exclude-low = 'LPP'.
APPEND w_del_types_exclude TO t_del_types_exclude.
t_dels_tvpod_tmp[] = t_dels_tvpod[].
SORT t_dels_tvpod_tmp BY vbeln.
DELETE ADJACENT DUPLICATES FROM t_dels_tvpod_tmp
COMPARING
vbeln.
SELECT vbeln
FROM likp
INTO TABLE lt_dels_tvpod
FOR ALL ENTRIES IN t_dels_tvpod_tmp
WHERE vbeln EQ t_dels_tvpod_tmp-vbeln
AND erdat IN s_erdat
AND bldat IN s_bldat
AND podat IN s_podat
AND ernam IN s_ernam
AND kunnr IN s_kunnr
AND vkorg IN s_vkorg
AND vstel IN s_vstel
AND lfart IN t_del_types_exclude.
ENDIF. -
Modify a SELECT Query on ISU DB tables to improve performance
Hi Experts,
I have a SELECT query in a Program which is hitting 6 DB tables by means of 5 inner joins.
The outcome is that the program takes an exceptionally long time to execute, the SELECT statement being the main time consumer.
Need your expertise on how to split the Query without affecting functionality -
The Query :
SELECT fkkvkpgpart eablablbelnr eabladat eablistablart
FROM eabl
INNER JOIN eablg ON eablgablbelnr = eablablbelnr
INNER JOIN egerh ON egerhequnr = eablequnr
INNER JOIN eastl ON eastllogiknr = egerhlogiknr
INNER JOIN ever ON everanlage = eastlanlage
INNER JOIN fkkvkp ON fkkvkpvkont = evervkonto
INTO TABLE itab
WHERE eabl~adat GT [date which is (sy-datum - 3 years)]
Thanks in advance,
PDHi Prajakt
There are a couple of issues with the code provided by Aviansh:
1) Higher Memory consumption by extensive use of internal tables (possible shortdump TSV_NEW_PAGE_ALLOC_FAILED)
2) In many instances multiple SELECT ... FOR ALL ENTRIES... are not faster than a single JOIN statement
3) In the given code the timeslices tables are limited to records active of today, which is not the same as your select (taking into account that you select for the last three years you probably want historical meter/installation relationships as well*)
4) Use of sorted/hashed internal tables instead of standard ones could also improve the runtime (in case you stick to all the internal tables)
Did you create an index on EABL including columns MANDT, ADAT?
Did you check the execution plan of your original JOIN Select statement?
Yep
Jürgen
You should review your selection, because you probably want business partner that was linked to the meter reading at the time of ADAT, while your select doesn't take the specific Contract / Device Installation of the time of ADAT into account.
Example your meter reading is from 16.02.2010
Meter 00001 was in Installation 3000001 between 01.02.2010 and 23.08.2010
Meter 00002 was in Installation 3000001 between 24.08.2010 and 31.12.9999
Installation 3000001 was linked to Account 4000001 between 01.01.2010 and 23.01.2011
Installation 3000001 was linked to Account 4000002 between 24.01.2010 and 31.12.9999
This means with your select returns four lines and you probably want only one.
To achieve that you have to limit all timeslices to the date of EABL-ADAT (selects from EGERH, EASTL, EVER).
Update:
Coming back to point one and the memory consumption:
What are you planning to do with the output of the select statment?
Did you get a shortdump TSV_NEW_PAGE_ALLOC_FAILED with three years meter reading history?
Or did you never run on production like volumes yet?
Dependent on this you might want to redesign your program anyway.
Edited by: sattlerj on Jun 24, 2011 10:38 AM -
How to improve performance of select query when primary key is not referred
Hi,
There is a select query where we are unable to refrence primary key of the tables:
Since, the the below code is refrensing to vgbel and vgpos fields instead of vbeln and posnr..... the performance is very slow.
select vbeln posnr into (wa-vbeln1, wa-posnr1)
from lips
where ( pstyv ne 'ZBAT'
and pstyv ne 'ZNLN' )
and vgbel = i_vbap-vbeln
and vgpos = i_vbap-posnr.
endselect.
Please le t me know if you have some tips..hi,
I hope you are using the select statement inside a loop ...endloop get that outside to improve the performance ..
if not i_vbap[] is initial.
select vbeln posnr into table it_lips
from lips
for all entries in i_vbap
where ( pstyv ne 'ZBAT'
and pstyv ne 'ZNLN' )
and vgbel = i_vbap-vbeln
and vgpos = i_vbap-posnr.
endif. -
Using DB Links - Improving SELECT query performance
Hi there,
I am using dblink in the following query:
I would like to improve performance of the query by using hints as per described in the link: http://www.experts-exchange.com/Database/Oracle/9.x/Q_23640348.html. However, i am not sure how can i include this in my select query.
Details are:
Oracle - 9i Database Terminal Release .8
DB Link: TCPROD
Could someone please explain with an example how to use hints to get the query to select data on the remote database and then return the results to the target database?
Many Thanks.
SELECT ec.obid AS prObid,
ec.b2ProgramName AS program,
ec.projectName AS project,
ec.wbsID AS prNo,
ec.wbsName AS title,
ec.revision AS revision,
ec.superseded AS revisionSuperseded,
ec.lifeCycleState AS lifeCycleState,
ec.b2ChangeType AS type,
ec.b2Complexity AS subType,
ec.r1SsiCode AS ssi,
ec.b2disposition as disposition,
ec.wbsOriginator AS requestor,
ec.wbsAdministrator AS administrator,
ec.changepriority as priority,
ec.r1tsc as tsc,
ec.t1comments as tenixComments,
ec.b2securityclass as securityClassification,
ec.t1changesafety as safety,
ec.t1actionofficer as actionOfficer,
ec.t1changereason as changeReason,
ec.t1wbsextchangenumber as extChangeNo,
ec.creator as creator,
to_date(substr(ec.creationdate,
0,
instr(ec.creationdate, ':', 1, 3) - 1),
'YYYY/MM/DD-HH24:MI:SS') as creationdate,
to_date(ec.originatorassigndate, 'YYYY/MM/DD') as originatorassigndate,
zbd.description as description,
zbc.comments as comments
FROM (SELECT obid,
b2ProgramName,
projectName,
wbsID,
wbsName,
revision,
superseded,
lifeCycleState,
b2ChangeType,
b2Complexity,
r1SsiCode,
b2disposition,
wbsOriginator,
wbsAdministrator,
changepriority,
r1tsc,
t1comments,
b2securityclass,
t1changesafety,
t1actionofficer,
t1changereason,
t1wbsextchangenumber,
creator,
creationdate,
originatorassigndate
FROM awdbt1m4.cmPrRpIt@TCPROD
UNION
SELECT obid,
b2ProgramName,
projectName,
wbsID,
wbsName,
revision,
superseded,
lifeCycleState,
b2ChangeType,
b2Complexity,
r1SsiCode,
b2disposition,
wbsOriginator,
wbsAdministrator,
changepriority,
r1tsc,
t1comments,
b2securityclass,
t1changesafety,
t1actionofficer,
t1changereason,
t1wbsextchangenumber,
creator,
creationdate,
originatorassigndate
FROM mart1m4.cmPrRpIt@TCPROD
UNION
SELECT obid,
b2ProgramName,
projectName,
wbsID,
wbsName,
revision,
superseded,
lifeCycleState,
b2ChangeType,
b2Complexity,
r1SsiCode,
b2disposition,
wbsOriginator,
wbsAdministrator,
changepriority,
r1tsc,
t1comments,
b2securityclass,
t1changesafety,
t1actionofficer,
t1changereason,
t1wbsextchangenumber,
creator,
creationdate,
originatorassigndate
FROM mpsdt1m4.cmPrRpIt@TCPROD
UNION
SELECT obid,
b2ProgramName,
projectName,
wbsID,
wbsName,
revision,
superseded,
lifeCycleState,
b2ChangeType,
b2Complexity,
r1SsiCode,
b2disposition,
wbsOriginator,
wbsAdministrator,
changepriority,
r1tsc,
t1comments,
b2securityclass,
t1changesafety,
t1actionofficer,
t1changereason,
t1wbsextchangenumber,
creator,
creationdate,
originatorassigndate
FROM nondt1m4.cmPrRpIt@TCPROD
UNION
SELECT obid,
b2ProgramName,
projectName,
wbsID,
wbsName,
revision,
superseded,
lifeCycleState,
b2ChangeType,
b2Complexity,
r1SsiCode,
b2disposition,
wbsOriginator,
wbsAdministrator,
changepriority,
r1tsc,
t1comments,
b2securityclass,
t1changesafety,
t1actionofficer,
t1changereason,
t1wbsextchangenumber,
creator,
creationdate,
originatorassigndate
FROM rnast1m4.cmPrRpIt@TCPROD
UNION
SELECT obid,
b2ProgramName,
projectName,
wbsID,
wbsName,
revision,
superseded,
lifeCycleState,
b2ChangeType,
b2Complexity,
r1SsiCode,
b2disposition,
wbsOriginator,
wbsAdministrator,
changepriority,
r1tsc,
t1comments,
b2securityclass,
t1changesafety,
t1actionofficer,
t1changereason,
t1wbsextchangenumber,
creator,
creationdate,
originatorassigndate
FROM rnlht1m4.cmPrRpIt@TCPROD
UNION
SELECT obid,
b2ProgramName,
projectName,
wbsID,
wbsName,
revision,
superseded,
lifeCycleState,
b2ChangeType,
b2Complexity,
r1SsiCode,
b2disposition,
wbsOriginator,
wbsAdministrator,
changepriority,
r1tsc,
t1comments,
b2securityclass,
t1changesafety,
t1actionofficer,
t1changereason,
t1wbsextchangenumber,
creator,
creationdate,
originatorassigndate
FROM rnolt1m4.cmPrRpIt@TCPROD
UNION
SELECT obid,
b2ProgramName,
projectName,
wbsID,
wbsName,
revision,
superseded,
lifeCycleState,
b2ChangeType,
b2Complexity,
r1SsiCode,
b2disposition,
wbsOriginator,
wbsAdministrator,
changepriority,
r1tsc,
t1comments,
b2securityclass,
t1changesafety,
t1actionofficer,
t1changereason,
t1wbsextchangenumber,
creator,
creationdate,
originatorassigndate
FROM rzptt1m4.cmPrRpIt@TCPRODit's the tablename in the hint, not the column name
something like
SELECT ec.obid AS prObid,
ec.b2ProgramName AS program,
ec.projectName AS project,
ec.wbsID AS prNo,
ec.wbsName AS title,
ec.revision AS revision,
ec.superseded AS revisionSuperseded,
ec.lifeCycleState AS lifeCycleState,
ec.b2ChangeType AS type,
ec.b2Complexity AS subType,
ec.r1SsiCode AS ssi,
ec.b2disposition as disposition,
ec.wbsOriginator AS requestor,
ec.wbsAdministrator AS administrator,
ec.changepriority as priority,
ec.r1tsc as tsc,
ec.t1comments as tenixComments,
ec.b2securityclass as securityClassification,
ec.t1changesafety as safety,
ec.t1actionofficer as actionOfficer,
ec.t1changereason as changeReason,
ec.t1wbsextchangenumber as extChangeNo,
ec.creator as creator,
to_date(substr(ec.creationdate,
0,
instr(ec.creationdate, ':', 1, 3) - 1),
'YYYY/MM/DD-HH24:MI:SS') as creationdate,
to_date(ec.originatorassigndate, 'YYYY/MM/DD') as originatorassigndate
FROM (SELECT /*+ DRIVING_SITE(awdbt1m4.cmPrRpIt) */ obid,
b2ProgramName,
projectName,
wbsID,
wbsName,
revision,
superseded,
lifeCycleState,
b2ChangeType,
b2Complexity,
r1SsiCode,
b2disposition,
wbsOriginator,
wbsAdministrator,
changepriority,
r1tsc,
t1comments,
b2securityclass,
t1changesafety,
t1actionofficer,
t1changereason,
t1wbsextchangenumber,
creator,
creationdate,
originatorassigndate
FROM awdbt1m4.cmPrRpIt@TCPROD
UNION
SELECT /*+ DRIVING_SITE(mart1m4.cmPrRpIt) */ obid,
b2ProgramName,
projectName,
wbsID,
wbsName,
revision,
superseded,
lifeCycleState,
b2ChangeType,
b2Complexity,
r1SsiCode,
b2disposition,
wbsOriginator,
wbsAdministrator,
changepriority,
r1tsc,
t1comments,
b2securityclass,
t1changesafety,
t1actionofficer,
t1changereason,
t1wbsextchangenumber,
creator,
creationdate,
originatorassigndate
FROM mart1m4.cmPrRpIt@TCPROD
UNION
SELECT /*+ DRIVING_SITE(mpsdt1m4.cmPrRpIt) */ obid,
b2ProgramName,
projectName,
wbsID,
wbsName,
revision,
superseded,
lifeCycleState,
b2ChangeType,
b2Complexity,
r1SsiCode,
b2disposition,
wbsOriginator,
wbsAdministrator,
changepriority,
r1tsc,
t1comments,
b2securityclass,
t1changesafety,
t1actionofficer,
t1changereason,
t1wbsextchangenumber,
creator,
creationdate,
originatorassigndate
FROM mpsdt1m4.cmPrRpIt@TCPROD
UNION
SELECT /*+ DRIVING_SITE(nondt1m4.cmPrRpIt) */ obid,
b2ProgramName,
projectName,
wbsID,
wbsName,
revision,
superseded,
lifeCycleState,
b2ChangeType,
b2Complexity,
r1SsiCode,
b2disposition,
wbsOriginator,
wbsAdministrator,
changepriority,
r1tsc,
t1comments,
b2securityclass,
t1changesafety,
t1actionofficer,
t1changereason,
t1wbsextchangenumber,
creator,
creationdate,
originatorassigndate
FROM nondt1m4.cmPrRpIt@TCPROD
UNION
SELECT /*+ DRIVING_SITE(rnast1m4.cmPrRpIt) */ obid,
b2ProgramName,
projectName,
wbsID,
wbsName,
revision,
superseded,
lifeCycleState,
b2ChangeType,
b2Complexity,
r1SsiCode,
b2disposition,
wbsOriginator,
wbsAdministrator,
changepriority,
r1tsc,
t1comments,
b2securityclass,
t1changesafety,
t1actionofficer,
t1changereason,
t1wbsextchangenumber,
creator,
creationdate,
originatorassigndate
FROM rnast1m4.cmPrRpIt@TCPROD
UNION
SELECT /*+ DRIVING_SITE(rnlht1m4.cmPrRpIt) */ obid,
b2ProgramName,
projectName,
wbsID,
wbsName,
revision,
superseded,
lifeCycleState,
b2ChangeType,
b2Complexity,
r1SsiCode,
b2disposition,
wbsOriginator,
wbsAdministrator,
changepriority,
r1tsc,
t1comments,
b2securityclass,
t1changesafety,
t1actionofficer,
t1changereason,
t1wbsextchangenumber,
creator,
creationdate,
originatorassigndate
FROM rnlht1m4.cmPrRpIt@TCPROD
UNION
SELECT /*+ DRIVING_SITE(rnolt1m4.cmPrRpIt) */ obid,
b2ProgramName,
projectName,
wbsID,
wbsName,
revision,
superseded,
lifeCycleState,
b2ChangeType,
b2Complexity,
r1SsiCode,
b2disposition,
wbsOriginator,
wbsAdministrator,
changepriority,
r1tsc,
t1comments,
b2securityclass,
t1changesafety,
t1actionofficer,
t1changereason,
t1wbsextchangenumber,
creator,
creationdate,
originatorassigndate
FROM rnolt1m4.cmPrRpIt@TCPROD
UNION
SELECT /*+ DRIVING_SITE(rzptt1m4.cmPrRpIt) */ obid,
b2ProgramName,
projectName,
wbsID,
wbsName,
revision,
superseded,
lifeCycleState,
b2ChangeType,
b2Complexity,
r1SsiCode,
b2disposition,
wbsOriginator,
wbsAdministrator,
changepriority,
r1tsc,
t1comments,
b2securityclass,
t1changesafety,
t1actionofficer,
t1changereason,
t1wbsextchangenumber,
creator,
creationdate,
originatorassigndate
FROM rzptt1m4.cmPrRpIt@TCPROD) ec(not tested, of course) -
Needed help to improve the performance of a select query?
Hi,
I have been preparing a report which involves data to be fetched from 4 to 5 different tables and calculation has to performed on some columns also,
i planned to write a single cursor to populate 1 temp table.i have used INLINE VIEW,EXISTS more frequently in the select query..please go through the query and suggest me a better way to restructure the query.
cursor c_acc_pickup_incr(p_branch_code varchar2, p_applDate date, p_st_dt date, p_ed_dt date) is
select sca.branch_code "BRANCH",
sca.cust_ac_no "ACCOUNT",
to_char(p_applDate, 'YYYYMM') "YEARMONTH",
sca.ccy "CURRENCY",
sca.account_class "PRODUCT",
sca.cust_no "CUSTOMER",
sca.ac_desc "DESCRIPTION",
null "LOW_BAL",
null "HIGH_BAL",
null "AVG_CR_BAL",
null "AVG_DR_BAL",
null "CR_DAYS",
null "DR_DAYS",
--null "CR_TURNOVER",
--null "DR_TURNOVER",
null "DR_OD_DAYS",
(select sum(gf.limit_amount * (scal.linkage_percentage / 100)) +
(case when (p_applDate >= sca.tod_limit_start_date and
p_applDate <= nvl(sca.tod_limit_end_date, p_applDate)) then
sca.tod_limit else 0 end) dd
from getm_facility gf, sttm_cust_account_linkages scal
where gf.line_code || gf.line_serial = scal.linked_ref_no
and cust_ac_no = sca.cust_ac_no) "OD_LIMIT",
--sc.credit_rating "CR_GRADE",
null "AVG_NET_BAL",
null "UNAUTH_OD_AMT",
sca.acy_blocked_amount "AMT_BLOCKED",
(select sum(amt)
from ictb_entries_history ieh
where ieh.acc = sca.cust_ac_no
and ieh.brn = sca.branch_code
and ieh.drcr = 'D'
and ieh.liqn = 'Y'
and ieh.entry_passed = 'Y'
and ieh.ent_dt between p_st_dt and p_ed_dt
and exists (
select * from ictm_pr_int ipi, ictm_rule_frm irf
where ipi.product_code = ieh.prod
and ipi.rule = irf.rule_id
and irf.book_flag = 'B')) "DR_INTEREST",
(select sum(amt)
from ictb_entries_history ieh
where ieh.acc = sca.cust_ac_no
and ieh.brn = sca.branch_code
and ieh.drcr = 'C'
and ieh.liqn = 'Y'
and ieh.entry_passed = 'Y'
and ieh.ent_dt between p_st_dt and p_ed_dt
and exists (
select * from ictm_pr_int ipi, ictm_rule_frm irf
where ipi.product_code = ieh.prod
and ipi.rule = irf.rule_id
and irf.book_flag = 'B')) "CR_INTEREST",
(select sum(amt) from ictb_entries_history ieh
where ieh.brn = sca.branch_code
and ieh.acc = sca.cust_ac_no
and ieh.ent_dt between p_st_dt and p_ed_dt
and exists (
select product_code
from ictm_product_definition ipd
where ipd.product_code = ieh.prod
and ipd.product_type = 'C')) "FEE_INCOME",
sca.record_stat "ACC_STATUS",
case when (trunc(sca.ac_open_date,'MM') = trunc(p_applDate,'MM')
and not exists (select 1
from ictm_tdpayin_details itd
where itd.multimode_payopt = 'Y'
and itd.brn = sca.branch_code
and itd.acc = sca.cust_ac_no
and itd.multimode_offset_brn is not null
and itd.multimode_tdoffset_acc is not null))
then 1 else 0 end "NEW_ACC_FOR_THE_MONTH",
case when (trunc(sca.ac_open_date,'MM') = trunc(p_applDate,'MM')
and trunc(sc.cif_creation_date,'MM') = trunc(p_applDate,'MM')
and not exists (select 1
from ictm_tdpayin_details itd
where itd.multimode_payopt = 'Y'
and itd.brn = sca.branch_code
and itd.acc = sca.cust_ac_no
and itd.multimode_offset_brn is not null
and itd.multimode_tdoffset_acc is not null))
then 1 else 0 end "NEW_ACC_FOR_NEW_CUST",
(select 1 from dual
where exists (select 1 from ictm_td_closure_renew itcr
where itcr.brn = sca.branch_code
and itcr.acc = sca.cust_ac_no
and itcr.renewal_date = sysdate)
or exists (select 1 from ictm_tdpayin_details itd
where itd.multimode_payopt = 'Y'
and itd.brn = sca.branch_code
and itd.acc = sca.cust_ac_no
and itd.multimode_offset_brn is not null
and itd.multimode_tdoffset_acc is not null)) "RENEWED_OR_ROLLOVER",
(select maturity_date from ictm_acc ia
where ia.brn = sca.branch_code
and ia.acc = sca.cust_ac_no) "MATURITY_DATE",
sca.ac_stat_no_dr "DR_DISALLOWED",
sca.ac_stat_no_cr "CR_DISALLOWED",
sca.ac_stat_block "BLOCKED_ACC", Not Reqd
sca.ac_stat_dormant "DORMANT_ACC",
sca.ac_stat_stop_pay "STOP_PAY_ACC", --New
sca.ac_stat_frozen "FROZEN_ACC",
sca.ac_open_date "ACC_OPENING_DT",
sca.address1 "ADD_LINE_1",
sca.address2 "ADD_LINE_2",
sca.address3 "ADD_LINE_3",
sca.address4 "ADD_LINE_4",
sca.joint_ac_indicator "JOINT_ACC",
sca.acy_avl_bal "CR_BAL",
0 "DR_BAL",
0 "CR_BAL_LCY", t
0 "DR_BAL_LCY",
null "YTD_CR_MOVEMENT",
null "YTD_DR_MOVEMENT",
null "YTD_CR_MOVEMENT_LCY",
null "YTD_DR_MOVEMENT_LCY",
null "MTD_CR_MOVEMENT",
null "MTD_DR_MOVEMENT",
null "MTD_CR_MOVEMENT_LCY",
null "MTD_DR_MOVEMENT_LCY",
'N' "BRANCH_TRFR", --New
sca.provision_amount "PROVISION_AMT",
sca.account_type "ACCOUNT_TYPE",
nvl(sca.tod_limit, 0) "TOD_LIMIT",
nvl(sca.sublimit, 0) "SUB_LIMIT",
nvl(sca.tod_limit_start_date, global.min_date) "TOD_START_DATE",
nvl(sca.tod_limit_end_date, global.max_date) "TOD_END_DATE"
from sttm_cust_account sca, sttm_customer sc
where sca.branch_code = p_branch_code
and sca.cust_no = sc.customer_no
and ( exists (select 1 from actb_daily_log adl
where adl.ac_no = sca.cust_ac_no
and adl.ac_branch = sca.branch_code
and adl.trn_dt = p_applDate
and adl.auth_stat = 'A')
or exists (select 1 from catm_amount_blocks cab
where cab.account = sca.cust_ac_no
and cab.branch = sca.branch_code
and cab.effective_date = p_applDate
and cab.auth_stat = 'A')
or exists (select 1 from ictm_td_closure_renew itcr
where itcr.acc = sca.cust_ac_no
and itcr.brn = sca.branch_code
and itcr.renewal_date = p_applDate)
or exists (select 1 from sttm_ac_stat_change sasc
where sasc.cust_ac_no = sca.cust_ac_no
and sasc.branch_code = sca.branch_code
and sasc.status_change_date = p_applDate
and sasc.auth_stat = 'A')
or exists (select 1 from cstb_acc_brn_trfr_log cabtl
where cabtl.branch_code = sca.branch_code
and cabtl.cust_ac_no = sca.cust_ac_no
and cabtl.process_status = 'S'
and cabtl.process_date = p_applDate)
or exists (select 1 from sttbs_provision_history sph
where sph.branch_code = sca.branch_code
and sph.cust_ac_no = sca.cust_ac_no
and sph.esn_date = p_applDate)
or exists (select 1 from sttms_cust_account_dormancy scad
where scad.branch_code = sca.branch_code
and scad.cust_ac_no = sca.cust_ac_no
and scad.dormancy_start_dt = p_applDate)
or sca.maker_dt_stamp = p_applDate
or sca.status_since = p_applDate
l_tb_acc_det ty_tb_acc_det_int;
l_brnrec cvpks_utils.rec_brnlcy;
l_acbr_lcy sttms_branch.branch_lcy%type;
l_lcy_amount actbs_daily_log.lcy_amount%type;
l_xrate number;
l_dt_rec sttm_dates%rowtype;
l_acc_rec sttm_cust_account%rowtype;
l_acc_stat_row ty_r_acc_stat;
Edited by: user13710379 on Jan 7, 2012 12:18 AMI see it more like shown below (possibly with no inline selects
Try to get rid of the remaining inline selects ( left as an exercise ;) )
and rewrite traditional joins as ansi joins as problems might arise using mixed syntax as I have to leave so I don't have time to complete the query
select sca.branch_code "BRANCH",
sca.cust_ac_no "ACCOUNT",
to_char(p_applDate, 'YYYYMM') "YEARMONTH",
sca.ccy "CURRENCY",
sca.account_class "PRODUCT",
sca.cust_no "CUSTOMER",
sca.ac_desc "DESCRIPTION",
null "LOW_BAL",
null "HIGH_BAL",
null "AVG_CR_BAL",
null "AVG_DR_BAL",
null "CR_DAYS",
null "DR_DAYS",
-- null "CR_TURNOVER",
-- null "DR_TURNOVER",
null "DR_OD_DAYS",
w.dd "OD_LIMIT",
-- sc.credit_rating "CR_GRADE",
null "AVG_NET_BAL",
null "UNAUTH_OD_AMT",
sca.acy_blocked_amount "AMT_BLOCKED",
x.dr_int "DR_INTEREST",
x.cr_int "CR_INTEREST",
y.fee_amt "FEE_INCOME",
sca.record_stat "ACC_STATUS",
case when trunc(sca.ac_open_date,'MM') = trunc(p_applDate,'MM')
and not exists(select 1
from ictm_tdpayin_details itd
where itd.multimode_payopt = 'Y'
and itd.brn = sca.branch_code
and itd.acc = sca.cust_ac_no
and itd.multimode_offset_brn is not null
and itd.multimode_tdoffset_acc is not null
then 1
else 0
end "NEW_ACC_FOR_THE_MONTH",
case when (trunc(sca.ac_open_date,'MM') = trunc(p_applDate,'MM')
and trunc(sc.cif_creation_date,'MM') = trunc(p_applDate,'MM')
and not exists(select 1
from ictm_tdpayin_details itd
where itd.multimode_payopt = 'Y'
and itd.brn = sca.branch_code
and itd.acc = sca.cust_ac_no
and itd.multimode_offset_brn is not null
and itd.multimode_tdoffset_acc is not null
then 1
else 0
end "NEW_ACC_FOR_NEW_CUST",
(select 1 from dual
where exists(select 1
from ictm_td_closure_renew itcr
where itcr.brn = sca.branch_code
and itcr.acc = sca.cust_ac_no
and itcr.renewal_date = sysdate
or exists(select 1
from ictm_tdpayin_details itd
where itd.multimode_payopt = 'Y'
and itd.brn = sca.branch_code
and itd.acc = sca.cust_ac_no
and itd.multimode_offset_brn is not null
and itd.multimode_tdoffset_acc is not null
) "RENEWED_OR_ROLLOVER",
m.maturity_date "MATURITY_DATE",
sca.ac_stat_no_dr "DR_DISALLOWED",
sca.ac_stat_no_cr "CR_DISALLOWED",
-- sca.ac_stat_block "BLOCKED_ACC", --Not Reqd
sca.ac_stat_dormant "DORMANT_ACC",
sca.ac_stat_stop_pay "STOP_PAY_ACC", --New
sca.ac_stat_frozen "FROZEN_ACC",
sca.ac_open_date "ACC_OPENING_DT",
sca.address1 "ADD_LINE_1",
sca.address2 "ADD_LINE_2",
sca.address3 "ADD_LINE_3",
sca.address4 "ADD_LINE_4",
sca.joint_ac_indicator "JOINT_ACC",
sca.acy_avl_bal "CR_BAL",
0 "DR_BAL",
0 "CR_BAL_LCY", t
0 "DR_BAL_LCY",
null "YTD_CR_MOVEMENT",
null "YTD_DR_MOVEMENT",
null "YTD_CR_MOVEMENT_LCY",
null "YTD_DR_MOVEMENT_LCY",
null "MTD_CR_MOVEMENT",
null "MTD_DR_MOVEMENT",
null "MTD_CR_MOVEMENT_LCY",
null "MTD_DR_MOVEMENT_LCY",
'N' "BRANCH_TRFR", --New
sca.provision_amount "PROVISION_AMT",
sca.account_type "ACCOUNT_TYPE",
nvl(sca.tod_limit, 0) "TOD_LIMIT",
nvl(sca.sublimit, 0) "SUB_LIMIT",
nvl(sca.tod_limit_start_date, global.min_date) "TOD_START_DATE",
nvl(sca.tod_limit_end_date, global.max_date) "TOD_END_DATE"
from sttm_cust_account sca,
sttm_customer sc,
(select sca.cust_ac_no
sum(gf.limit_amount * (scal.linkage_percentage / 100)) +
case when p_applDate >= sca.tod_limit_start_date
and p_applDate <= nvl(sca.tod_limit_end_date, p_applDate)
then sca.tod_limit else 0
end
) dd
from sttm_cust_account sca
getm_facility gf,
sttm_cust_account_linkages scal
where gf.line_code || gf.line_serial = scal.linked_ref_no
and cust_ac_no = sca.cust_ac_no
group by sca.cust_ac_no
) w,
(select acc,
brn,
sum(decode(drcr,'D',amt)) dr_int,
sum(decode(drcr,'C',amt)) cr_int
from ictb_entries_history ieh
where ent_dt between p_st_dt and p_ed_dt
and drcr in ('C','D')
and liqn = 'Y'
and entry_passed = 'Y'
and exists(select null
from ictm_pr_int ipi,
ictm_rule_frm irf
where ipi.rule = irf.rule_id
and ipi.product_code = ieh.prod
and irf.book_flag = 'B'
group by acc,brn
) x,
(select acc,
brn,
sum(amt) fee_amt
from ictb_entries_history ieh
where ieh.ent_dt between p_st_dt and p_ed_dt
and exists(select product_code
from ictm_product_definition ipd
where ipd.product_code = ieh.prod
and ipd.product_type = 'C'
group by acc,brn
) y,
ictm_acc m,
(select sca.cust_ac_no,
sca.branch_code
coalesce(nvl2(coalesce(t1.ac_no,t1.ac_branch),'exists',null),
nvl2(coalesce(t2.account,t2.account),'exists',null),
nvl2(coalesce(t3.acc,t3.brn),'exists',null),
nvl2(coalesce(t4.cust_ac_no,t4.branch_code),'exists',null),
nvl2(coalesce(t5.cust_ac_no,t5.branch_code),'exists',null),
nvl2(coalesce(t6.cust_ac_no,t6.branch_code),'exists',null),
nvl2(coalesce(t7.cust_ac_no,t7.branch_code),'exists',null),
decode(sca.maker_dt_stamp,p_applDate,'exists'),
decode(sca.status_since,p_applDate,'exists')
) existence
from sttm_cust_account sca
left outer join
(select ac_no,ac_branch
from actb_daily_log
where trn_dt = p_applDate
and auth_stat = 'A'
) t1
on (sca.cust_ac_no = t1.ac_no
and sca.branch_code = t1.ac_branch
left outer join
(select account,account
from catm_amount_blocks
where effective_date = p_applDate
and auth_stat = 'A'
) t2
on (sca.cust_ac_no = t2.account
and sca.branch_code = t2.branch
left outer join
(select acc,brn
from ictm_td_closure_renew itcr
where renewal_date = p_applDate
) t3
on (sca.cust_ac_no = t3.acc
and sca.branch_code = t3.brn
left outer join
(select cust_ac_no,branch_code
from sttm_ac_stat_change
where status_change_date = p_applDate
and auth_stat = 'A'
) t4
on (sca.cust_ac_no = t4.cust_ac_no
and sca.branch_code = t4.branch_code
left outer join
(select cust_ac_no,branch_code
from cstb_acc_brn_trfr_log
where process_date = p_applDate
and process_status = 'S'
) t5
on (sca.cust_ac_no = t5.cust_ac_no
and sca.branch_code = t5.branch_code
left outer join
(select cust_ac_no,branch_code
from sttbs_provision_history
where esn_date = p_applDate
) t6
on (sca.cust_ac_no = t6.cust_ac_no
and sca.branch_code = t6.branch_code
left outer join
(select cust_ac_no,branch_code
from sttms_cust_account_dormancy
where dormancy_start_dt = p_applDate
) t7
on (sca.cust_ac_no = t7.cust_ac_no
and sca.branch_code = t7.branch_code
) z
where sca.branch_code = p_branch_code
and sca.cust_no = sc.customer_no
and sca.cust_ac_no = w.cust_ac_no
and sca.cust_ac_no = x.acc
and sca.branch_code = x.brn
and sca.cust_ac_no = y.acc
and sca.branch_code = y.brn
and sca.cust_ac_no = m.acc
and sca.branch_code = m.brn
and sca.cust_ac_no = z.sca.cust_ac_no
and sca.branch_code = z.branch_code
and z.existence is not nullRegards
Etbin -
How to optimize the select query that is executed in a cursor for loop?
Hi Friends,
I have executed the code below and clocked the times for every line of the code using DBMS_PROFILER.
CREATE OR REPLACE PROCEDURE TEST
AS
p_file_id NUMBER := 151;
v_shipper_ind ah_item.shipper_ind%TYPE;
v_sales_reserve_ind ah_item.special_sales_reserve_ind%TYPE;
v_location_indicator ah_item.exe_location_ind%TYPE;
CURSOR activity_c
IS
SELECT *
FROM ah_activity_internal
WHERE status_id = 30
AND file_id = p_file_id;
BEGIN
DBMS_PROFILER.start_profiler ('TEST');
FOR rec IN activity_c
LOOP
SELECT DISTINCT shipper_ind, special_sales_reserve_ind, exe_location_ind
INTO v_shipper_ind, v_sales_reserve_ind, v_location_indicator
FROM ah_item --464000 rows in this table
WHERE item_id_edw IN (
SELECT item_id_edw
FROM ah_item_xref --700000 rows in this table
WHERE item_code_cust = rec.item_code_cust
AND facility_num IN (
SELECT facility_code
FROM ah_chain_div_facility --17 rows in this table
WHERE chain_id = ah_internal_data_pkg.get_chain_id (p_file_id)
AND div_id = (SELECT div_id
FROM ah_div --8 rows in this table
WHERE division = rec.division)));
END LOOP;
DBMS_PROFILER.stop_profiler;
EXCEPTION
WHEN NO_DATA_FOUND
THEN
NULL;
WHEN TOO_MANY_ROWS
THEN
NULL;
END TEST;The SELECT query inside the cursor FOR LOOP took 773 seconds.
I have tried using BULK COLLECT instead of cursor for loop but it did not help.
When I took out the select query separately and executed with a sample value then it gave the results in a flash of second.
All the tables have primary key indexes.
Any ideas what can be done to make this code perform better?
Thanks,
Raj.As suggested I'd try merging the queries into a single SQL. You could also rewrite your IN clauses as JOINs and see if that helps, e.g.
SELECT DISTINCT ai.shipper_ind, ai.special_sales_reserve_ind, ai.exe_location_ind
INTO v_shipper_ind, v_sales_reserve_ind, v_location_indicator
FROM ah_item ai, ah_item_xref aix, ah_chain_div_facility acdf, ah_div ad
WHERE ai.item_id_edw = aix.item_id_edw
AND aix.item_code_cust = rec.item_code_cust
AND aix.facility_num = acdf.facility_code
AND acdf.chain_id = ah_internal_data_pkg.get_chain_id (p_file_id)
AND acdf.div_id = ad.div_id
AND ad.division = rec.division;ALSO: You are calling ah_internal_data_pkg.get_chain_id (p_file_id) every time. Why not do it outside the loop and just use a variable in the inner query? That will prevent context switching and improve speed.
Edited by: Dave Hemming on Dec 3, 2008 9:34 AM -
HI All experts,<br><br>
i have a program in which i have a select query <pre>
SELECT b~vertrag
a~anlage
a~operand
a~saison
a~ab
a~ablfdnr
a~bis
a~wert1
FROM ettifn AS a INNER JOIN ever AS b
ON aanlage = banlage
INTO TABLE li_captran PACKAGE SIZE p_pack
WHERE a~anlage IN s_anlage
AND a~ab <= l_date
AND a~bis >= l_date
AND a~operand IN ('ID-CAPLOAD','ID-OBLT')
AND b~einzdat <= l_date
AND b~auszdat >= l_date.</pre><br><br>
this query is taking a lot of time in running and we are facing lot of performance related issues
<br><br>
kindly guide me how i can break this into som simpler query
<br><br>
ettifn has 10 million records
ever has around 5 million records .
<br><br>
also please tell me how much data i can store into an internal table
can i store 5 million records in an internal table ??????
is it good in terms of performance of a program if not please tell me what is the better way of doing dat .
<br><br>
also tell me how secondry indexes help in improving performance of select query
is it good to maintain number of secondry indexes in a standard table or it decreases performance of a standard table .
<br><br>
<pre>
TYPES: BEGIN OF ty_eanlh,
anlage LIKE eanlh-anlage,
bis LIKE eanlh-bis,
tariftyp LIKE eanlh-tariftyp,
END OF ty_eanlh.
DATA: l_date TYPE datum.
DATA: li_eanlh TYPE HASHED TABLE OF ty_eanlh WITH UNIQUE KEY anlage
bis
WITH HEADER LINE.
DATA: li_captran TYPE HASHED TABLE OF ty_captran WITH UNIQUE KEY vertrag
anlage
operand
saison
ab
ablfdnr
WITH HEADER LINE.
DATA: l_tariftyp LIKE eanlh-tariftyp.
l_date = p_date + 6.
l_date = p_date + 5. "As per Terry's Request 1/23/2009
CHECK NOT p_cap IS INITIAL.
Get Operand Values
SELECT b~vertrag
a~anlage
a~operand
a~saison
a~ab
a~ablfdnr
a~bis
a~wert1
FROM ettifn AS a INNER JOIN ever AS b
ON aanlage = banlage
INTO TABLE li_captran PACKAGE SIZE p_pack
WHERE a~anlage IN s_anlage
AND a~ab <= l_date
AND a~bis >= l_date
AND a~operand IN ('ID-CAPLOAD','ID-OBLT')
AND b~einzdat <= l_date
AND b~auszdat >= l_date.
IF sy-subrc EQ 0 AND NOT li_captran[] IS INITIAL.
Get Rate Category
SELECT anlage
bis
tariftyp
FROM eanlh INTO CORRESPONDING FIELDS OF TABLE li_eanlh
FOR ALL ENTRIES IN li_captran
WHERE anlage = li_captran-anlage
AND ab <= l_date
AND bis >= l_date.
Get POD ID
SELECT a~anlage
a~int_ui
a~dateto
a~timeto
b~ext_ui
INTO TABLE i_pod
FROM euiinstln AS a INNER JOIN euitrans AS b
ON aint_ui = bint_ui
FOR ALL ENTRIES IN li_captran
WHERE a~anlage = li_captran-anlage.
IF sy-subrc EQ 0 AND NOT i_pod[] IS INITIAL.
SORT i_pod BY anlage.
SELECT vertrag
int_ui
serviceid
FROM eservice
INTO TABLE i_servicect
FOR ALL ENTRIES IN i_pod
WHERE int_ui = i_pod-int_ui
AND service_start <= l_date
AND service_end >= l_date
AND service = 'ESUP'.
ENDIF.
LOOP AT li_captran.
i_ct-anlage = li_captran-anlage.
READ TABLE li_eanlh WITH KEY anlage = li_captran-anlage.
IF sy-subrc EQ 0.
i_ct-tariftyp = li_eanlh-tariftyp.
READ TABLE i_rate WITH KEY tariftyp = i_ct-tariftyp.
IF sy-subrc NE 0.
CONTINUE.
ENDIF.
ELSE.
i_ct-tariftyp = '0000'.
ENDIF.
READ TABLE i_pod WITH KEY anlage = li_captran-anlage.
IF sy-subrc EQ 0.
READ TABLE i_servicect WITH KEY int_ui = i_pod-int_ui.
IF sy-subrc EQ 0.
i_ct-serviceid = i_servicect-serviceid.
ENDIF.
ENDIF.
IF li_captran-operand = 'ID-CAPLOAD'.
i_ct-cap = li_captran-wert1.
ELSEIF li_captran-operand = 'ID-OBLT'.
i_ct-tran = li_captran-wert1.
ENDIF.
COLLECT i_ct.
CLEAR: li_captran, i_ct.
ENDLOOP.
ENDIF.
ENDSELECT.</pre><br><br>
this code is taking a lot of time to execute and decreasing the system performance .<br><br>
please guide me how can i increase the performance of this code .<br><br>
Thanks in advance<br><br><br><br>
Edited by: Matt on Oct 11, 2009 9:45 PMHi matt ,
no i am not thru bcoz of formatting i just trying to remove it and i just marked it answered so that it will not come in my unanswered because unanswered questions are limited.
also could you please help me in this question .
now how can i change this question to unanswerd .
plz guide me
Thanks in advance -
TIME-OUT error in BSAK select query(Progress Indicator is also used)
Hi,
In my report program one select query is there on BSAK table, which is as follows --
SELECT BUKRS
BELNR
GJAHR
SHKZG
BSCHL
UMSKZ
LIFNR
EBELN
EBELP
WRBTR
DMBTR
XZAHL
REBZG
AUGBL
BLART
AUFNR
AUGDT
BUZEI FROM BSAK
INTO TABLE IT_BSAK
FOR ALL ENTRIES IN IT_BKPF1
WHERE BUKRS = IT_BKPF1-BUKRS
AND AUGDT = IT_BKPF1-BUDAT
AND AUGBL = IT_BKPF1-BELNR
AND BSCHL IN ('31' , '29', '26', '39', '25').
I used Progress Indicator befor running this query and after this query also. But still It's giving me TIME-OUT error in this select query only.
I run the same query for 10 records in IT_BKPF1 table, it runs perfectly. But when I run it for 1000 records it giving dump.
And in actual bussiness my records are always more than 100 only.
I also check the indexing. It having secondary indexing on this BUKRS, AUGDT, AUGBL fields. Then also it's giving error.
so, How can I solve this dump..?? What could be the reason..??
Thanks in advance...!!
Regards,
Poonam.Hi Poonam Patil,
Try to provide BELNR and GJAHR in where condition...
BKPF-DBBLG ==> BSAK-BELNR
Also check
BKPF-BLDAT ==> BSAK-AUGDT
Check out above relation...
If data is there in these fields of the table and both are matching then you can pass it and as they are in primary key of BSAK it will improve the performance...
Hope it will solve your problem..
Thanks & Regards
ilesh 24x7
ilesh Nandaniya -
SELECT QUERY BASED ON SECONDARY INDEX
Hi all,
CAN ANYONE TELL ME HOW TO WRITE SELECT QUERY BASED ON SECONDARY INDEX.
IN WHAT WAY DOES IT IMPROVE PERFORMANCE.
i KNOW WHEN CREATING SECONDARY INDEX I NEED TO GIVE AN INDEX NO -iT SHOULD BE ANY NUMBER RIGHT?
I HAVE TO LIST ALL PRIMARY KEYS FIRST AND THEN THE FIELD FOR WHICH I AM CREATING SECONDARY INDEX RIGHT?
LETS SAY I HAVE 2 PRIMARY KEYS AND I WANT TO CREATE SEONDARY INDEX FOR 2 FIELDS THEN
I NEED TO CREATE A SEPERTE SECONDARY INDEX FOR EACH ONE OF THOSE FIELDS OR ONE SHOULD BE ENOUGH
pLS LET ME KNOW IF IAM WRONGHI,
If you cannot use the primary index to determine the result set because, for example, none of the primary index fields occur in the WHERE or HAVINGclauses, the system searches through the entire table (full table scan). For this case, you can create secondary indexes, which can restrict the number of table entries searched to form the result set.
You create secondary indexes using the ABAP Dictionary. There you can create its columns and define it as UNIQUE. However, you should not create secondary indexes to cover all possible combinations of fields.
Only create one if you select data by fields that are not contained in another index, and the performance is very poor. Furthermore, you should only create secondary indexes for database tables from which you mainly read, since indexes have to be updated each time the database table is changed. <b>As a rule, secondary indexes should not contain more than four fields</b>, <b>and you should not have more than five indexes for a single database table</b>.
<b>What to Keep in Mind for Secondary Indexes:</b>
http://help.sap.com/saphelp_nw04s/helpdata/en/cf/21eb2d446011d189700000e8322d00/content.htm
http://www.sap-img.com/abap/quick-note-on-design-of-secondary-database-indexes-and-logical-databases.htm
Regards
Sudheer -
Select query in case of Multiple line items
Hi Gurus ,
I've a doubt in general SQL select query. I want to know , if suppose I've an internal table - itab . I've fetched G/L Account numbers 1st, based on the input selections . Next , I want to loop on those G/L accounts. However, if the G/L account has multiple line items, then I personally use this select query -- >
loop at itab.
select <field> from <table> appending corresponding fields of <itab1> where hkont eq itab-hkont.
endloop.
Now, the execution time for this query is longer than expected. The biggest problem here is, i've to sum up the totals as well. So totaling is an added load. I want to reduce the execution time of this. Kindly suggest me some good method in case u have any.
I've pasted the code which I've written , for u ppl to understand--
SELECT DISTINCT HKONT BELNR
FROM BSIS
INTO CORRESPONDING FIELDS OF TABLE OTAB
WHERE HKONT IN S_RACCT
* AND PRCTR IN P_PRCTR
AND MONAT IN S_POPER
AND BUKRS EQ P_BUKRS
AND GJAHR EQ P_GJAHR
AND PRCTR IN S_PRCTR.
***The code below takes a lot of time to execute.***
LOOP AT OTAB .
SELECT DMBTR HKONT
FROM BSIS APPENDING CORRESPONDING FIELDS OF TABLE CREDITS
WHERE HKONT EQ OTAB-HKONT
AND BELNR EQ OTAB-BELNR
AND MONAT IN S_POPER
AND BUKRS EQ P_BUKRS
AND GJAHR EQ P_GJAHR
AND PRCTR IN S_PRCTR
AND SHKZG EQ 'H'.
COLLECT CREDITS.
ENDLOOP.Hi,
First of all try to avoid doing select into corresponding fields. THis would improve the performance of the program.
Try to do a single fetch from the BSIS table . fetch the hkont, belnr, dmbtr fields in to a master internal table. Manipulate and play with the data as required. Don't hit the data base table more than once (unless it is required) . This would improve the performance of your code.
Try to code this way.
types: begin of ty_bsis,
hkont type hkont,
belnr type belnr_d,
dmbtr type dmbtr,
end of ty_bsis.
data: it_bsis type standard table of ty_bsis,
wa_bsis type ty_bsis,
select hkont belnr dmbtr
from bsis
into table it_bsis
WHERE HKONT IN S_RACCT
AND PRCTR IN P_PRCTR
AND MONAT IN S_POPER
AND BUKRS EQ P_BUKRS
AND GJAHR EQ P_GJAHR
AND PRCTR IN S_PRCTR.
Using the data availabe in the it_bsis, you can manipulate as required.
Hope this would be helpful
Regards
Ramesh Sundaram
Maybe you are looking for
-
CS4 Text around a bottom curve but text straight up and down?
I have to change the names on bottles that were shot and so I am trying to type the names. 1st I tried Photoshop and could not get the 3D to work and could not get the arc or the warp to work correctly. So I went over to Illustrator. I finally got it
-
IDOC to Flat File and XML ( Need both the Output)
Hi , My Scenario is IDOC to File . I need o/p in 2 format . ie, 1) Flat File 2 ) XML file I have a plan to go with Interface determination , 2 IM and what shd be the condition ? or Else 2 Receiver detmination but condition ? How to slove this ?
-
Just bought this MacBook Pro and already having a problem with iMovie HD. I have a Sony HDV-HC3 camcorder and had no problems importing the footage but when I go to edit and use titles it shuts down a soon as I click add. iMovie completely shuts down
-
Everything is fine with my iPhone, the 3G turns back on as soon as I reboot my phone and I can turn it off and on no problem as long as its not off for more than about 24 hours Anyone else getting the same problem?
-
Sql Dev Third party migration issue
i have installed 11g on window 7 using Sql Developer of 11g i am migrating the sql server 2008 database to Oracle 11g Migration> Quick Migration selecting Source Connection >Target Connection> Repository AND verifyit generate Errors: Locating source