1. A system for optimizing a query, comprising: a memory, and at least one processor operatively coupled to the memory; a construction module executed via the at least one processor and capable of 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; a flow module executed via the at least one processor and capable of determining a plurality of flows of query variables between the plurality of components; and a cost determination and ranking module executed via the at least one processor and capable of 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.