Regarding "select query" on performance basis..
Hi Experts,
I am creating an ALV report,so that i have to collect all data in 1 internal table.
I have to collect data from 3 standard tables i.e.T157E,MSEG,MKPF.
For all entries of T157E, i have to fetch data from MSEG.
But in where condition i have to give POSTING DATE n this field is in MKPF.
Also Database performance analysis is around 98%.
How can i give better performance, please help..
Below is my coding...
(second select query is taking too much time,how can i rectify)
SELECT BWART
GRUND
FROM T157D
INTO CORRESPONDING FIELDS OF TABLE IT_MOV_TYPE.
SELECT MATNR
MJAHR
MBLNR
BWART
WERKS
LGORT
CHARG
GRUND
MENGE
MEINS
FROM MSEG
INTO CORRESPONDING FIELDS OF TABLE IT_MSEG
FOR ALL ENTRIES IN IT_MOV_TYPE
WHERE GRUND = IT_MOV_TYPE-GRUND
AND BWART = IT_MOV_TYPE-BWART
AND WERKS = PLANT
AND LGORT IN ST_L.
SORT IT_MSEG BY MBLNR.
LOOP AT IT_MSEG.
AT NEW MBLNR.
SELECT SINGLE BUDAT
FROM MKPF
INTO V_BUDAT
WHERE MBLNR = IT_MSEG-MBLNR
AND MJAHR = IT_MSEG-MJAHR.
SELECT SINGLE MAKTX
FROM MAKT
INTO V_MAKTX
WHERE MATNR = IT_MSEG-MATNR.
SELECT SINGLE GRTXT
FROM T157E
INTO V_GRTXT
WHERE GRUND = IT_MSEG-GRUND.
ENDAT.
IT_MSEG-BUDAT = V_BUDAT.
IT_MSEG-MAKTX = V_MAKTX.
IT_MSEG-GRTXT = V_GRTXT.
MODIFY IT_MSEG.
ENDLOOP.
Regards,
Ankur
Before you use
FOR ALL ENTRIES IN
Do the following :
1. Sort the Internal table used in the 'FOR ALL ENTRIES IN' clause.
2. Make sure they do not contain duplicates.
3. Last and most importantly include a primary key field(Unique entry) in the select list. The reason, the system gets unique field records based on the where condition. It summarizes the data based on the Data type and field list in the target internal table.
One should be sure the internal table, used in the FOR ALL ENTRIES clause is not empty. If the table is empty, ALL ENTRIES will be selected. (Actually, all WHERE clauses are neglected). Of course, you should also be sure you select on INDEX fields. Otherwise the use of FOR ALL ENTRIES increases the runtime instead of improving it.
Similar Messages
-
Hi ABAPERS,
I had written one written one 'select single' query to retrieve the data from PRPS table.
but what the data was selecting for OBJNR (Object number)field based on PSPNR(WBS element) .
That OBJNR values in the report is totally different from the database table OBJNR values.How its possible
I am not getting.So can anyone please help me regarding this...
Thanks and Regards
DeepaHi deepa,
Pass the values to the select query where clause as you can see the values in SE16 display.(This is external format)
If a table field as conversion routine, then SAP dosplay some thing on screen and store some thing else in data base.
To check conversion exit go to domain of that field. Here you can see the routine used.
For example you can check domain MATNR. Externally it display with leading zeros. But internally it stores with out zeros.
Thanks,
Vinod. -
Dear experts,
Is there a way for the following SELECT query to be improved?
The problem that I'm seeing here is that the same table (l_item_tab) is being queried twice in the SELECT query (due to the "table joins", a and b). How can I improve the data retrieval here? And I also can't change the SELECT query to be out of the loop - ledger is a select-option and multiple values are possible.
* Get Ledger
SELECT * FROM t881 INTO TABLE lt_t881
WHERE rldnr IN s_rldnr.
LOOP AT lt_t881.
* Get FI-SL user-defined item table based on ledger
PERFORM get_sl_item_tab USING lt_t881-rldnr CHANGING l_item_tab.
" L_ITEM_TAB is populated here
* Get SL line items
SELECT * APPENDING CORRESPONDING FIELDS OF TABLE gt_glu1
FROM (l_item_tab) AS a
WHERE
rldnr IN s_rldnr " Ledger
AND rbukrs IN s_bukrs " Company code
AND ryyrkeg_wwsub IN s_wwsub " Subsystem
AND racct IN s_racct " Account no
AND ryymac IN s_yymac " Management area
AND rtcur IN s_rtcur " Trx currency
AND docnr IN s_docnr " Doc. number
AND docty IN s_docty " Doc. type
AND docct EQ c_docct_l " Doc. category (L = Local)
AND ryear IN s_ryear " Fiscal year
AND budat IN s_budat " Posting date
AND yystodt IN s_stodt " Reversal date
AND yystgrd IN s_stgrd " Reversal reason
AND yyintref IN s_intref " Interface ID
AND NOT exists
( SELECT * FROM (l_item_tab) AS b
WHERE
b~rldnr = a~rldnr AND
b~docnr = a~docnr AND
b~rbukrs = a~rbukrs AND
( ( ( b~docct = c_docct_y
OR b~docct = c_docct_x ) AND
b~refryear = a~ryear ) OR
( ( b~docct = c_docct_u
OR b~docct = c_docct_t ) AND
b~ryear = a~ryear ) )
ENDLOOP.
Edited by: Rob Burbank on Jun 23, 2010 12:33 PM>
Siegfried Boes wrote:
> > Will certainly try this out too..
> maybe you should think twice .... The usage of a subselect is that the result set is not transferred to the application server it is only needed
> during the selection.
>
> You should anser the following questions:
> + who wrote the code? you or? I get the impression that you don't know what is intended.
> + SQL Trace, what are the numbers for repeated executions, (go to summary by SQL statement), duration, execution, records
> + how many different tables are accessed, l_item_tab is dynamic
> + what knid of tables are accessed?
>
> Siegfried
Hi Siegfried,
- The codes are currently existing ones and they were not written by me too. I just do know that the first / main SELECT statement in the query is for retrieving FI special ledger line item data records, while the second / sub SELECT statement is to ensure that the line item data records are not already reversed, and not a reversal.
- In the summarized SQL trace of a sample test run: executions = 1, identical executions = 0, duration = 700247324 (almost 100% of the total processing durations), records = 0 (there should be more records returned in an actual production run)
- Only one table, ZZGLV4A (custom) is accessed based on the selection screen variant. ZZGLV4A is an FI special ledger line item table and its data volume: 455 mil. data records. Note: The codes are written for a report that runs in the background, and the selection screen variant is used for the executions too. The table accessed (l_item_tab is ZZGLV4A in this case) depends on the ledger inputted here - only one ledger is specified for the current selection screen variant.
- Table accessed - FI special ledger line item data.
I tried tuning the query a little further by just properly specifying the WHERE fields - only a minimal improvement is observed, an average of about 6% of runtime improvement only (tested via SE30 in the development box for ZZGLV4A but its table volume is 4 mil. data records here only). This obviously works only for the table ZZGLV4A for now, I'm afraid.
Any other ideas on how such subqueries can be improved generally (maybe secondary indexes)? The subquery is certainly re-hitting the same table at least twice.
Thanks for the inputs once again! -
Select query u0096 Performance
Hi Friends,
How can I improve the performance of this select query?
*-Fetch Actions / Organizational Assignment / Personal Data
SELECT a~pernr
b~kostl
b~orgeh
b~plans
b~ename
c~nachn
c~vorna
c~rufnm
INTO TABLE lt_emp_pa0000
FROM pa0000 AS a
INNER JOIN pa0001 AS b ON apernr = bpernr
INNER JOIN pa0002 AS c ON bpernr = cpernr
WHERE a~sprps = space
AND a~endda GE p_date
AND a~begda LE p_date
AND a~stat2 = p_status
AND b~sprps = space
AND b~endda GE p_date
AND b~begda LE p_date
AND b~werks IN s_werks
AND b~persg IN s_persg
AND b~persk IN s_persk
AND b~abkrs IN s_abkrs
AND c~sprps = space
AND c~begda LE p_date
AND c~endda GE p_date.
Thanks,
VimalHi,
Here instead of inner join u can use for all entries keyword,
like follows,
first select from one table into itab.
then,
if itab[] is not initial.
selcet from secind table into itab for all entries in itab
where pernr = itab-pernr.
*like that for third databse table.
endif.
finally in the loop all these table gields u can move one final internal table for the output.
then performance will impove.
reward points if useful,
seshu. -
Need clarification regarding select query
Hi,
I need a little clarification regrding a Select senario
I want to select data from table which have been minupulated between a certian date like between 1-DEC-10 to 31-DEC-10 and note that table does not have any time/date column. I've applied the following query to do this.
select * from TABLE_NAME where sysdate between to_date('01-DEC-10') AND to_date('31-DEC-10');
Would it work fine because I've tried it against a table and it returned me nothing however DML occur between time period.
Regards,
AbbasiAbbasi wrote:
Hi,
I need a little clarification regrding a Select senario
I want to select data from table which have been minupulated between a certian date like between 1-DEC-10 to 31-DEC-10 and note that table does not have any time/date column. I've applied the following query to do this.
select * from TABLE_NAME where sysdate between to_date('01-DEC-10') AND to_date('31-DEC-10');
Would it work fine because I've tried it against a table and it returned me nothing however DML occur between time period.
Regards,
AbbasiAFAIK without log mining and auditing this is not possible.
http://download.oracle.com/docs/cd/B19306_01/server.102/b14215/logminer.htm -
Regarding Select query to select only 1000 records in Internal Table.
Hello Friends,
Please explain me to how to Select only 1000 records from data base table?
For Example
SELECT * INTO TABLE ITAB OR SELECT * INTO ITAB
FROM EKKO FROM EKKO
WHERE EBLEN IN S_EBLEN. WHERE EBLEN IN S_EBLEN
(Currently i am using) UP TO 1000 ROWS.
ENDSELECT. (I do not want to use)
In this case Internal table may be store more then 1000 records. But i want to select first 1000 records.
Please explain me.
Regards
AmitHi,
TABLES : ekko.
selection-screen begin of block b1 with frame title text_t01.
SELECT-OPTIONS: S_EBELN for ekko-EBELN.
selection-screen end of block b1.
DATA itab TYPE STANDARD TABLE OF ekko.
SELECT *
FROM ekko
INTO TABLE itab
UP TO 1000 ROWS
WHERE EBELN IN S_EBELN.
Thanks,
Sri. -
Regarding select query with code
Hi Experts ,
i m taking two transparent table "VBAK , VBAP" WITH CLUSTER TABLE "KONV"
In this Table we create inner join between VBAK ,ABAP to itab and make itab2 with some field of KONV.
===========================================================
*& Report Z_VBAP_REPORT *
REPORT Z_VBAP_REPORT .
TABLES:VBAP,VBAK,KONV.
DATA KNUMV LIKE VBAK-KNUMV.
DATA:BEGIN OF ITAB OCCURS 0,
VBELN LIKE VBAK-VBELN,
POSNR LIKE VBAP-POSNR,
MATNR LIKE VBAP-MATNR,
NETPR LIKE VBAP-NETPR,
BNAME LIKE VBAK-BNAME,
ERDAT LIKE VBAK-ERDAT,
KBETR LIKE KONV-KBETR,
KNTYP LIKE KONV-KNTYP,
KNUMV LIKE VBAK-KNUMV,
END OF ITAB.
DATA ITAB2 LIKE ITAB OCCURS 0 WITH HEADER LINE.
PARAMETER SALE_DOC LIKE VBAK-VBELN.
SELECT-OPTIONS D_DATE FOR VBAK-ERDAT.
SELECT BVBELN APOSNR AMATNR ANETPR BBNAME BERDAT INTO
CORRESPONDING FIELDS OF TABLE ITAB FROM VBAP AS A INNER JOIN VBAK
AS B ON ( BVBELN = AVBELN ) WHERE B~ERDAT IN D_DATE AND
A~VBELN = SALE_DOC.
SELECT KBETR KNTYP FROM KONV INTO CORRESPONDING FIELDS OF TABLE ITAB2
WHERE KNUMV = KNUMV .
EXPORT ITAB TO MEMORY ID 'ASHISH'.
EXPORT ITAB2 TO MEMORY ID 'ASHISH1'.
SUBMIT Z_ABAP2 VIA SELECTION-SCREEN AND RETURN.
///////Code for report Z_ABAP2
*& Report Z_ABAP2 *
REPORT Z_ABAP2 .
TABLES:VBAP,VBAK,KONV.
DATA:BEGIN OF ITAB OCCURS 0,
VBELN LIKE VBAK-VBELN,
POSNR LIKE VBAP-POSNR,
MATNR LIKE VBAP-MATNR,
NETPR LIKE VBAP-NETPR,
BNAME LIKE VBAK-BNAME,
ERDAT LIKE VBAK-ERDAT,
KBETR LIKE KONV-KBETR,
KNTYP LIKE KONV-KNTYP,
KNUMV LIKE VBAK-KNUMV,
END OF ITAB.
DATA ITAB2 LIKE ITAB OCCURS 0 WITH HEADER LINE.
LOOP AT ITAB.
WRITE : / ITAB-VBELN,
ITAB-POSNR, 25
ITAB-MATNR,
ITAB-NETPR,
ITAB-BNAME,
ITAB-ERDAT,
ITAB-KBETR,
ITAB-KNTYP.
ENDLOOP.
LOOP AT ITAB2.
WRITE :/ ITAB2-KBETR,
ITAB2-KNTYP.
ENDLOOP.
INITIALIZATION .
IMPORT ITAB FROM MEMORY ID 'ASHISH'.
IMPORT ITAB2 FROM MEMORY ID 'ASHISH1'.
=========================================================
thanks & regards ,
AshishHi
after the first select use for all entries of this ITAB and fetch data from KONV as below, declare I_FINAL and move the ITAB and ITAB2 data into it and display its output
SELECT BVBELN APOSNR AMATNR ANETPR BBNAME BERDAT <b>B~KNUMV</b> INTO
CORRESPONDING FIELDS OF TABLE ITAB FROM VBAP AS A INNER JOIN VBAK
AS B ON ( BVBELN = AVBELN ) WHERE B~ERDAT IN D_DATE AND
A~VBELN = SALE_DOC.
<b>IF NOT ITAB[] IS INITIAL.
SELECT KNUMV KPOSN KBETR KNTYP FROM KONV INTO CORRESPONDING FIELDS OF TABLE ITAB2 for all entries in ITAB
WHERE KNUMV = ITAB-KNUMV and KPOSN = ITAB-POSNR .</b>
Loop at itab.
move-corresponding itab to i_final.
read table itab2 with key knumv = itab-knumv and kposn = itab-posnr.
if sy-subrc = 0.
move-corresponding itab2 to i_final.
endif.
append i_final.
clear i_final.
endloop.
Loop at i_final.
write: / I_final (write all the fields)
endloop.
<b>Reward points for useful Answers</b>
Regards
Anji -
Hello experts,
I want a join on table ekpo & mbew(for valuation class).
But its not working.
if valclass is initial.
select aebeln aebelp atxz01 amatnr abukrs awerks a~menge
a~mtart
b~bklas
from ekpo as a inner join mbew as b on bmatnr eq amatnr
into corresponding fields of table ekpo_mbew
where ( a~matnr eq mat_num ) and
( a~bukrs eq compcode ) and
( a~werks eq plant ) and
( a~mtart eq mat_type ) and
b~bklas in ('3001','3045' ,'3006','3055','3065') and
( amatnr eq bmatnr ).
else.
select aebeln aebelp atxz01 amatnr abukrs awerks a~menge
a~mtart
b~bklas
from ekpo as a inner join mbew as b on bmatnr eq amatnr
into corresponding fields of table ekpo_mbew
where ( a~matnr eq mat_num ) and
( a~bukrs eq compcode ) and
( a~werks eq plant ) and
( a~mtart eq mat_type ) and
( b~bklas in valclass ) and
( amatnr eq bmatnr ).
endif.
Plz tell where I m wrng.
Ravi.Hi,
Try the below code,
SELECT aebeln aebelp atxz01 amatnr abukrs awerks a~menge
a~mtart
b~bklas
FROM ekpo AS a INNER JOIN mbew AS b ON bmatnr EQ amatnr INTO CORRESPONDING FIELDS OF TABLE ekpo_mbew
WHERE a~matnr EQ mat_num AND
a~bukrs EQ compcode AND
a~werks EQ plant AND
a~mtart EQ mat_type AND
b~bklas IN ('3001','3045' ,'3006','3055','3065').
ELSE.
SELECT aebeln aebelp atxz01 amatnr abukrs awerks a~menge
a~mtart
b~bklas
FROM ekpo AS a INNER JOIN mbew AS b
ON bmatnr EQ amatnr
INTO CORRESPONDING FIELDS OF TABLE ekpo_mbew
WHERE a~matnr EQ mat_num AND
a~bukrs EQ compcode AND
a~werks EQ plant AND
a~mtart eq mat_type AND
b~bklas IN valclass .
Note :- ( amatnr eq bmatnr ) this is not necessary as the INNER JOIN is on that condition itself.
Regards
Bala Krishna -
Reg: SQL select Query in BPEL process flow
<p>
Hi,
I am suppose to execute a SQL select query (in BPEL Process flow) as mention below in JDeveloper using Database adapter.
</p>
<p>
SELECT LENGTH, WIDTH, HEIGHT, WEIGHT,
</p>
<p>
LENGTH*WIDTH* HEIGHT AS ITEM_CUBE
</p>
<p>
FROM CUBE
</p>
<p>
WHERE ITEM= <xyz>
</p>
<p>
AND OBJECT= (SELECT CASE_NAME FROM CUBE_SUPPLIER WHERE ITEM=<xyz> AND SUPP_IND = ‘Y')
<strong>Now my question is:
1.</strong> What does this "*" refer to in the query and how can I retrieve the value of LENGTH*WIDTH* HEIGHT from the query where LENGTH,WIDTH and HEIGHT are the individual field in the table.
2.What does this " AS" refer to? If " ITEM_CUBE " is the alies for the table name "ITEM" to retrieve the value, then query shoud be evaluated as
</p>
<p>
SELECT LENGTH, WIDTH, HEIGHT, WEIGHT,
</p>
<p>
LENGTH*WIDTH* HEIGHT AS ITEM_CUBE
</p>
<p>
FROM CUBE
</p>
<p>
WHERE <strong>ITEM_CUBE.ITEM</strong>= <xyz>
</p>
<p>
AND <strong>ITEM_CUBE.OBJECT</strong>= (SELECT CASE_NAME FROM CUBE_SUPPLIER WHERE ITEM=<xyz> AND SUPP_IND = ‘Y')
Is my assumption correct?
Please suggest asap.
Thanks...
</p>
<p>
</p>Hi
Thank for your reply!
I have a nested select query which performs on two different table as shown below:
<p>
SELECT LENGTH, WIDTH, HEIGHT, WEIGHT,
</p>
<p>
LENGTH*WIDTH* HEIGHT AS ITEM_CUBE
</p>
<p>
FROM CUBE
</p>
<p>
WHERE ITEM= <abc>
</p>
<p>
AND OBJECT= (SELECT NAME FROM SUPPLIER WHERE ITEM=<Item> AND SUPP_IND = ‘Y')
I am using DB adapter of Oracle JDeveloper in BPEL process flow, where I can able to select only one master table in DB adapter say SUPPLIER and its attributes at a time.But as per my requirment I need to select both the table (CUBE and SUPPLIER) in a single adapter to execute my query.
It can be achievable by using two DB adapter , One to execute the nested query and another to execute the main qyery considering value of nested query as a parameter.But I want to achieve it by using a single one.
Am I correct with my concept?
Please suggest how to get it ?
</p>
Edited by: user10259700 on Oct 23, 2008 12:17 AM -
Regarding to perform in select query
could any tell the select query in this piece of code would affect the performance of the programe
DATA: BEGIN OF OUTREC,
BANKS LIKE BNKA-BANKS,
BANKL LIKE BNKA-BANKL,
BANKA LIKE BNKA-BANKA,
PROVZ LIKE BNKA-PROVZ, "Region (State, Province, County)
BRNCH LIKE BNKA-BRNCH,
STRAS LIKE BNKA-STRAS,
ORT01 LIKE BNKA-ORT01,
SWIFT LIKE BNKA-SWIFT,
END OF OUTREC.
OPEN DATASET P_OUTPUT FOR OUTPUT IN TEXT MODE.
IF SY-SUBRC NE 0. EXIT. ENDIF.
SELECT * FROM BNKA
WHERE BANKS EQ P_BANKS
AND LOEVM NE 'X'
AND XPGRO NE 'X'
ORDER BY BANKS BANKL.
PERFORM TRANSFER_DATA.
ENDSELECT.
CLOSE DATASET P_OUTPUT.
*& Transfer the data to the output file
FORM TRANSFER_DATA.
OUTREC-BANKS = BNKA-BANKS.
OUTREC-BANKL = BNKA-BANKL.
OUTREC-BANKA = BNKA-BANKA.
OUTREC-PROVZ = BNKA-PROVZ.
OUTREC-BRNCH = BNKA-BRNCH.
OUTREC-STRAS = BNKA-STRAS.
OUTREC-ORT01 = BNKA-ORT01.
OUTREC-SWIFT = BNKA-SWIFT.
TRANSFER OUTREC TO P_OUTPUT.
ENDFORM. " READ_IN_DATAHi
Ways of Performance Tuning
1. Selection Criteria
2. Select Statements
Select Queries
SQL Interface
Aggregate Functions
For all Entries
Select Over more than one Internal table
Selection Criteria
1. Restrict the data to the selection criteria itself, rather than filtering it out using the ABAP code using CHECK statement.
2. Select with selection list.
Points # 1/2
SELECT * FROM SBOOK INTO SBOOK_WA.
CHECK: SBOOK_WA-CARRID = 'LH' AND
SBOOK_WA-CONNID = '0400'.
ENDSELECT.
The above code can be much more optimized by the code written below which avoids CHECK, selects with selection list
SELECT CARRID CONNID FLDATE BOOKID FROM SBOOK INTO TABLE T_SBOOK
WHERE SBOOK_WA-CARRID = 'LH' AND
SBOOK_WA-CONNID = '0400'.
Select Statements Select Queries
1. Avoid nested selects
2. Select all the records in a single shot using into table clause of select statement rather than to use Append statements.
3. When a base table has multiple indices, the where clause should be in the order of the index, either a primary or a secondary index.
4. For testing existence , use Select.. Up to 1 rows statement instead of a Select-Endselect-loop with an Exit.
5. Use Select Single if all primary key fields are supplied in the Where condition .
Point # 1
SELECT * FROM EKKO INTO EKKO_WA.
SELECT * FROM EKAN INTO EKAN_WA
WHERE EBELN = EKKO_WA-EBELN.
ENDSELECT.
ENDSELECT.
The above code can be much more optimized by the code written below.
SELECT PF1 PF2 FF3 FF4 INTO TABLE ITAB
FROM EKKO AS P INNER JOIN EKAN AS F
ON PEBELN = FEBELN.
Note: A simple SELECT loop is a single database access whose result is passed to the ABAP program line by line. Nested SELECT loops mean that the number of accesses in the inner loop is multiplied by the number of accesses in the outer loop. One should therefore use nested SELECT loops only if the selection in the outer loop contains very few lines or the outer loop is a SELECT SINGLE statement.
Point # 2
SELECT * FROM SBOOK INTO SBOOK_WA.
CHECK: SBOOK_WA-CARRID = 'LH' AND
SBOOK_WA-CONNID = '0400'.
ENDSELECT.
The above code can be much more optimized by the code written below which avoids CHECK, selects with selection list and puts the data in one shot using into table
SELECT CARRID CONNID FLDATE BOOKID FROM SBOOK INTO TABLE T_SBOOK
WHERE SBOOK_WA-CARRID = 'LH' AND
SBOOK_WA-CONNID = '0400'.
Point # 3
To choose an index, the optimizer checks the field names specified in the where clause and then uses an index that has the same order of the fields . In certain scenarios, it is advisable to check whether a new index can speed up the performance of a program. This will come handy in programs that access data from the finance tables.
Point # 4
SELECT * FROM SBOOK INTO SBOOK_WA
UP TO 1 ROWS
WHERE CARRID = 'LH'.
ENDSELECT.
The above code is more optimized as compared to the code mentioned below for testing existence of a record.
SELECT * FROM SBOOK INTO SBOOK_WA
WHERE CARRID = 'LH'.
EXIT.
ENDSELECT.
Point # 5
If all primary key fields are supplied in the Where condition you can even use Select Single.
Select Single requires one communication with the database system, whereas Select-Endselect needs two.
Select Statements contd.. SQL Interface
1. Use column updates instead of single-row updates
to update your database tables.
2. For all frequently used Select statements, try to use an index.
3. Using buffered tables improves the performance considerably.
Point # 1
SELECT * FROM SFLIGHT INTO SFLIGHT_WA.
SFLIGHT_WA-SEATSOCC =
SFLIGHT_WA-SEATSOCC - 1.
UPDATE SFLIGHT FROM SFLIGHT_WA.
ENDSELECT.
The above mentioned code can be more optimized by using the following code
UPDATE SFLIGHT
SET SEATSOCC = SEATSOCC - 1.
Point # 2
SELECT * FROM SBOOK CLIENT SPECIFIED INTO SBOOK_WA
WHERE CARRID = 'LH'
AND CONNID = '0400'.
ENDSELECT.
The above mentioned code can be more optimized by using the following code
SELECT * FROM SBOOK CLIENT SPECIFIED INTO SBOOK_WA
WHERE MANDT IN ( SELECT MANDT FROM T000 )
AND CARRID = 'LH'
AND CONNID = '0400'.
ENDSELECT.
Point # 3
Bypassing the buffer increases the network considerably
SELECT SINGLE * FROM T100 INTO T100_WA
BYPASSING BUFFER
WHERE SPRSL = 'D'
AND ARBGB = '00'
AND MSGNR = '999'.
The above mentioned code can be more optimized by using the following code
SELECT SINGLE * FROM T100 INTO T100_WA
WHERE SPRSL = 'D'
AND ARBGB = '00'
AND MSGNR = '999'.
Select Statements contd Aggregate Functions
If you want to find the maximum, minimum, sum and average value or the count of a database column, use a select list with aggregate functions instead of computing the aggregates yourself.
Some of the Aggregate functions allowed in SAP are MAX, MIN, AVG, SUM, COUNT, COUNT( * )
Consider the following extract.
Maxno = 0.
Select * from zflight where airln = LF and cntry = IN.
Check zflight-fligh > maxno.
Maxno = zflight-fligh.
Endselect.
The above mentioned code can be much more optimized by using the following code.
Select max( fligh ) from zflight into maxno where airln = LF and cntry = IN.
Select Statements contd For All Entries
The for all entries creates a where clause, where all the entries in the driver table are combined with OR. If the number of entries in the driver table is larger than rsdb/max_blocking_factor, several similar SQL statements are executed to limit the length of the WHERE clause.
The plus
Large amount of data
Mixing processing and reading of data
Fast internal reprocessing of data
Fast
The Minus
Difficult to program/understand
Memory could be critical (use FREE or PACKAGE size)
Points to be must considered FOR ALL ENTRIES
Check that data is present in the driver table
Sorting the driver table
Removing duplicates from the driver table
Consider the following piece of extract
Loop at int_cntry.
Select single * from zfligh into int_fligh
where cntry = int_cntry-cntry.
Append int_fligh.
Endloop.
The above mentioned can be more optimized by using the following code.
Sort int_cntry by cntry.
Delete adjacent duplicates from int_cntry.
If NOT int_cntry[] is INITIAL.
Select * from zfligh appending table int_fligh
For all entries in int_cntry
Where cntry = int_cntry-cntry.
Endif.
Select Statements contd Select Over more than one Internal table
1. Its better to use a views instead of nested Select statements.
2. To read data from several logically connected tables use a join instead of nested Select statements. Joins are preferred only if all the primary key are available in WHERE clause for the tables that are joined. If the primary keys are not provided in join the Joining of tables itself takes time.
3. Instead of using nested Select loops it is often better to use subqueries.
Point # 1
SELECT * FROM DD01L INTO DD01L_WA
WHERE DOMNAME LIKE 'CHAR%'
AND AS4LOCAL = 'A'.
SELECT SINGLE * FROM DD01T INTO DD01T_WA
WHERE DOMNAME = DD01L_WA-DOMNAME
AND AS4LOCAL = 'A'
AND AS4VERS = DD01L_WA-AS4VERS
AND DDLANGUAGE = SY-LANGU.
ENDSELECT.
The above code can be more optimized by extracting all the data from view DD01V_WA
SELECT * FROM DD01V INTO DD01V_WA
WHERE DOMNAME LIKE 'CHAR%'
AND DDLANGUAGE = SY-LANGU.
ENDSELECT
Point # 2
SELECT * FROM EKKO INTO EKKO_WA.
SELECT * FROM EKAN INTO EKAN_WA
WHERE EBELN = EKKO_WA-EBELN.
ENDSELECT.
ENDSELECT.
The above code can be much more optimized by the code written below.
SELECT PF1 PF2 FF3 FF4 INTO TABLE ITAB
FROM EKKO AS P INNER JOIN EKAN AS F
ON PEBELN = FEBELN.
Point # 3
SELECT * FROM SPFLI
INTO TABLE T_SPFLI
WHERE CITYFROM = 'FRANKFURT'
AND CITYTO = 'NEW YORK'.
SELECT * FROM SFLIGHT AS F
INTO SFLIGHT_WA
FOR ALL ENTRIES IN T_SPFLI
WHERE SEATSOCC < F~SEATSMAX
AND CARRID = T_SPFLI-CARRID
AND CONNID = T_SPFLI-CONNID
AND FLDATE BETWEEN '19990101' AND '19990331'.
ENDSELECT.
The above mentioned code can be even more optimized by using subqueries instead of for all entries.
SELECT * FROM SFLIGHT AS F INTO SFLIGHT_WA
WHERE SEATSOCC < F~SEATSMAX
AND EXISTS ( SELECT * FROM SPFLI
WHERE CARRID = F~CARRID
AND CONNID = F~CONNID
AND CITYFROM = 'FRANKFURT'
AND CITYTO = 'NEW YORK' )
AND FLDATE BETWEEN '19990101' AND '19990331'.
ENDSELECT.
1. Table operations should be done using explicit work areas rather than via header lines.
2. Always try to use binary search instead of linear search. But dont forget to sort your internal table before that.
3. A dynamic key access is slower than a static one, since the key specification must be evaluated at runtime.
4. A binary search using secondary index takes considerably less time.
5. LOOP ... WHERE is faster than LOOP/CHECK because LOOP ... WHERE evaluates the specified condition internally.
6. Modifying selected components using MODIFY itab TRANSPORTING f1 f2.. accelerates the task of updating a line of an internal table.
Point # 2
READ TABLE ITAB INTO WA WITH KEY K = 'X BINARY SEARCH.
IS MUCH FASTER THAN USING
READ TABLE ITAB INTO WA WITH KEY K = 'X'.
If TAB has n entries, linear search runs in O( n ) time, whereas binary search takes only O( log2( n ) ).
Point # 3
READ TABLE ITAB INTO WA WITH KEY K = 'X'. IS FASTER THAN USING
READ TABLE ITAB INTO WA WITH KEY (NAME) = 'X'.
Point # 5
LOOP AT ITAB INTO WA WHERE K = 'X'.
ENDLOOP.
The above code is much faster than using
LOOP AT ITAB INTO WA.
CHECK WA-K = 'X'.
ENDLOOP.
Point # 6
WA-DATE = SY-DATUM.
MODIFY ITAB FROM WA INDEX 1 TRANSPORTING DATE.
The above code is more optimized as compared to
WA-DATE = SY-DATUM.
MODIFY ITAB FROM WA INDEX 1.
7. Accessing the table entries directly in a "LOOP ... ASSIGNING ..." accelerates the task of updating a set of lines of an internal table considerably
8. If collect semantics is required, it is always better to use to COLLECT rather than READ BINARY and then ADD.
9. "APPEND LINES OF itab1 TO itab2" accelerates the task of appending a table to another table considerably as compared to LOOP-APPEND-ENDLOOP.
10. DELETE ADJACENT DUPLICATES accelerates the task of deleting duplicate entries considerably as compared to READ-LOOP-DELETE-ENDLOOP.
11. "DELETE itab FROM ... TO ..." accelerates the task of deleting a sequence of lines considerably as compared to DO -DELETE-ENDDO.
Point # 7
Modifying selected components only makes the program faster as compared to Modifying all lines completely.
e.g,
LOOP AT ITAB ASSIGNING <WA>.
I = SY-TABIX MOD 2.
IF I = 0.
<WA>-FLAG = 'X'.
ENDIF.
ENDLOOP.
The above code works faster as compared to
LOOP AT ITAB INTO WA.
I = SY-TABIX MOD 2.
IF I = 0.
WA-FLAG = 'X'.
MODIFY ITAB FROM WA.
ENDIF.
ENDLOOP.
Point # 8
LOOP AT ITAB1 INTO WA1.
READ TABLE ITAB2 INTO WA2 WITH KEY K = WA1-K BINARY SEARCH.
IF SY-SUBRC = 0.
ADD: WA1-VAL1 TO WA2-VAL1,
WA1-VAL2 TO WA2-VAL2.
MODIFY ITAB2 FROM WA2 INDEX SY-TABIX TRANSPORTING VAL1 VAL2.
ELSE.
INSERT WA1 INTO ITAB2 INDEX SY-TABIX.
ENDIF.
ENDLOOP.
The above code uses BINARY SEARCH for collect semantics. READ BINARY runs in O( log2(n) ) time. The above piece of code can be more optimized by
LOOP AT ITAB1 INTO WA.
COLLECT WA INTO ITAB2.
ENDLOOP.
SORT ITAB2 BY K.
COLLECT, however, uses a hash algorithm and is therefore independent
of the number of entries (i.e. O(1)) .
Point # 9
APPEND LINES OF ITAB1 TO ITAB2.
This is more optimized as compared to
LOOP AT ITAB1 INTO WA.
APPEND WA TO ITAB2.
ENDLOOP.
Point # 10
DELETE ADJACENT DUPLICATES FROM ITAB COMPARING K.
This is much more optimized as compared to
READ TABLE ITAB INDEX 1 INTO PREV_LINE.
LOOP AT ITAB FROM 2 INTO WA.
IF WA = PREV_LINE.
DELETE ITAB.
ELSE.
PREV_LINE = WA.
ENDIF.
ENDLOOP.
Point # 11
DELETE ITAB FROM 450 TO 550.
This is much more optimized as compared to
DO 101 TIMES.
DELETE ITAB INDEX 450.
ENDDO.
12. Copying internal tables by using ITAB2[ ] = ITAB1[ ] as compared to LOOP-APPEND-ENDLOOP.
13. Specify the sort key as restrictively as possible to run the program faster.
Point # 12
ITAB2[] = ITAB1[].
This is much more optimized as compared to
REFRESH ITAB2.
LOOP AT ITAB1 INTO WA.
APPEND WA TO ITAB2.
ENDLOOP.
Point # 13
SORT ITAB BY K. makes the program runs faster as compared to SORT ITAB.
Internal Tables contd
Hashed and Sorted tables
1. For single read access hashed tables are more optimized as compared to sorted tables.
2. For partial sequential access sorted tables are more optimized as compared to hashed tables
Hashed And Sorted Tables
Point # 1
Consider the following example where HTAB is a hashed table and STAB is a sorted table
DO 250 TIMES.
N = 4 * SY-INDEX.
READ TABLE HTAB INTO WA WITH TABLE KEY K = N.
IF SY-SUBRC = 0.
ENDIF.
ENDDO.
This runs faster for single read access as compared to the following same code for sorted table
DO 250 TIMES.
N = 4 * SY-INDEX.
READ TABLE STAB INTO WA WITH TABLE KEY K = N.
IF SY-SUBRC = 0.
ENDIF.
ENDDO.
Point # 2
Similarly for Partial Sequential access the STAB runs faster as compared to HTAB
LOOP AT STAB INTO WA WHERE K = SUBKEY.
ENDLOOP.
This runs faster as compared to
LOOP AT HTAB INTO WA WHERE K = SUBKEY.
ENDLOOP. -
Regarding Data Base Select Query
Hi All,
I want to extract the data from data base into my internal table
by not giving the exact value but the pattern.
Ex) I want to select the vendor starting with patter '12*'
from LFA table in my internal table.
Which select Query I should use for getting the
Vendor No. starting with pattern '12*' ?.
Points will be rewaded.
Thanks in Advance.
With Regards
Jitendra Gujarathi.Hi jitendra,
1. simple
2.
select * from lfa1
into table lfa1
where lifnr like '12%'.
regards,
amit m . -
Regarding performance in select query
hi to all,
i wrote select query for fetching duplicate records,like select distinct *.
so at runtime it fetches all fields in table (FI dbtable).
but i want to fetch only 8 records,for this i am using
select distinct
field1
field2
..... like this ,it wasnt excuted.
how can i avoid this issue.
in program not much logic is there,only fetching the data and summation,n display.....
plz giv me reply asap.
regards
satyasatya wrote:>
> hi to all,
>
> i wrote select query for fetching duplicate records,like select distinct *.
> so at runtime it fetches all fields in table (FI dbtable).
>
> but i want to fetch only 8 records,for this i am using
> select distinct
> field1
> field2
> ..... like this ,it wasnt excuted.
>
> how can i avoid this issue.
> in program not much logic is there,only fetching the data and summation,n display.....
>
> plz giv me reply asap.
>
> regards
> satya
Hi,
write like this
> select field1
> field2
> .......
> from table
> into ( itab-f1,
> itab-f2,
> ........ )
<REMOVED BY MODERATOR>
Edited by: Alvaro Tejada Galindo on Feb 19, 2008 5:14 PM -
Performance Tuning on a Select Query
Hi,
We have a select statement as mentioned below.
SELECT loobj1 gpart vkont proid tdate lotyp lockr
INTO TABLE tb_dfkklocks
FROM dfkklocks
FOR ALL ENTRIES IN tb_fkkvkp
WHERE
lotyp = '02'
AND proid = '01'
AND lockr >= '0'
AND lockr <= '9'
AND gpart = tb_fkkvkp-gpart
AND vkont = tb_fkkvkp-vkont.
We have Primary key as shown below.
CLIENT
LOOBJ1
LOTYP
PROID
LOCKR
FDATE
TDATE
GPART
VKONT
We have a secondary index only on TDATE. What can be the ideal select stement to improve the performance?
Regards,
Lijo JosephHi Lijo,
Couple of pointers from my end.
1). Try avoiding a FOR ALL ENTRIES if the base table is huge. Instead use a array fetch and pick up more data instead of trying to restrict the data using a FOR ALL ENTRIES as this does a LOOP on the database while selecting the data.
So i would suggest not use FOR ALL ENTRIES if the base table is huge. So analyse before using a select query.
2). It is actually very difficult to mention which option is better. I would say that it all depends on the scenario. What amount of data is to be read, from which table it is being read, what kind of processing is required on database,..... and many more analysis goes to write a an efficient select statement.
3). If i am not wrong i think if you are using a FOR ALL ENTRIES and dont specify the key completely then there is no gaurentee that you fetch all the unique records. The system will compare all the character fields as a KEY field and it will not fetch all the records. So while using a FOR ALL ENTRIES its recommended to use all the key fields.
4). If you would like to really spend time and work on a efficient select statement then try using the tcode ST05 --> Select the button EXPLAIN SQL REQUEST.
type in your select query and see what is the cost of the select statement fired on the database. This will help you to select the best statement. This is a very useful tool if we have complex select statements.
Hope this info is helpful.
Cheers
VJ
Message was edited by: Vijayendra Rao -
Performance Problem in Select query
Hi,
I have performance Problem in following Select Query :
SELECT VBELN POSNR LFIMG VRKME VGBEL VGPOS
FROM LIPS INTO CORRESPONDING FIELDS OF TABLE GT_LIPS
FOR ALL ENTRIES IN GT_EKPO1
WHERE VGBEL = GT_EKPO1-EBELN
AND VGPOS = GT_EKPO1-EBELP.
as per trace i have analysed that it is fetch the complete table scan from the LIPS table and table contants almost 3 lakh records.
Kindly Suggest what we can do to optimize this query.
Regards,
Harshtypes: begin of line,
vbeln type lips-vbeln
posnr type lips-posnr
lfimg type lips-lfimg
vrkme type lips-vrkme
vgbel type lips- vgbel
vgpos type lips-vgpos
end of line.
data: itab type standard table of line,
wa type line.
IF GT_EKPO1[] IS NOT INITIAL.
SELECT VBELN POSNR LFIMG VRKME VGBEL VGPOS
FROM LIPS INTO TABLE ITAB
FOR ALL ENTRIES IN GT_EKPO1
WHERE VGBEL = GT_EKPO1-EBELN
AND VGPOS = GT_EKPO1-EBELP.
ENDIF. -
Performance issue after Upgrade from 4.7 to ECC 6.0 with a select query
Hi All,
There is a Performance issue after Upgrade from 4.7 to ECC 6.0 with a select query in a report painter.
This query is working fine when executed in 4.7 system where as it is running for more time in ECC6.0.
Select query is on the table COSP.
SELECT (FIELD_LIST)
INTO CORRESPONDING FIELDS OF TABLE I_COSP PACKAGE SIZE 1000
FROM COSP CLIENT SPECIFIED
WHERE GJAHR IN SELR_GJAHR
AND KSTAR IN SELR_KSTAR
AND LEDNR EQ '00'
AND OBJNR IN SELR_OBJNR
AND PERBL IN SELR_PERBL
AND VERSN IN SELR_VERSN
AND WRTTP IN SELR_WRTTP
AND MANDT IN MANDTTAB
GROUP BY (GROUP_LIST).
LOOP AT I_COSP .
COSP = I_COSP .
PERFORM PCOSP USING I_COSP-_COUNTER.
CLEAR: $RWTAB, COSP .
CLEAR CCR1S .
ENDLOOP.
ENDSELECT.
I have checked with the table indexes, they were same as in 4.7 system.
What can be the reson for the difference in execution time. How can this be reduced without adjusting the select query.
Thanks in advance for the responses.
Regards,
Dedeepya.Hi,
ohhhhh....... lots of problems in select query......this is not the way you should write it.
Some generic comments:
1. never use SELECT
endselect.
SELECT
into table
for all entries in table
where.
use perform statment after this selection.
2. Do not use into corresponding fields. use exact structure type.
3. use proper sequence of fields in the where condition so that it helps table go according to indexes.
e.g in your case
sequence should be
LEDNR
OBJNR
GJAHR
WRTTP
VERSN
KSTAR
HRKFT
VRGNG
VBUND
PARGB
BEKNZ
TWAER
PERBL
sequence should be same as defined in table.
Always keep select query as simple as possible and perform all other calculations etc. afterwords.
I hope it helps.
Regards,
Pranaya
Maybe you are looking for
-
What is the Account and Contact workflow or best practice?
I'm just learning the use of the web services. I have written something to upload my customers into accounts using the web services. I need to now include a contact for each account. I'm trying to understand the workflow. It looks like I need to firs
-
Dreamweaver CS4 critical installation error
I have upgraded to a new PC (running windows 8.1) and when I run the Dreamweaver CS4 installer I get "Critical errors were found in setup". I don't have any other CS4 products on my PC. What should I do?
-
Lion finder 2 finger gesture?
In Finder, i cannot use 2 finger to enlarge. i try 3 4 finger they do not work~ For example: In Snow Leopard i can use 2 finger to enlarge my desktop file icon,but in Lion it does not work- I wander to know if I can set up some gesture, or the functi
-
Black bar on bottom of iPhone 5 lock screen
There is a black bar at the bottom of my iphone 5 lock screen, is this supposed to be there? Is enyone else having this problem? The top image is what shows when I set the lock screen picture, and the bottom is what the actual lock screen looks like
-
All my icons on my dock are opening without any problem, but my mail icon will not open.