关闭

ABAP 语句优化

406人阅读 评论(0) 收藏 举报
分类:

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.

0
0
查看评论
发表评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场

ABAP程序性能优化的一些做法(应该用那些优秀的写法)

待定
  • zzj_rainbow
  • zzj_rainbow
  • 2013-11-08 18:58
  • 2827

sap abap 性能优化(二)

四、性能分析 SQL 性能追踪(ST05) 1、SAP R/3 提供标准ABAP SQL 跟踪工具。使用T-Code:ST05 可以进入追踪设定画面。 2、Trace on with Filte...
  • wu21211721
  • wu21211721
  • 2016-07-27 17:30
  • 544

ABAP程序优化的一些总结

最近做了一个数据迁移的程序,了解学习了不少优化程序的方法,下面总结下。   第一部分 程序运行时间的优化 优化的重点之一是运行时间,影响运行时间的因素有很多,首先程序本身的话,从CPU角度讲,指令越少...
  • Raffin
  • Raffin
  • 2015-02-04 22:53
  • 1258

我们一起学ABAP(05)~流程控制

1、ABAP-流程控制 SAP的数据流程控制中,有很多复杂的逻辑操作。因此也开发有相应的管理和组织项目的语法,如:ABAP的分支结构: if、case 。循环操作:do、while等。 1.1、IF...
  • qq_37625033
  • qq_37625033
  • 2017-03-29 15:08
  • 201

ABAP中Collect的用法

简单来说:collect在非数值字段相同的情况下,起到了数值字段汇总作用。
  • sunnyonly1992
  • sunnyonly1992
  • 2016-06-30 10:08
  • 6348

ABAP-SQL基础知识

SQL语法    我们在编写ABAP4程序的时候,经常需要从TABLE中根据某些条件读取数据,读取数据最常用的方法就是通过SQL语法实现的。ABAP/4中可以利用SQL语法创建或读取TABLE,...
  • zhongguomao
  • zhongguomao
  • 2016-08-05 14:41
  • 340

ABAP-用Shift去掉字符串的前导'0'

SHIFT str LEFT DELETING LEADING '0'. 如果要在layout显示不出前面的0 格式: &字段(zc)& 如果要在layout显示不出小数点后面的0 格式:   &...
  • Yanweijie1
  • Yanweijie1
  • 2013-05-06 17:17
  • 1295

ABAP 基本语法

 ABAP 基本语法 数据类型 C :字符串 D :日期型 格式为 YYYYMMDD 例:'1999/12/03' F : 浮点数 长度为8 I :整数 N :数值组成的...
  • alu049
  • alu049
  • 2017-04-13 18:39
  • 464

ABAP从逐条语句入门到精通——变量、…

REPORT ZHELLOWORLD. * 基本的输入输出操作 WRITE '!!!!@#!@#!@#!@#!@#/ANY NUMBER'. * 输出换行符 WRITE /. * Data...
  • u010770993
  • u010770993
  • 2017-04-21 10:51
  • 228

ABAP DYNP_VALUES_UPDATE 更新屏幕字段的函数及用法

ABAP更新屏幕字段的函数及用法
  • SAPmatinal
  • SAPmatinal
  • 2016-11-17 13:30
  • 1805
    个人资料
    • 访问:4381755次
    • 积分:62253
    • 等级:
    • 排名:第46名
    • 原创:1739篇
    • 转载:2520篇
    • 译文:101篇
    • 评论:329条
    博客专栏
    最新评论
    微信公众号
      为你推荐最新的博文~更有惊喜等着你