为了使用二进制搜索读取表的目的,基于一个字段对内部表(已经基于三个字段进行了排序)进行排序是一个好习惯吗?
例如:
SORT itab by field1 field2 field3.
READ TABLE itab WITH KEY field1 = 'X'
field2 = 'Y'
field3 = 'Z' BINARY SEARCH.
SORT itab by field1.
READ TABLE itab WITH KEY field1 = 'X' BINARY SEARCH.
如果我基于field1再次对内部表进行排序以便为第二个读表声明服务吗?
答案 0 :(得分:1)
You would get the same result by leaving out the second SORT, but much faster, as the internal table is already in the right order:
SORT itab by field1 field2 field3.
READ TABLE itab WITH KEY field1 = 'X'
field2 = 'Y'
field3 = 'Z' BINARY SEARCH.
READ TABLE itab WITH KEY field1 = 'X' BINARY SEARCH.
Even if the second READ TABLE
is by field2
, you should leave out the SORT
(and of course the BINARY SEARCH
too).
Scanning a table from start to finish is linear with the number of lines, but SORT
+ BINARY SEARCH
speed is n+log(n) even in the best case.
答案 1 :(得分:1)
将表排序为只读一条记录是一种不好的做法。
排序和读取组合所需的工作量总是超过在未排序的表上读取所需的工作量。