What are the steps in query processing? Explain briefly. List out any 3 equivalence rules for relational algebra for query optimization.
Solution
Stages of Query Processing:
1. Parsing: The SQL query is tokenized and parsed against the SQL grammar. Syntax errors are caught here. A parse tree is produced.
2. Translation: The parse tree is converted into a relational algebra expression (or an equivalent internal representation — a query tree).
3. Optimization: The most important stage. The optimizer applies equivalence rules (push selections down, reorder joins, combine projections) and estimates the I/O cost of candidate plans using statistics (table sizes, cardinalities, histogram data). The plan with the lowest estimated cost is selected.
4. Execution: The selected physical execution plan is executed by the query executor. Physical operators (sequential scan, index scan, nested-loop join, hash join, sort-merge join) are invoked.
5. Result delivery: Rows flow from the executor to the client (via pipelining) or are materialized to a buffer first.