Clo*_*ldo 18 postgresql index optimization execution-plan postgresql-9.4
表t
有两个索引:
create table t (a int, b int);
create type int_pair as (a int, b int);
create index t_row_idx on t (((a,b)::int_pair));
create index t_a_b_idx on t (a,b);
insert into t (a,b)
select i, i
from generate_series(1, 100000) g(i)
;
Run Code Online (Sandbox Code Playgroud)
ANY
运算符不使用索引:
explain analyze
select *
from t
where (a,b) = any(array[(1,1),(1,2)])
;
QUERY PLAN
---------------------------------------------------------------------------------------------------
Seq Scan on t (cost=0.00..1693.00 rows=1000 width=8) (actual time=0.042..126.789 rows=1 loops=1)
Filter: (ROW(a, b) = ANY (ARRAY[ROW(1, 1), ROW(1, 2)]))
Rows Removed by Filter: 99999
Planning time: 0.122 ms
Execution time: 126.836 ms
Run Code Online (Sandbox Code Playgroud)
但其中之一与IN
运算符一起使用:
explain analyze
select *
from t
where (a,b) in ((1,1),(1,2))
;
QUERY PLAN
------------------------------------------------------------------------------------------------------------------
Index Only Scan using t_a_b_idx on t (cost=0.29..8.32 rows=1 width=8) (actual time=0.028..0.029 rows=1 loops=1)
Index Cond: (a = 1)
Filter: ((b = 1) OR (b = 2))
Heap Fetches: 1
Planning time: 0.161 ms
Execution time: 0.066 ms
Run Code Online (Sandbox Code Playgroud)
如果记录被转换为正确的类型,它使用记录索引:
explain analyze
select *
from t
where (a,b)::int_pair = any(array[row(1,1),row(1,2)])
;
QUERY PLAN
--------------------------------------------------------------------------------------------------------------
Index Scan using t_row_idx on t (cost=0.42..12.87 rows=2 width=8) (actual time=0.106..0.126 rows=1 loops=1)
Index Cond: (ROW(a, b)::int_pair = ANY (ARRAY[ROW(1, 1), ROW(1, 2)]))
Planning time: 0.208 ms
Execution time: 0.203 ms
Run Code Online (Sandbox Code Playgroud)
为什么规划器不对ANY
运算符使用非记录索引,因为它对运算符使用它IN
?
Erw*_*ter 15
在内部,有两种不同形式的IN
,以及两种不同形式的ANY
构造。
其中一个,采用set,等效于另一个,并且expr IN (<set>)
也导致与expr = ANY(<set>)
可以使用普通索引相同的查询计划。细节:
因此,以下两个查询是等效的,并且都可以使用普通索引t_a_b_idx
(如果您试图让查询使用索引,这也可以是解决方案):
EXPLAIN ANALYZE
SELECT *
FROM t
WHERE (a,b) = ANY(VALUES (1,1),(1,2));
Run Code Online (Sandbox Code Playgroud)
或者:
...
WHERE (a,b) IN (VALUES (1,1),(1,2));
Run Code Online (Sandbox Code Playgroud)
两者相同:
Run Code Online (Sandbox Code Playgroud)QUERY PLAN -------------------------------------------------------------------------------------------------------------------------- Nested Loop (cost=0.33..16.71 rows=1 width=8) (actual time=0.101..0.101 rows=0 loops=1) -> Unique (cost=0.04..0.05 rows=2 width=8) (actual time=0.068..0.070 rows=2 loops=1) -> Sort (cost=0.04..0.04 rows=2 width=8) (actual time=0.067..0.068 rows=2 loops=1) Sort Key: "*VALUES*".column1, "*VALUES*".column2 Sort Method: quicksort Memory: 25kB -> Values Scan on "*VALUES*" (cost=0.00..0.03 rows=2 width=8) (actual time=0.005..0.005 rows=2 loops=1) -> Index Only Scan using t_plain_idx on t (cost=0.29..8.32 rows=1 width=8) (actual time=0.009..0.009 rows=0 loops=2) Index Cond: ((a = "*VALUES*".column1) AND (b = "*VALUES*".column2)) Heap Fetches: 0 Planning time: 4.080 ms Execution time: 0.202 ms
但是,这不能轻易传递给函数,因为 Postgres 中没有“表变量”。这导致了启动本主题的问题:
该问题有多种解决方法。一个是我在那里添加的替代答案。其他一些:
每种形式的第二种形式都不同:ANY
采用实际数组,而IN
采用逗号分隔的值列表。
这对输入输入有不同的影响。正如我们EXPLAIN
在问题的输出中看到的,这种形式:
WHERE (a,b) = ANY(ARRAY[(1,1),(1,2)]);
Run Code Online (Sandbox Code Playgroud)
被视为以下的简写:
ROW(a, b) = ANY (ARRAY[ROW(1, 1), ROW(1, 2)])
Run Code Online (Sandbox Code Playgroud)
并比较实际的 ROW 值。Postgres 目前还不够聪明,无法看到复合类型上的索引t_row_idx
是否适用。它也没有意识到简单索引也t_a_b_idx
应该适用。
明确的演员阵容有助于克服这种缺乏智慧的问题:
WHERE (a,b)::int_pair = ANY(ARRAY[(1,1),(1,2)]::int_pair[]);
Run Code Online (Sandbox Code Playgroud)
强制转换正确的操作数 ( ::int_pair[]
) 是可选的(尽管为了性能和避免歧义更可取)。一旦左操作数具有众所周知的类型,右操作数就会从“匿名记录”强制转换为匹配类型。只有这样,运算符才被明确定义。Postgres 根据运算符和左操作数选择适用的索引。对于定义 a 的许多运算符,COMMUTATOR
查询计划器可以翻转操作数以将索引表达式移到左侧。但这对于ANY
构造是不可能的。
有关的:
WHERE (a,b) IN ((1,1),(1,2));
Run Code Online (Sandbox Code Playgroud)
.. 值被视为元素,并且 Postgres 能够比较单个整数值,正如我们在EXPLAIN
输出中再次看到的那样:
Run Code Online (Sandbox Code Playgroud)Filter: ((b = 1) OR (b = 2))
因此 Postgres 发现t_a_b_idx
可以使用简单索引。
因此,对于示例中的特定情况,将有另一种解决方案:由于示例中的自定义复合类型int_pair
恰好等同于表t
本身的行类型,我们可以简化:
CREATE INDEX t_row_idx2 ON t ((t.*));
Run Code Online (Sandbox Code Playgroud)
更短的等效语法:
CREATE INDEX t_row_idx2 ON t ((t));
Run Code Online (Sandbox Code Playgroud)
但第一个变体更安全。如果应该存在同名的列,则第二个变体将解析为该列。
然后这个查询将使用索引而不进行任何更显式的转换:
EXPLAIN ANALYZE
SELECT *
FROM t
WHERE t = ANY(ARRAY[(1,1),(1,2)]);
Run Code Online (Sandbox Code Playgroud)
Run Code Online (Sandbox Code Playgroud)QUERY PLAN ----------------------------------------------------------------------------------------------------------------------- Bitmap Heap Scan on t (cost=40.59..496.08 rows=1000 width=8) (actual time=0.19 1..0.191 rows=0 loops=1) Recheck Cond: (t.* = ANY (ARRAY[ROW(1, 1), ROW(1, 2)])) -> Bitmap Index Scan on t_row_idx2 (cost=0.00..40.34 rows=1000 width=0) (actual time=0.188..0.188 rows=0 loops=1) Index Cond: (t.* = ANY (ARRAY[ROW(1, 1), ROW(1, 2)])) Planning time: 2.575 ms Execution time: 0.267 ms
但是典型的用例将无法利用表行的隐式存在类型。
归档时间: |
|
查看次数: |
13613 次 |
最近记录: |