简体   繁体   English

在where子句中使用多个条件的缺点

[英]Drawback of using multiple conditions in where clause

As we all know that the where clause of a sql query executes before select clause. 众所周知,sql查询的where子句在select子句之前执行。 And we put some conditions in where clause to filter out the result according to our requirement. 并且我们在where子句中放置了一些条件以根据我们的要求过滤出结果。

While writing some queries I encountered a question in my mind that, is/are there any drawback(s) of using multiple conditions in the where clause and in what order they are applied to filter the result from the selected table. 在编写一些查询时,我在脑海中遇到一个问题,即在where子句中使用多个条件以及以何种顺序将它们应用于从选定表中筛选结果是否有任何弊端。

For example: we have a table 例如:我们有一张桌子

    Building(name,height,owner,builder_name,age)

and we have a query: 我们有一个查询:

    select * from Building
    where height between X and Y and age between A and B

Now, how this query will execute. 现在,该查询将如何执行。 And what about the order of the conditions ie 那条件的顺序呢

X<=height<=Y and A<=age<=B X <=高度<= Y和A <=年龄<= B

Will it be something like, first the whole record will be searched for the height within the given range and then same thing will be done for age well. 会是这样吗,首先将在整个记录中搜索给定范围内的高度,然后对年龄进行相同的操作。 ??? ???

The query planner will try to find the optimal way to search the table and test the WHERE clause. 查询计划者将尝试找到搜索表和测试WHERE子句的最佳方法。 It will start by trying to use an index if possible, which will narrow down the rows that it needs to search. 它将首先尝试使用索引(如果可能的话),这将缩小需要搜索的行的范围。 If there are multiple potential indexes, it will try to use the one that it estimates will narrow it down best. 如果存在多个潜在指数,它将尝试使用它估计的最佳指数。

Then it will scan all these rows, and test each of them against all of the remaining conditions. 然后它将扫描所有这些行,并针对所有其余条件测试它们。 It should never need to make multiple passes over the entire table. 它永远不需要在整个表上进行多次遍历。

If you want to see how a particular query will be executed, use the EXPLAIN command. 如果要查看如何执行特定查询,请使用EXPLAIN命令。

the Database server has multiple options to solve that query. 数据库服务器具有多个选项来解决该查询。 It will choose the option the server "thinks" is faster. 它将选择服务器“认为”更快的选项。

The options I see are: 我看到的选项是:

  • Scan the whole table and filter out rows that don't satisfy the where clause 扫描整个表并过滤出不满足where子句的行
  • Seek a height range on an index on height column, then filter out rows using age between A and B predicate. height列的索引上寻找height范围,然后使用age between A and B谓词age between A and B过滤掉行。
  • Seek an age range on an index on age column, then filter out rows using height between X and Y predicate age列的索引上寻找age范围,然后使用height between X and Y谓词height between X and Y过滤掉行
  • Seek both indexes, then perform an index intersection 寻找两个索引,然后执行索引交集

The database server not always use an index that might be applicable, it considers some things before using it, such as: 数据库服务器并不总是使用可能适用的索引,它在使用它之前会考虑一些事项,例如:

  • The index selectivity. 指数选择性。
  • The index coverage. 索引覆盖率。

High selectivity indexes are more likely to be used. 高选择性指数更可能被使用。 Covering indexes are likely used. 可能使用覆盖索引。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM