PostgreSQL 9.5.7
我为用户名创建了一个索引。
现在我做了:
explain analyze select * from authentification_user where username='Pele';
我得到了:
QUERY PLAN
-----------------------------------------------------------------------------------------------------------------
Seq Scan on authentification_user (cost=0.00..1.25 rows=1 width=478) (actual time=0.017..0.019 rows=1 loops=1)
Filter: ((username)::text = 'Pele'::text)
Rows Removed by Filter: 19
Planning time: 0.101 ms
Execution time: 0.040 ms
(5 rows)
问题是:为什么它是Seq Scan?它应该是一个索引扫描。你能帮我一把吗?
simple_project=# \d authentification_user
Table "public.authentification_user"
Column | Type | Modifiers
----------+------------------------+--------------------------------------------------------------------
id | integer | not null default nextval('authentification_user_id_seq'::regclass)
username | character varying(10) | not null
pw_hash | character varying(100) | not null
email | character varying(100) | not null
Indexes:
"authentification_user_pkey" PRIMARY KEY, btree (id)
"authentification_user_username_e9ac7af7" btree (username)
"authentification_user_username_e9ac7af7_like" btree (username varchar_pattern_ops)
simple_project=#
答案 0 :(得分:1)
很好Postgres Wiki page about Index vs Seq scan usage解释了这一点。
在您的情况下,根本没有足够的数据来获取索引扫描的任何好处。