8. A method for optimizing a query, comprising: building a data structure for a query, the data structure including a plurality of components, wherein each of the plurality of components corresponds to at least one graph pattern; determining a plurality of flows of query variables between the plurality of components; generating one or more constraints to dynamically eliminate invalid flows from the plurality of flows of query variables, wherein a flow is determined to be invalid if the flow would violate semantics of one or more control statements in the query; wherein the one or more constraints are expressed as a function of decision variables and comprise one or more of at least one component constraint enforcing semantics of an external view of the plurality of components, at least one graph constraint enforcing semantics of the plurality of flows of query variables, and at least one predecessor constraint enforcing semantics of one or more potential predecessors; formulating a cost function associated with the plurality of flows; and outputting a query plan based on the cost function, wherein outputting the query plan comprises determining a combination of the plurality of flows that results in a minimum cost under the one or more constraints.
8. A method for optimizing a query, comprising: building a data structure for a query, the data structure including a plurality of components, wherein each of the plurality of components corresponds to at least one graph pattern; determining a plurality of flows of query variables between the plurality of components; generating one or more constraints to dynamically eliminate invalid flows from the plurality of flows of query variables, wherein a flow is determined to be invalid if the flow would violate semantics of one or more control statements in the query; wherein the one or more constraints are expressed as a function of decision variables and comprise one or more of at least one component constraint enforcing semantics of an external view of the plurality of components, at least one graph constraint enforcing semantics of the plurality of flows of query variables, and at least one predecessor constraint enforcing semantics of one or more potential predecessors; formulating a cost function associated with the plurality of flows; and outputting a query plan based on the cost function, wherein outputting the query plan comprises determining a combination of the plurality of flows that results in a minimum cost under the one or more constraints. 15. The method of claim 8 , wherein the plurality of components represent respective access methods for the graph pattern, and wherein each access method is associated with a respective cost.