简体   繁体   English

LIKE运算符的预准备语句的执行计划

[英]Execution plan for prepared statement with LIKE operator

I was wondering if there might be a difference between the execution plan created for a prepared query whether the '%' in a LIKE predicate is provided initially or later. 我想知道为准备好的查询创建的执行计划之间是否存在差异,是否最初提供LIKE谓词中的'%'或稍后提供。 What I mean whether this 我的意思是否这个

SELECT * FROM table WHERE column LIKE ? || '%'

and then setting the placeholder to the value you are looking for or doing 然后将占位符设置为您要查找或正在执行的值

SELECT * FROM table WHERE column LIKE ?

and then setting the placeholder to the value you're looking for plus the '%' suffix will result in different execution plans (with the assumption that first one will be more efficient). 然后将占位符设置为您要查找的值加上'%'后缀将导致不同的执行计划(假设第一个将更有效)。

In the first version, theoretically the database would know that what I'll likely be querying for is a prefix, and hence choose an index to do this, whereas for the second form, it is arbitrary and it cannot assume anything (that said, of course also with the first form I'm still free to put any % in there, so there would still be some fallback needed). 在第一个版本中,理论上数据库会知道我可能要查询的是一个前缀,因此选择一个索引来执行此操作,而对于第二个形式,它是任意的,它不能假设任何东西(说,当然还有第一种形式,我仍然可以自由地放任何% ,所以仍然需要一些后备)。

So might there be some performance benefit, especially on some heavily optimized commercial DBs, of doing the first one or will it not matter as both execution plans will be the same and need to do some extra preparation once the parameter is filled in? 那么可能会有一些性能优势,尤其是在一些经过大量优化的商业数据库上,做第一个或者无关紧要,因为两个执行计划都是相同的,并且一旦填充参数需要做一些额外的准备工作?

You do not specify the database, but the use of double vertical parts for string concatenation suggests Oracle or Postgres. 您没有指定数据库,但使用双垂直部分进行字符串连接建议使用Oracle或Postgres。

In any case, most database engines are smart enough to recognize this: 无论如何,大多数数据库引擎足够聪明,可以识别:

where col like 'pattern%'

as something that can be handled by an index on col . 作为可以由col上的索引处理的东西。 I'm pretty sure both Postgres and Oracle do this optimization (as do MySQL and SQL Server and probably other databases). 我很确定Postgres和Oracle都会进行这种优化(就像MySQL和SQL Server以及其他数据库一样)。

The next part is speculation. 下一部分是猜测。 I would imagine that when substituting a parameterized constant into the string, the same optimization works. 我想象当将参数化常量替换为字符串时,相同的优化也起作用。 You can check this by looking at the execution plans for such queries in your database. 您可以通过查看数据库中此类查询的执行计划来检查此问题。

If you need more sophisticated text matching algorithms, most databases have support for full text search -- and this can considerably speed such searching operations. 如果您需要更复杂的文本匹配算法,大多数数据库都支持全文搜索 - 这可以大大加快搜索操作的速度。

A prepared statement which actually leads to a cached plan will never use any index for this, because it's not know what kind of wildcards are within the supplied parameter. 实际导致缓存计划的预准备语句将永远不会使用任何索引,因为它不知道提供的参数中有哪种通配符。 Only if the optimizer decides not to use a cached plan (ie re-optimizes the query every time based on the actual value in the parameter) it might actually to use an index. 仅当优化器决定不使用缓存计划(即每次基于参数中的实际值重新优化查询)时,它实际上可能使用索引。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM