首页 > 数据库 > PostgreSQL > PostgreSQL的部分索引
我以前用过MySQL的部分索引。不过说实话使用场景不多。于是上次本来打算在书中也写这个。结果徐老师说PG的不一样。后来我尝试了。果然不一样。
Seq Scan on xxg (cost=0.00…45691.00 rows=100000 width=12)
(1 row)
xxg=# \d xxg
Table “public.xxg”
Column | Type | Collation | Nullable | Default
--------±--------±----------±---------±--------
id | integer | | |
a | integer | | |
b | integer | | |
Indexes:
“t1” btree (id)
Index Scan using t1 on xxg (cost=0.42…20.90 rows=10 width=12)
Index Cond: (id < 10)
(2 rows)
这个SQL用到了ID索引,属于正常。
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
但是创建时候犯了个错误 (我给a列创建,条件写的是id。这是半年前的事情。当时没注意,结果是死活不起作用)
xxg=# create index xxg_a on xxg (a) where id<10;
CREATE INDEX
Seq Scan on xxg (cost=0.00…45941.00 rows=10 width=12)
Filter: (a < 10)
(2 rows)
Index Scan using xxg_a on xxg (cost=0.14…16.59 rows=10 width=12)
(1 row)
xxg=# create index xxg_b on xxg (b) where b>10 and b<100;
CREATE INDEX
Index Scan using xxg_b on xxg (cost=0.14…8.16 rows=1 width=12)
Index Cond: (b = 22)
(2 rows)
Seq Scan on xxg (cost=0.00…45941.00 rows=1 width=12)
Filter: (b = 220)
(2 rows)
时隔半年写了一下。当时书中后来没把这个加进去。以后再结合实际加到后续中吧。
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/637517/viewspace-3024837/ ,如需转载,请注明出处,否则将追究法律责任。