在ABAP的SQL语句中写Oracle Hints

本文介绍如何在ABAP中使用OracleHints来指定查询时使用的索引,包括全表扫描和利用次级索引的方法,并提供了具体示例。此外还讨论了如何控制FORALLENTRIESHints。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

①用过的两个写法:

1、指定使用全表扫描:%_HINTS ORACLE 'FULL(table_name)'

2、指定索引:%_HINTS ORACLE 'INDEX(table_name index_name)'

其他Oracle Hints的写法可以参见这篇文章:Oracle Hint的用法

在SQL语句优化过程中,经常会用到hint。

 

②Using secondary indexes

 

Consider the following example:

SELECT * FROM SPFLI
  %_HINTS ORACLE 'INDEX("SPFLI" "SPFLI~001")'
.......
ENDSELECT.

 

In the above example, 001 is the secondary index of the table SPFLI. It's a well-known fact that the efficient way of retrieving data from the database tables is by using secondary indexes. Many database vendors provide the optimizer hints for the same. From SAP v4.5, optimizer hints can be provided by the %_HINTS parameter. This is dependent on the database systems that support optimizer hints. The point to be noted here is these optimizer hints are not standardized by the SQL standards. Each database vendor is free to provide the optimizer hints.

Now to know which index to use for our table:
1. Go to SE11 and there specify the table name
2. Now from the menu, goto --> indexes
3. select the required index.

 

Now suppose that the identifier 001 represents a non-unique secondary index comprising of the columns CITYFROM and CITYTO. The index name should be defined as:
           <tablename>~<Index Identifier>
like SPFLI~001 in the above example.The sequence of fields in the WHERE condition is of no relevance in using this optimizers index. If you specify hints incorrectly, ABAP ignores them but doesn't return a syntax error or runtime error.


The code was written in R/3 4.6C.

Code

Consider the following example:


REPORT Suresh_test.

TABLES: spfli.

DATA : t_spfli LIKE spfli OCCURS 0 WITH HEADER LINE.

SELECT * FROM spfli
  INTO TABLE t_spfli
  %_HINTS ORACLE 'INDEX("SPFLI" "SPFLI~001")'.

LOOP AT t_spfli.
  WRITE :/ t_spfli.
ENDLOOP.

 

③ABAP--如何在SELECT语句中指定索引(example)

report z_generic_test_program .

tables: csks.

start-of-selection.  

select * up to 10 rows from csks                         

               where kokrs <> space and 

                          kostl <> space                         

                %_hints oracle 'index(csks"J")'.  

  write: / csks.

endselect. 

 

④Control over FOR ALL ENTRIES Hints

Under the heading Database Interface Hints, Note 129385 describes the options you have for influencing the database interface by entering hints. The hints are evaluated in the database interface itself and are not passed on to the database. Starting with kernel Release 4.6B all the above mentioned FOR ALL ENTRIES parameters can be set via such a hint for a single statement.

In the example:  

 

SELECT * FROM [..] FOR ALL ENTRIES IN [..] WHERE [..]  

%_HINTS ORACLE '&prefer_in_itab_opt 1&&prefer_fix_blocking -1&'.

 

This way, the boolean parameter 'prefer_in_itab_opt' is explictly set and the boolean parameter 'prefer_fix_blocking' is set to its default value. FOR ALL ENTRIES hints, like hints are generally only used as a a corrective device in emergency situations; Note 129385 goes into this. The hints described here should only be used with careful consideration.

 

出处:http://blog.youkuaiyun.com/cyber_holic/archive/2008/11/20/3341718.aspx

### 测试 ABAP SQL 语句执行时间的工具 为了评估和优化 ABAPSQL 查询的性能,开发人员可以利用多种内置工具和技术来测量查询的响应时间和资源消耗情况。 #### 使用 SQL Trace 工具 SQL Trace 是一种强大的诊断工具,能够记录数据库访问的时间戳和其他相关信息。通过启用此功能,开发者可以获得详细的日志文件,其中包含了每次数据库交互的具体耗时数据[^4]。 ```abap CALL FUNCTION 'ST05_SET_TRACE' EXPORTING trace_on_off = 'X'. ``` #### 利用 SE38 或者 SE80 内置调试器 当在事务码 SE38 (Programs) 或者 SE80 (Object Navigator) 下运行程序时,可以选择开启 Debugging 模式,在这里不仅可以逐行跟踪代码逻辑,还可以查看每条 SQL 命令的实际执行计划以及所花费的时间开销[^1]。 #### 应用 ST05 性能分析仪 ST05 提供了一个图形化的界面用于展示 SQL 调用统计信息,包括但不限于读取次数、CPU 时间、等待事件等重要指标。这有助于识别潜在瓶颈并指导进一步调优工作[^2]。 #### 实施自定义计时函数模块 对于更精细粒度的需求,可以在应用程序内部编专门负责计算特定部分(如单个 OPEN SQL 请求)所需毫秒数的小型服务类或方法: ```abap DATA: lv_start TYPE timestampl, lv_end TYPE timestampl. lv_start = cl_abap_tstmp=>current_utc_timestamp( ). SELECT * INTO TABLE @lt_data FROM dba_objects WHERE owner = 'SYSTEM'. lv_end = cl_abap_tstmp=>current_utc_timestamp( ). WRITE: / |Execution Time(ms):|, ( lv_end - lv_start ) * 1000. ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值