Patent ID: 7475064

Claim:
An apparatus comprising: at least one processor; a memory coupled to the at least one processor; a query residing in the memory that includes a UNION-type expression; a query optimizer residing in the memory and executed by the at least one processor, wherein the query optimizer determines there are common components on both sides of the UNION-type expression of sufficient complexity that optimization is achievable by processing the UNION-type expression to a different form by comparing the estimated time to run the UNION-type expression with the estimated time to run the optimized UNION-type expression plus the time to create a temporary data structure; and wherein the query optimizer removes a common component from both sides of the UNION-type expression in the query and formulates the common component into the temporary data structure using a WITH statement, and processes the query so non-common components are in a reformulated UNION-type expression, and generates a new query that includes the WITH statement and the reformulated UNION-type expression.