ABAP 语句优化

翻译 2015年07月08日 11:03:51

Correct Way For Selection

Instead of selecting all the data and doing the processing during the selection, it is advisable to restrict the data to the selection criteria itself, rather than filtering it out using the ABAP code.

Incorrect

            Select * from zflight.

             Check : zflight -airln = 'LF' and zflight-fligh = 'BW222'.

            Endselect.

Correct            Select * from zflight where airln = 'LF' and fligh = '222'.

            Endselect.

One more point to be noted here is of the select *. Often this is a lazy coding practice. When a programmer gives select * even if one or two fields are to be selected, this can significantly slow the program and put unnecessary load on the entire system. When the application server sends this request to the database server, and the database server has to pass on the entire structure for each row back to the application server. This consumes both CPU and networking resources, especially for large structures.

Thus it is advisable to select only those fields that are needed, so that the database server passes only a small amount of data back.

Also it is advisable to avoid selecting the data fields into local variables as this also puts unnecessary load on the server. Instead attempt must be made to select the fields into an internal table.

How To use aggregate functions

Using the already provided aggregate functions, instead of finding out the minimum/maximum values using ABAP code.

Incorrect             Maximum_numb = 0.

            Select * from zflight where airln = 'LF' and cntry = 'IN'.

             Check zflight -fligh > maximum_numb.

             Maximum_numb = zflight -fligh.

            Endselect.

Correct             Select max( field ) from ztable into maximum_numb where airln = 'LF' and cntry = 'IN'.

The other aggregate functions that can be used are min (to find the minimum value), avg (to find the average of a Data interval), sum (to add up a data interval) and count (counting the lines in a data selection).

Using Views in place of tables


Many times ABAP programmers deal with base tables and nested selects. Instead it is always advisable to see whether there is any view provided by SAP on those base tables, so that the data can be filtered out directly, rather than specially coding for it.

Incorrect

            Select * from zcntry where cntry like 'IN%'.

             Select single * from zflight where cntry = zcntry-cntry and airln = 'LF'.

            Endselect.

Correct

            Select * from zcnfl where cntry like 'IN%' and airln = 'LF'.

            Endselect.

Use of the into table clause of select statement

Instead of appending one record at a time into an internal table, it is advisable to select all the records in a single shot.

Incorrect

            Refresh: int_fligh.

            Select * from zflight into int_fligh.

             Append int_fligh. Clear int_fligh.

            Endselect.

Correct

            Refresh: int_fligh.

            Select * from zflight into table int_fligh.

Modify cluster of lines

Use the variations of the modify command to speed up this kind of processing.

Incorrect

            Loop at int_fligh.

             If int_fligh-flag is initial.

                        Int_fligh-flag = 'X'.

             Endif.

             Modify int_fligh.

            Endloop.

Correct

            Int_fligh-flag = 'X'.

            Modify int_fligh transporting flag where flag is initial.

Binary Search

When a programmer uses the read command, the table is sequentially searched. This slows down the processing. Instead of this, use the binary search addition. The binary search algorithm helps faster search of a value in an internal table. It is advisable to sort the internal table before doing a binary search. Binary search repeatedly divides the search interval in half. If the value to be searched is less than the item in the middle of the interval, the search is narrowed to the lower half, otherwise the search is narrowed to the upper half.

Incorrect

            Read table int_fligh with key  airln = 'LF'.

Correct

            Read table int_fligh with key  airln = 'LF' binary search.

Appending 2 internal tables

Instead of using the normal loop-endloop approach for this kind of programming, use the variation of the append command. Care should be taken that the definition of both the internal tables should be identical.

Incorrect

            Loop at int_fligh1.

             Append int_fligh1 to int_fligh2.

            Endloop.

Correct

            Append lines of int_fligh1 to int_fligh2.

Table Buffering

Use of buffered tables is recommended to improve the performance considerably. The buffer is bypassed while using the following statements

  1. Select distinct
  2. Select ... for update
  3. Order by, group by, having clause
  4. Joins

Use the Bypass buffer addition to the select clause in order to explicitly bypass the buffer while selecting the data.

FOR ALL Entries

