Performance Optimization - 5.4 [Traversal technology] First-half ordered grouping
For the transaction table example taken above, we now want to change its order in a way that the transaction records of account are changed to be sorted by the day of the week of the occurrence date, that is, put together all the transactions occurred on Sunday, put together all the transactions occurred on Monday, Tuesday… Essentially, it changes the sorting method of original table, yet it belongs to big sorting operation. The big sorting algorithm will generate the buffer file, and the performance will not be good.
However, we found that the original data table has been ordered by account, it can be considered as a “half ordered” table, moreover, the amount of unordered data in each (ordered) account is not large. In this case, we can use the program cursor mentioned above to perform the big sorting.
|2||for B1;id||return B2.sort(day@w(dt))|
Fetching the data of each account followed by sorting the “second-half”, the program cursor can collect the results and return the desired big sorting result.
Of course, this method can also be used for grouping. For example, if we want to calculate the total transaction amount of each account on the day of each week, we just need to change the above A3 to:
It can also be handled in the subprogram, in this way, the amount of returned data will be less:
|2||for B1;id||return B2.groups(id,day@w(dt);sum(amount))|
This situation is not uncommon in reality. SPL directly provides an option at the group() function:
Note that group@q()has three groups of parameters. The first represents the ordered grouping keys, the second represents the unordered grouping keys, and the third is aggregating expressions. By means of the three group of parameters together with the @q option, SPL will know that it should firstly use the ordered cursor to fetch the data with same id, and then group by day@w(dt).
This code can also be used directly to sort.
If the @s option is added, it means that only sorting is performed without grouping, that is, sort the data with the same id by day@w(dt).