What is the execution order of conditions in a query with an indexed column?

1.5k views Asked by At

In SQL, when a query is filtered by multiple conditions, a simple optimization is to list more restrictive conditions before less restrictive ones, because the conditions are evaluated in first-to-last order.

SELECT _id, col1, col2, col3 
FROM table 
WHERE 
    col1 = "most_resctrictive_condition" AND 
    col2 = "less_resctrictive_condition" AND
    col3 = "least_resctrictive_condition"

If a particular column is indexed, does SQLite evaluate the condition on the indexed column first, even if it is listed after other conditions?

For instance, if col3 is indexed but the other columns are not, will SQLite evaluate the 3rd condition first, or will it evaluate in the order the conditions appear in the query as it is written?

1

There are 1 answers

0
CL. On BEST ANSWER

SQLite ignores the order of conditions in the query, and always evaluates them in the order that it estimates to be the most efficient.

Read the documentation:
ANALYZE
EXPLAIN QUERY PLAN
Query Planning
Optimizer Overview