Note: This was originally posted by an inactive account. Content was preserved by moving under an admin account.
Originally posted by: stonysmithIn the case, as you point out, it (obviously) is faster to not use the complicated expressions inside the AggEx.
The reason for this is actually rather simple.. the SORT node (inside the AggEx) can perform better with simpler expressions.
The sort node makes multiple passes of the data. This is an over-generalization of the sort node, but it'll explain why:
The sort node makes an initial pass of the data to determine which of several sort algorithms will work best for the data. During this first pass, the sort node breaks the data up into "sort segments" - smaller groups of records. The individual sort segments are then sorted, sometimes requiring more "sort segments" to be created, and finally the node outputs the final sorted sequence.
If you have complicated expressions for the sort keys, then the sort node has to re-evaluate the key(s) each time it writes records for the various segments.. if an initial segment has to be broken again up into 2 or 3 smaller segments, then it has to re-evaluate the expressions.
You've hit upon one of the optimizations possible... First, if the data is already sorted, then in the AggEx, turn off sorting, and you'll save that whole step. Second, as you found, if the Sort key is based on expressions, you may well find that it's faster to build a temporary column for the key.
FYI.. when building that temporary field, you might find another small speed enhancement if you'd make it a single column...
FIELD1.date().year() * 100 + FIELD2.date().month().subtract(1).div(3)
=======
A small side note:
The "Sort Segments" are individual files that are held open by the operating system while Sort is running. Sometimes, for huge datasets, you end up opening more files than the system can handle. On some servers, the setting is at only 1024 files, and you might see an error message "Too many files open".
There are two parameters you can tweak to work with this situation.
1) In Linux, there is an "open files limit" per user. You can see the settings by using the "ulimit -a" command. You can change the OpenFilesLimit by using the command "ulimit -n nnnnnn"
2) There is a LAE Server Property named ls.brain.node.sort.maxbuffersize which you can work with. If you make the number larger, the node will create larger segments, requiring fewer open files. Unfortunately, there is no "rule" for what to set it to.. it purely is a function of how much data you have to sort (both number of rows and number of columns), how many keys are in use, and what your system's cpu and memory configuration is. You'll have to just experiment with it a bit.