108. A machine-readable storage medium storing one or more sequences of instructions for processing a query, which instructions, when executed by one or more processors, cause the one or more processors to perform steps comprising: receiving the query, wherein the query comprises one or more query blocks; determining multiple starting queries based on the query and one or more heuristics; for each starting query of the multiple starting queries: establishing said each starting query as a local-best query; determining a set of candidate queries and costs, wherein the costs include a cost for each candidate query in the set of candidate queries, wherein each candidate query in the set of candidate queries is determined based on the local-best query and an alternative state for a particular query block of the one or more query blocks, and wherein, if a specific cost for a specific candidate query satisfies a first particular mathematical relationship with a local-best cost for the local-best query, then establishing the specific candidate query as the local-best query; wherein each candidate query in the set of candidate queries is semantically equivalent to the query; choosing a particular query from a set of local-best queries based on costs for each query in the set of local-best queries, wherein the set of local-best queries comprises the local-best query for each set of candidate queries; and executing the particular query in response to a request to execute the query.
108. A machine-readable storage medium storing one or more sequences of instructions for processing a query, which instructions, when executed by one or more processors, cause the one or more processors to perform steps comprising: receiving the query, wherein the query comprises one or more query blocks; determining multiple starting queries based on the query and one or more heuristics; for each starting query of the multiple starting queries: establishing said each starting query as a local-best query; determining a set of candidate queries and costs, wherein the costs include a cost for each candidate query in the set of candidate queries, wherein each candidate query in the set of candidate queries is determined based on the local-best query and an alternative state for a particular query block of the one or more query blocks, and wherein, if a specific cost for a specific candidate query satisfies a first particular mathematical relationship with a local-best cost for the local-best query, then establishing the specific candidate query as the local-best query; wherein each candidate query in the set of candidate queries is semantically equivalent to the query; choosing a particular query from a set of local-best queries based on costs for each query in the set of local-best queries, wherein the set of local-best queries comprises the local-best query for each set of candidate queries; and executing the particular query in response to a request to execute the query. 113. The machine-readable storage medium of claim 108 , wherein a particular set of candidate queries from among the sets of candidate queries comprises a certain query, and wherein the instructions that cause determining the particular set of candidate queries and costs comprise instructions which, when executed by the one or more processors, cause the one or more processors to perform the steps of: determining a partial cost for a first portion of the certain query; and if the partial cost satisfies the first particular mathematical relationship with the local-best cost, determining a third cost based on the entire certain query.