我有一个 Postgresql 表:
CREATE TABLE IF NOT EXISTS acls1k (
pkey serial PRIMARY KEY,
user_name VARCHAR(50),
tenant_id VARCHAR(36),
CONSTRAINT user_name_unique1k UNIQUE (user_name)
);
Run Code Online (Sandbox Code Playgroud)
uniqueuser_name 列上有一个索引。当我使用常量查询表时,索引用于查询:
explain analyze select * from acls1k where user_name = 'p1kuser1t1';
QUERY PLAN
----------------------------------------------------------------------------------------------------------------------------
Index Scan using user_name_unique1k on acls1k (cost=0.28..8.29 rows=1 width=53) (actual time=0.071..0.073 rows=1 loops=1)
Index Cond: ((user_name)::text = 'p1kuser1t1'::text)
Planning Time: 0.240 ms
Execution Time: 0.094 ms
(4 rows)
Run Code Online (Sandbox Code Playgroud)
但是当我使用current_user变量时,会执行顺序扫描而不是索引扫描:
explain analyze select * from acls1k …Run Code Online (Sandbox Code Playgroud)