20. An article of manufacture comprising a non-transitory computer readable storage medium comprising program code tangibly embodied thereon, which when executed by a computer, performs method steps for optimizing a query, the method steps 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; and determining a combination of the plurality of flows between the plurality of components that results in a minimum cost to execute the query; wherein a component of the plurality of components represents an access method for evaluating a graph pattern, the access method comprising a way to evaluate the graph pattern based on indexing of data in one or more databases; and wherein the minimum cost is a function of the access method for evaluating the graph pattern.
20. An article of manufacture comprising a non-transitory computer readable storage medium comprising program code tangibly embodied thereon, which when executed by a computer, performs method steps for optimizing a query, the method steps 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; and determining a combination of the plurality of flows between the plurality of components that results in a minimum cost to execute the query; wherein a component of the plurality of components represents an access method for evaluating a graph pattern, the access method comprising a way to evaluate the graph pattern based on indexing of data in one or more databases; and wherein the minimum cost is a function of the access method for evaluating the graph pattern. 21. The article of manufacture according to claim 20 , wherein: the method steps further comprise generating one or more constraints for ruling out invalid flows of the plurality of flows; and the plurality of 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.