如何在 PL/SQL 中内联变量?
情况
对于 Oracle 11.2.0.2.0 中的大量数据的中型查询,我的查询执行计划遇到了一些问题。为了加快速度,我引入了一个范围过滤器,它的作用大致如下:
PROCEDURE DO_STUFF(
org_from VARCHAR2 := NULL,
org_to VARCHAR2 := NULL)
-- [...]
JOIN organisations org
ON (cust.org_id = org.id
AND ((org_from IS NULL) OR (org_from <= org.no))
AND ((org_to IS NULL) OR (org_to >= org.no)))
-- [...]
如您所见,我想使用可选范围来限制 organizations
的 JOIN
组织编号。客户端代码可以在有(应该很快)或没有(非常慢)限制的情况下调用DO_STUFF
。
麻烦的
是,PL/SQL 将为上述 org_from 和 org_to 参数创建绑定变量,这正是我在大多数情况下所期望
-- [...]
JOIN organisations org
ON (cust.org_id = org.id
AND ((:B1 IS NULL) OR (:B1 <= org.no))
AND ((:B2 IS NULL) OR (:B2 >= org.no)))
-- [...]
:
的 在这种情况下,当我内联这些值时,我测量到查询执行计划要好得多,即当 Oracle 执行的查询实际上类似于
-- [...]
JOIN organisations org
ON (cust.org_id = org.id
AND ((10 IS NULL) OR (10 <= org.no))
AND ((20 IS NULL) OR (20 >= org.no)))
-- [...]
“很多”时,我的意思是快 5-10 倍。请注意,该查询很少执行,即每月一次。所以我不需要缓存执行计划。
我的问题
如何在 PL/SQL 中内联值?我知道立即执行,但我更愿意PL/SQL 编译我的查询,而不进行字符串连接。
我只是测量了巧合发生的事情还是我可以假设内联变量确实更好(在这种情况下)?我之所以问这个问题,是因为我认为绑定变量迫使 Oracle 设计一个通用执行计划,而内联值则允许分析非常具体的列和索引统计信息。所以我可以想象这不仅仅是一个巧合。
我错过了什么吗?除了变量内联之外,也许还有一种完全不同的方法来实现查询执行计划的改进(请注意,我也尝试了很多提示,但我不是该领域的专家)?
The Situation
I have some trouble with my query execution plan for a medium-sized query over a large amount of data in Oracle 11.2.0.2.0. In order to speed things up, I introduced a range filter that does roughly something like this:
PROCEDURE DO_STUFF(
org_from VARCHAR2 := NULL,
org_to VARCHAR2 := NULL)
-- [...]
JOIN organisations org
ON (cust.org_id = org.id
AND ((org_from IS NULL) OR (org_from <= org.no))
AND ((org_to IS NULL) OR (org_to >= org.no)))
-- [...]
As you can see, I want to restrict the JOIN
of organisations
using an optional range of organisation numbers. Client code can call DO_STUFF
with (supposed to be fast) or without (very slow) the restriction.
The Trouble
The trouble is, PL/SQL will create bind variables for the above org_from
and org_to
parameters, which is what I would expect in most cases:
-- [...]
JOIN organisations org
ON (cust.org_id = org.id
AND ((:B1 IS NULL) OR (:B1 <= org.no))
AND ((:B2 IS NULL) OR (:B2 >= org.no)))
-- [...]
The Workaround
Only in this case, I measured the query execution plan to be a lot better when I just inline the values, i.e. when the query executed by Oracle is actually something like
-- [...]
JOIN organisations org
ON (cust.org_id = org.id
AND ((10 IS NULL) OR (10 <= org.no))
AND ((20 IS NULL) OR (20 >= org.no)))
-- [...]
By "a lot", I mean 5-10x faster. Note that the query is executed very rarely, i.e. once a month. So I don't need to cache the execution plan.
My questions
How can I inline values in PL/SQL? I know about EXECUTE IMMEDIATE, but I would prefer to have PL/SQL compile my query, and not do string concatenation.
Did I just measure something that happened by coincidence or can I assume that inlining variables is indeed better (in this case)? The reason why I ask is because I think that bind variables force Oracle to devise a general execution plan, whereas inlined values would allow for analysing very specific column and index statistics. So I can imagine that this is not just a coincidence.
Am I missing something? Maybe there is an entirely other way to achieve query execution plan improvement, other than variable inlining (note I have tried quite a few hints as well but I'm not an expert on that field)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
在您的一篇评论中您说:
有两种路径。如果您传入 NULL 作为参数,那么您将选择所有记录。在这种情况下,全表扫描是检索数据的最有效方法。如果您传入值然后索引读取可能更有效,因为您只选择信息的一小部分,
当您使用绑定变量制定查询时,优化器必须做出决定:是否应该假设大多数情况下您会通过。那么,换个角度看:当您只需要选择记录的子集时进行全表扫描,或者当您需要选择记录的子集时进行索引读取,效率是否更高。需要选择所有记录吗?
似乎优化器已经将全表扫描视为覆盖所有可能性的最低效操作,
而当您对值进行硬编码时,优化器会立即知道
10 IS NULL 评估结果为 FALSE,因此它可以权衡使用索引读取来查找所需子集记录的优点。
那么,该怎么办呢?正如您所说,此查询每月仅运行一次,我认为只需要对业务流程进行少量更改即可进行单独的查询:一个针对所有组织,一个针对组织的子集。
好吧,问题是你有一对指定范围的绑定变量。根据值的分布,不同的范围可能适合不同的执行计划。也就是说,这个范围(可能)适合索引范围扫描......
而这可能更适合全表扫描......
这就是绑定变量查看发挥作用的地方
(取决于分布 。当然是价值观)。
In one of your comments you said:
There are two paths. If you pass in NULL for the parameters then you are selecting all records. Under those circumstances a Full Table Scan is the most efficient way of retrieving data. If you pass in values then indexed reads may be more efficient, because you're only selecting a small subset of the information.
When you formulate the query using bind variables the optimizer has to take a decision: should it presume that most of the time you'll pass in values or that you'll pass in nulls? Difficult. So look at it another way: is it more inefficient to do a full table scan when you only need to select a sub-set of records, or to do indexed reads when you need to select all records?
It seems as though the optimizer has plumped for full table scans as being the least inefficient operation to cover all eventualities.
Whereas when you hard code the values the Optimizer knows immediately that
10 IS NULL
evaluates to FALSE, and so it can weigh the merits of using indexed reads for find the desired sub-set records.So, what to do? As you say this query is only run once a month I think it would only require a small change to business processes to have separate queries: one for all organisations and one for a sub-set of organisations.
Okay, so the thing is you have a pair of bind variables which specify a range. Depending on the distribution of values, different ranges might suit different execution plans. That is, this range would (probably) suit an indexed range scan...
...whereas this is likely to be more fitted to a full table scan...
That is where Bind Variable Peeking comes into play.
(depending on distribution of values, of course).
由于查询计划实际上始终不同,这意味着优化器的基数估计由于某种原因而关闭。您能否从查询计划中确认优化器预期使用绑定变量时条件选择性不足?由于您使用的是 11.2,Oracle 应该使用 自适应光标共享,因此它不应该是绑定变量窥视问题(假设您在测试中使用不同的
NO
值多次调用带有绑定变量的版本。是好计划的基数估计实际上是正确的吗?我知道您说过
NO
列上的统计信息是准确的,但我对您的常规统计收集过程可能不会更新的杂散直方图表示怀疑例如,您始终可以在查询中使用提示来强制使用特定索引(尽管使用
但是,可以尝试进行一项额外的测试。是将 SQL 99 连接语法替换为 Oracle 的旧语法,即
这显然不应该改变任何内容,但 SQL 99 语法存在解析器问题,因此需要检查。
Since the query plans are actually consistently different, that implies that the optimizer's cardinality estimates are off for some reason. Can you confirm from the query plans that the optimizer expects the conditions to be insufficiently selective when bind variables are used? Since you're using 11.2, Oracle should be using adaptive cursor sharing so it shouldn't be a bind variable peeking issue (assuming you are calling the version with bind variables many times with different
NO
values in your testing.Are the cardinality estimates on the good plan actually correct? I know you said that the statistics on the
NO
column are accurate but I would be suspicious of a stray histogram that may not be updated by your regular statistics gathering process, for example.You could always use a hint in the query to force a particular index to be used (though using a stored outline or optimizer plan stability would be preferable from a long-term maintenance perspective). Any of those options would be preferable to resorting to dynamic SQL.
One additional test to try, however, would be to replace the SQL 99 join syntax with Oracle's old syntax, i.e.
That obviously shouldn't change anything, but there have been parser issues with the SQL 99 syntax so that's something to check.
闻起来像绑定窥视< /a>,但我只使用 Oracle 10,所以我不能声称 11 中存在同样的问题。
It smells like Bind Peeking, but I am only on Oracle 10, so I can't claim the same issue exists in 11.
这看起来很像需要自适应游标共享,并结合 SQLPlan 稳定性。
我认为发生的情况是
capture_sql_plan_baselines 参数为 true
。use_sql_plan_baselines
也是如此。如果这是真的,则会发生以下情况:如果自适应游标共享已经处于活动状态,优化器将生成一个新的/更好的计划,将其存储在 sql_plan_baselines 中,但无法使用它,直到有人接受这个较新的计划作为可接受的替代计划。检查
dba_sql_plan_baselines
并查看您的查询是否包含accepted = 'NO'且verified = null
的条目您可以使用 dbms_spm.evolve 来改进新计划,如果计划的性能至少比没有新计划的性能好 1.5 倍,则自动接受它。
我希望这有帮助。
This looks a lot like a need for Adaptive Cursor Sharing, combined with SQLPlan stability.
I think what is happening is that the
capture_sql_plan_baselines parameter is true
. And the same foruse_sql_plan_baselines
. If this is true, the following is happening:If Adaptive Cursor Sharing is already active,the optimizer will generate a new/better plan, store it in the sql_plan_baselines but is not able to use it, until someone accepts this newer plan as an acceptable alternative plan. Check
dba_sql_plan_baselines
and see if your query has entries withaccepted = 'NO' and verified = null
You can use
dbms_spm.evolve
to evolve the new plan and have it automatically accepted if the performance of the plan is at least 1,5 times better than without the new plan.I hope this helps.
我将其添加为评论,但也会在这里提供。希望这不是过于简单化,看看详细的回复,我可能会误解确切的问题,但无论如何......
似乎您的组织表有列 no (org.no) 被定义为数字。在您的硬编码示例中,您使用数字进行比较。
在您的过程中,您传入 varchar2:
因此要将 varchar2 与数字进行比较,Oracle 将必须进行转换,因此这可能会导致完整扫描。
解决方案:更改过程以传递数字
I added this as a comment, but will offer up here as well. Hope this isn't overly simplistic, and looking at the detailed responses I may be misunderstanding the exact problem, but anyway...
Seems your organisations table has column no (org.no) that is defined as a number. In your hardcoded example, you use numbers to do the compares.
In your procedure, you are passing in varchar2:
So to compare varchar2 to number, Oracle will have to do the conversions, so this may cause the full scans.
Solution: change proc to pass in numbers