postgres LIKE % 运算符 vs similarity – pg_trgm.similarity_threshold . 不同的查询计划

huangapple go评论53阅读模式
英文:

postgres LIKE % operator vs similarity - pg_trgm.similarity_threshold . different query plans

问题

如果我使用:

SET pg_trgm.similarity_threshold = 0.9;
... where column % 'some string s';


PostgreSQL在列gin_trgm_ops上执行索引扫描。

相比之下,应该是相同的:

... where similarity(column, 'some string s' ) >= 0.9


这将执行顺序扫描而不是使用索引。

从文档中可以看到:
https://www.postgresql.org/docs/11/pgtrgm.html

> <code>text % text</code>	boolean	 
> 如果其参数的相似度大于由 `pg_trgm.similarity_threshold` 设置的当前相似度阈值,则返回true。

为什么呢?
英文:

If I use:

SET pg_trgm.similarity_threshold = 0.9;
... where column % 'some string s';

PostgreSQL does an index scan on: gin (column gin_trgm_ops)

In contrast, which should be the same:

... where similarity(column, 'some string s' ) >= 0.9

That does a seqential scan instead of using the index.

From the documentation:
https://www.postgresql.org/docs/11/pgtrgm.html

> <code>text % text</code> boolean
> Returns true if its arguments have a similarity that is greater than the current similarity threshold set by pg_trgm.similarity_threshold.

Why?

答案1

得分: 2

基本上,WHERE 条件必须具备以下形式才能允许索引扫描:

&lt;indexed expression&gt; &lt;operator&gt; &lt;constant&gt;

在这里,&lt;indexed expression&gt; 是你在索引上创建的表达式,&lt;operator&gt; 是索引的操作符类(operator class)支持的操作符,而 &lt;constant&gt; 是在索引扫描期间保持不变的表达式(特别地,它只能包含 STABLE 函数)。

在 PostgreSQL 中的一个例外情况是,如果你在 WHERE 条件中使用返回 boolean 的函数,并且该函数有一个优化器支持函数,允许 PostgreSQL 替代索引扫描。但是,similarity 不是这样的函数。

英文:

Fundamentally, a WHERE condition must look like this to allow an index scan:

&lt;indexed expression&gt; &lt;operator&gt; &lt;constant&gt;

Here, &lt;indexed expression&gt; is what you created the index on &lt;operator&gt; is an operator supported by the operator family of the index's operator class, and &lt;constant&gt; is an expression that is constant for the duration of the index scan (in particular, it can only contain STABLE functions).

The one exception in PostgreSQL is if you use a function returning boolean in the WHERE condition, and that function has an optimizer support function that allows PostgreSQL to substitute an index scan. But similarity is no such function.

huangapple
  • 本文由 发表于 2023年6月26日 19:58:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/76556461.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定