查询优化器根据要从每个表中检索的行数进行查询成本估计。反过来,估计的行数是根据 WHERE 子句所使用得每个条件表达式的选择性确定的。用来选择行的条件表达式称为过滤器。
选择性是 0 与 1 间的某个值,该值指示表中过滤器可以通过的行的比例。一个选择性的过滤器,如果通过很少的行,那么其选择性接近 0,如果几乎通过所有行,那么过滤器的选择性接近 1。有关过滤器的准则,请参阅改进过滤器选择性。
优化器可以使用数据分发计算查询中过滤器的选择性。然而,在没有数据分发的情况下,数据库服务器将根据表索引计算不同类型的过滤器的选择性。下表列出了优化器分配给不同类型的过滤器的一些选择性值。使用数据分发计算得出的选择性甚至要比该表显示的选择性更精确。
在该表中:
过滤器表达式 | 选择性 (F) |
---|---|
indexed-col = literal-valueindexed-col = host-variableindexed-col IS NULL | F = 1/(索引中不同键的个数) |
tab1.indexed-col = tab2.indexed-col | F = 1/(较大的索引中不同键的个数) |
indexed-col > literal-value | F = (2nd-max - literal-value)/(2nd-max - 2nd-min) |
indexed-col < literal-value | F = (literal-value - 2nd-min)/(2nd-max - 2nd-min) |
any-col IS NULLany-col = any-expression | F = 1/10 |
any-col > any-expressionany-col < any-expression | F = 1/3 |
any-col MATCHES any-expressionany-col LIKE any-expression | F = 1/5 |
EXISTS subquery | F = 1 if subquery estimated to return >0 rows, else 0 |
NOT expression | F = 1 - F(expression) |
expr1 AND expr2 | F = F(expr1) x F( expr2) |
expr1 OR expr2 | F = F(expr1) + F( expr2) - (F(expr1) x F(expr2 )) |
any-col IN list | Treated as any-col = item1 OR . . . OR any-col = itemn. |
any-col relop ANY subquery | Treated as any-col relop value1 OR
. . . OR any-col relop value
n for estimated size of subquery n。
其中 relop 是任何关系运算符,如 <、>、>= 和 <=。 |