Outer join can be created using this addition to the where clause in a select statement. It speeds up the performance tremendously, but the cons of using this variation are listed below

  1. Duplicates are automatically removed      from the resulting data set. Hence care should be taken that the unique      key of the detail line items should be given in the select statement.
  2. If the table on which the For All      Entries IN clause is based is empty, all rows are selected into the      destination table. Hence it is advisable to check before-hand that the      first table is not empty.
  3. If the table on which the For All      Entries IN clause is based is very large, the performance will go down      instead of improving. Hence attempt should be made to keep the table size      to a moderate level.

Incorrect

            Loop at int_cntry.

             Select single * from zfligh into int_fligh

where cntry = int_cntry-cntry.

Append int_fligh.

            Endloop.

Correct

            Select * from zfligh appending table int_fligh

            For all entries in int_cntry

            Where cntry = int_cntry-cntry.

Structure of Where Clause

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.

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. One more tip is that if a table begins with MANDT, while an index does not, there is a high possibility that the optimizer might not use that index.

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.

Move Statement

Instead of using the move-corresponding clause it is advisable to use the move statement instead. Attempt should be made to move entire internal table headers in a single shot, rather than moving the fields one by one.

Inner Join

When multiple SAP tables are logically joined, it is always advisable to use inner join to read the data from them. This certainly reduces the load on the network.

Let us take an example of 2 tables, zairln and zflight. The table zairln has the field airln, which is the airline code and the field lnnam, which is the name of the airline. The table zflight has the field airln, the airline code and other fields which hold the details of the flights that an airline operates.

Since these 2 tables a re logically joined by the airln field, it is advisable to use the inner join.

             Select a~airln a~lnnam b~fligh b~cntry into table int_airdet

            From zairln as a inner join zflight as b on a~airln = b~airln.

In order to restrict the data as per the selection criteria, a where clause can be added to the above inner join.

Using ABAP Sort in place of Order By

The order by clause is executed on the database server, while the sort statement is executed on the application server. Thus instead of giving the order by in the select clause statement, it is better to collect the records in an internal table and then use the sort command to sort the resulting data set.

相关文章推荐

abap 关于SQL语句的性能

  • 2007年11月07日 21:30
  • 1KB
  • 下载

SAP ABAP 动态语句详解

  • 2009年07月27日 18:52
  • 259KB
  • 下载

ABAP report常用的一些语句用法(包括字符串操作,变量动态操作)(持续更新)

1、计算内表的长度:       DESCRIBE TABLE   IT_BKPF   LINES   LEN.       后面除了可以接LINES显示条数,还可以接KIND,显示内表的属性,还有...

ABAP数据库操作之操作语句

  • 2013年04月25日 09:30
  • 2KB
  • 下载

ABAP程序的数据查询语句SELECT.doc

  • 2010年03月17日 17:14
  • 34KB
  • 下载

ABAP Screen的常用语句

ABAP Screen的常用语句 (1).Screen 设计 TABLES: SSCRFIELDS. PARAMETERS: P_EBLEN LIKE VBRK-EBLEN ...

ABAP数据库操作系列之操作语句讲解Select

1.select详解:       Select single:这个语句从数据库中选取一条数据,如果根据查询的条件(where)可以得到多条数据的话,必须有(endselect),在这种情况下,在a...

ABAP 筑基宝典(5) -- 关于使用READ TABLE语句的几点注意事项

转载自:http://www.dlsap.com/thread-34-1-1.html 向原作者 莫怀远 致谢   1.  如果使用READ TABLE语句来读取内部表数据,而不是简单看返回值判...

[abap][转载] 关于SCREEN的一些常用语句

说明: 屏幕参数设计 (SCREEN设计)原文地址: http://blog.sina.com.cn/s/blog_3f3df76a0100c6jr.html(1).SCREEN 设计TABLES: ...

ABAP最基础也最重要的语句之 select语句!讲述这些年来遇到的一些小问题!

本文不再copy前人们累积的select的用法等文章,旨在把这些年从初学者开始接触select语句所遇到的问题和解决办法写在这,一是帮助自己累积,二是希望可以供大家参考,有错的请拍砖,欢迎查错补漏! ...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:ABAP 语句优化
举报原因:
原因补充:

(最多只允许输入30个字)