Does SQLite optimize a query with multiple AND conditions in the WHERE clause?

后端 未结 4 2155
礼貌的吻别
礼貌的吻别 2021-01-14 05:41

In SQL databases (I use Python+Sqlite), how to make sure that, if we have 1 million rows, the query

SELECT * FROM mytable WHERE myfunction(description) <          


        
4条回答
  •  我在风中等你
    2021-01-14 06:21

    One way that you can force the order of execution is using a case expression. In general, SQL optimizers can re-arrange operations, the one exception is case.

    SELECT *
    FROM mytable
    WHERE (CASE WHEN column2 >= 1000  OR column2 IS NULL THEN 0
                WHEN myfunction(description) < 500 THEN 1
           END) = 1;
    

    Generally, case expressions are discouraged in WHERE clauses . . . one major reason is that they impede optimization. In this case, that is a good thing.

提交回复
热议问题