1. A method for optimizing a search by a query, comprising: building at least one optimization block for a subplan; determining complexity of the subplan based on at least one optimization block; selecting an algorithm from a pool of search space generation algorithms based on the complexity of the subplan and characteristics of the algorithm; and optimizing the subplan by using the selected algorithm.