我是 Postgres 的新手,我正在尝试更多地了解索引。我使用的是 12.5 版,这是我的代码:
CREATE TABLE textfun(content TEXT);
CREATE UNIQUE INDEX text_b ON textfun(content);
INSERT INTO textfun (content)
SELECT (CASE WHEN (random()<=0.3) THEN 'https://mywebsite/nanana/'
WHEN (random()<=0.6) THEN 'https://mywebsite/friendy/'
ELSE 'https://mywebsite/mina/' END) || generate_series(1000000,2000000);
在这里,我创建了一百万条记录,希望看到索引的效果。
当我尝试获取查询计划时:
explain analyze
SELECT content FROM textfun WHERE content LIKE 'mina%';
我得到了这个:
Gather (cost=1000.00..14300.34 rows=100 width=32) (actual time=77.574..80.054 rows=0 loops=1)
Workers Planned: 2
Workers Launched: 2
Parallel Seq Scan on textfun (cost=0.00..13290.34 rows=42 width=32) (actual time=69.022..69.022 rows=0 loops=3)
Filter: (content ~~ 'mina%'::text)
Rows Removed by Filter: 333334
Planning Time: 0.254 ms
Execution Time: 80.071 ms
(8 rows)
我期待一个并行索引扫描。
我试过了:
explain analyze
SELECT content FROM textfun WHERE content LIKE '1500000%';
和:
explain analyze
SELECT content FROM textfun WHERE content LIKE '%mina';
但两者都给了我一个顺序扫描计划。
这里有没有我遗漏的细节,为什么我没有得到索引扫描?