SORT_MERGE (UNION, INTERSECT, and EXCEPT Option)

SORT_MERGE causes the optimizer to consider sort-merge when choosing an algorithm for evaluating the statement. This can improve efficiency if you want the final result set to be ordered.

The sort-merge algorithm is considered only when the result of the UNION needs to be ordered, such as when you see a SORT node somewhere above the UNION in your query execution plan. If that is not the case, and you still want option SORT_MERGE to apply, you can add an ORDER BY clause at the end of the expression that contains the UNION, or at a level above it.

Note that if a SORT node is present, TDV automatically selects the UNION SORT_MERGE algorithm (in other words, no user action is needed). If you set SORT_MERGE to FALSE, the UNION SORT_MERGE algorithm is not used.

Note: An ORDER BY option is required at the end of the expression or at the level above in order for the sort-merge to apply.

Operators

UNION, UNION ALL

Syntax

SORT_MERGE[={"TRUE"|"FALSE"}]

Example

SELECT column1 FROM table1 
	UNION ALL {OPTION SORT_MERGE="TRUE"}
	ORDER BY column1