1. A system for optimizing a query, comprising: a construction module 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 capable of determining a plurality of flows of query variables between the plurality of components; and a cost determination and ranking module 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.