1

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"

Question: in SQLite, 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 as an optimization, or will it always evaluate in the order the conditions appear in the query as it is written?

4

1 回答 1

3

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

于 2014-01-04T19:21:42.820 回答