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; determining a combination of the plurality of flows between the plurality of components that results in a minimum cost to execute the query; generating one or more constraints for ruling out invalid flows of the plurality of flows; and using an integral linear programming (ILP) solver to solve a linear optimization problem of determining the combination of the plurality of flows between the plurality of components that results in the minimum cost under the one or more generated constraints; wherein the one or more constraints comprise a component constraint enforcing semantics of an external view of one or more of the components, a graph constraint enforcing semantics of the plurality of flows of query variables, and a predecessor constraint enforcing semantics of one or more potential predecessors.
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; determining a combination of the plurality of flows between the plurality of components that results in a minimum cost to execute the query; generating one or more constraints for ruling out invalid flows of the plurality of flows; and using an integral linear programming (ILP) solver to solve a linear optimization problem of determining the combination of the plurality of flows between the plurality of components that results in the minimum cost under the one or more generated constraints; wherein the one or more constraints comprise a component constraint enforcing semantics of an external view of one or more of the components, a graph constraint enforcing semantics of the plurality of flows of query variables, and a predecessor constraint enforcing semantics of one or more potential predecessors. 9. The method according to claim 8 , wherein determining the plurality of flows of query variables comprises ruling out one or more flows that would violate semantics of one or more control statements in the query.