Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Postgresql-具有混合大小写或ILike的trigram gin索引的表不使用该索引_Postgresql - Fatal编程技术网

Postgresql-具有混合大小写或ILike的trigram gin索引的表不使用该索引

Postgresql-具有混合大小写或ILike的trigram gin索引的表不使用该索引,postgresql,Postgresql,如果查询中存在混合大小写或ILike,则具有三元索引的表不起作用。 我不确定我错过了什么。有什么想法吗 (即时通讯使用PostgreSQL 9.6.2) 在查询中使用小写文本,并使用索引 explain analyse select * from tbltest where "mystring2" Like '%test%'; QUERY PLAN

如果查询中存在混合大小写或ILike,则具有三元索引的表不起作用。 我不确定我错过了什么。有什么想法吗

(即时通讯使用PostgreSQL 9.6.2)

在查询中使用小写文本,并使用索引

explain analyse 
select * from tbltest 
where "mystring2" Like '%test%';

QUERY PLAN                                                                                                                   |
-----------------------------------------------------------------------------------------------------------------------------|
Bitmap Heap Scan on tbltest  (cost=20.08..56.68 rows=10 width=24) (actual time=29.846..29.846 rows=0 loops=1)                |
  Recheck Cond: ((mystring2)::text ~~ '%test%'::text)                                                                        |
  Rows Removed by Index Recheck: 100000                                                                                      |
  Heap Blocks: exact=726                                                                                                     |
  ->  Bitmap Index Scan on tbltest_idx2  (cost=0.00..20.07 rows=10 width=0) (actual time=12.709..12.709 rows=100000 loops=1) |
        Index Cond: ((mystring2)::text ~~ '%test%'::text)                                                                    |
Planning time: 0.086 ms                                                                                                      |
Execution time: 29.875 ms                                                                                                    |
如果在搜索中添加混合大小写,Like不使用索引

explain analyse 
select * from tbltest 
where "mystring2" Like '%Test%';

QUERY PLAN                                                                                                    |
--------------------------------------------------------------------------------------------------------------|
Seq Scan on tbltest  (cost=0.00..1976.00 rows=99990 width=24) (actual time=0.011..33.376 rows=100000 loops=1) |
  Filter: ((mystring2)::text ~~ '%Test%'::text)                                                               |
Planning time: 0.083 ms                                                                                       |
Execution time: 51.259 ms                                                                                     |
ILike也不使用索引

explain analyse 
select * from tbltest 
where "mystring2" ILike '%Test%';

QUERY PLAN                                                                                                    |
--------------------------------------------------------------------------------------------------------------|
Seq Scan on tbltest  (cost=0.00..1976.00 rows=99990 width=24) (actual time=0.012..87.038 rows=100000 loops=1) |
  Filter: ((mystring2)::text ~~* '%Test%'::text)                                                              |
Planning time: 0.134 ms                                                                                       |
Execution time: 105.757 ms                                                                                    |

PostgreSQL在最后两个查询中不使用索引,因为这是处理查询的最佳方式,而不是因为它不能使用它

EXPLAIN
输出中,您可以看到第一个查询返回零行(
actual…rows=0
),而其他两个查询返回表中的每一行(
actual…rows=100000

PostgreSQL优化器的估计准确地反映了这种情况


由于PostgreSQL无论如何都必须访问表中的大多数行,因此它知道,如果它按顺序扫描表,得到的结果将比使用更复杂的索引访问方法便宜得多。

我也注意到,如果我将where子句改为:“where“mystring2”类似于“%1%tEst%”,那么它也会使用索引。但是使用“where”mystring2“我喜欢“%tEst%”;”,它不使用索引。
explain analyse 
select * from tbltest 
where "mystring2" ILike '%Test%';

QUERY PLAN                                                                                                    |
--------------------------------------------------------------------------------------------------------------|
Seq Scan on tbltest  (cost=0.00..1976.00 rows=99990 width=24) (actual time=0.012..87.038 rows=100000 loops=1) |
  Filter: ((mystring2)::text ~~* '%Test%'::text)                                                              |
Planning time: 0.134 ms                                                                                       |
Execution time: 105.757 ms                                                                                    |