‎2007 Apr 10 7:45 AM
Hi All,
In general we code like this
data: begin of t_kna1 occurs 0,
kunnr like kna1-kunnr,
name1 like kna1-name1,
end of t_kna1.
select kunnr name1 from kna1 into table t_kna1.
sort t_kna1 by kunnr.
loop at t_bseg.
read table t_kna1 with key kunnr = t_bseg-kunnr binary search.
............
endloop.
Suppose if we put like this
data: begin of t_kna1 occurs 0,
kunnr like kna1-kunnr,
name1 like kna1-name1,
end of t_kna1.
select kunnr name1 from kna1 into table t_kna1.
sort t_kna1 by kunnr.
loop at t_bseg.
read table t_kna1 with key name1 = t_bseg-name1 kunnr = t_bseg-kunnr
binary search.
.............
endloop.
What is the difference in reading the table t_kna1 in both the sample code.
Is there any disadvantage in the second method.
Will it have any performance disadvantage.
Can anyone explain in detail.
‎2007 Apr 10 8:01 AM
Hi Ram,
While using the binary search we should mention the proper kwys else performance will be down.
In the first method record can be fetch based on the primary key kunnr.
In the second method record can be fetch based on the primary as well as secondary key also.
Instead of this
read table t_kna1 with key name1 = t_bseg-name1 kunnr = t_bseg-kunnr
Try with this
<b>read table t_kna1 with key kunnr = t_bseg-kunnr name1 = t_bseg-name1
binary search.</b>
It will improve the performance.
Hope this helps you, reply for queries, Shall post you the updates.
Regards.
Kumar
‎2007 Apr 10 9:06 AM
Hi Shri Ram,
Use of binary search option
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.
Not Recommended
Read table int_fligh with key airln = LF.
Recommended
Read table int_fligh with key airln = LF binary search.Refer
http://help.sap.com/saphelp_nw04/helpdata/en/fc/eb373d358411d1829f0000e829fbfe/content.htm
Regards,
Santosh
‎2007 Apr 10 9:20 AM
Hi,
Note:
The standard table must be sorted in ascending order by the specified search key. The BINARY SEARCH addition means that you can access an entry in a standard table by its key as quickly as you would be able to in a sorted table.
Binary Search usage is best when you use the keys that are sorted in the same order. it speeds up the retrieval.
sort itab by A B.
Read table itab with key A = field1
B = field2.
this will give the much better performance compared to
Read table itab with key B = field2
A = field1.
Regards,
madhu
‎2007 Apr 10 12:34 PM
Hi,
You have added name1 field in comapring keys in second eg. but, immediatly before that while sorting you have not added that field into SORT. This may lead to erros sometimes. so write like:
sort t_kna1 by name1 kunnr.
After this it will improve performance.
Check it.
Jogdand M B
‎2007 Apr 10 12:37 PM
hi
good
due to the performance issues it would be better to read the particular tableusing the READ statment.
thanks
mrutyun^
‎2007 Apr 10 1:02 PM
As KUNNR is the primary key of KNA1, and therefore the internal table, you only need to sort or match on that field. Your second piece of code becomes:
TYPES: begin of tY_kna1,
kunnr like kna1-kunnr,
name1 like kna1-name1,
end of tY_kna1.
DATA : T_KNA1 TYPE SORTED TABLE OF TY_KNA1
WITH UNIQUE KEY KUNNR.
select kunnr name1 from kna1 into table t_kna1.
sort t_kna1 by kunnr.
loop at t_bseg.
read table t_kna1 with TABLE key kunnr = t_bseg-kunnr.
.............
endloop.MattG.
‎2007 Apr 11 3:13 PM
Hi ram,
I think the question you are asking is regarding the Binary search.
In the second case as you are using binary search and the table is sorted . the read statement will be fast than the first one.
beware you need to sort the table in the ascending order of the search filed's or else you get unwanted results.