AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / user-123892

user2864740's questions

Martin Hope
user2864740
Asked: 2021-03-09 15:41:38 +0800 CST

为什么 SQL Server 在将结果选择到标量变量时不使用 OPTION(RECOMPILE) 执行常量 (UNION ALL) 分支消除?

  • 5

我们使用一些“聚合”视图使用鉴别器从多个表中进行选择(注意:这些视图不是分区视图,因为鉴别器不在基表中)。这通常在使用 时效果很好option(recompile),因为查询计划程序将在选择查询计划之前消除不可到达的union all路径。

但是,当将结果选择为标量变量时,这种恒定折叠优化似乎失败了。将结果选择到临时表变量中不会对重新编译进行反优化。

这是 SQL Server 2017 中的复制案例:

-- A table, don't need any data.
create table [test].test_table (col1 int, primary key (col1));

-- A simple 'aggregate' view. Using the same table here is irrelevant and,
-- while the view shows the scenario, it might not be required to reproduce the issue.
create view [test].test_view as
select col1, descrim = 1 from [test].test_table
union all
select col1, descrim = 2 from [test].test_table

正常查询,这会导致优化的查询计划仅涉及其中一个union all分支:

declare @descrim int = 2;

select count(col1)
from [test].test_view
where descrim = @descrim
option (recompile) -- explicit recompile here "works"

但是,一旦使用“选择到标量变量”,该计划就会变得不优化,因为它不会消除未使用的联合。(在查询文本中使用文字值时,该计划仍然正确优化。)

declare @descrim int = 2;
declare @brokeit int;

select @brokeit = count(col1)
from [test].test_view
where descrim = @descrim
option (recompile) -- explicit recompile here does NOT optimize plan for @descrim!

1. 这种去优化是“预期的”吗?

2. 关于和/或选择标量变量的这种显着的去优化行为在哪里option(recompile)记录或以其他方式深入讨论?

3. 有没有一种简单的方法可以在select @x = ..不使用临时表(变量)的情况下获得重新编译优化的计划?

虽然在查询执行期间,这union all将阻止对辅助工件的实际 IO 访问,但这仍然是查询计划生成的问题。在产生此问题的特定错误情况下,保留多个表以供考虑会阻止 SQL Server 选择适当的搜索计划,并且生成的计划选项在给定域中是非常糟糕的选择。

第一个“好”计划:

在此处输入图像描述

第二个也是“坏”的计划:

在此处输入图像描述

这个“坏”计划也有一个隐式转换警告,让我怀疑选择到标量变量可能会绕过许多不同的优化 - 甚至option(recompile)完全忽略提示。

sql-server optimization
  • 2 个回答
  • 322 Views
Martin Hope
user2864740
Asked: 2021-03-05 15:17:37 +0800 CST

是否可以将 sp_executesql “绑定”到调用过程,如查询存储中记录的那样?

  • 1

执行的语句sp_executesql看起来通常与它们出现的过程“未绑定”。通过“绑定”,我的意思是“与调用对象相关联”。

目标是让查询存储中的语句关联起来更简单:

select object_name(q.object_id) as [Statement Context]
from sys.query_store_query q
where 1=1
    and object_name(q.object_id) like 'This will be the procedure name for ''normal'' statements'

虽然可以在将出现在 中的语句中嵌入注释query_sql_text,但这感觉有点多余。

此外,似乎sp_executesql需要某种形式的上下文绑定,因为动态 SQL可以访问周围范围内的非全局临时表:没有绑定,SQL Server 如何确保创建计划中临时表模式的有效性和稳定性?

sql-server sql-server-2016
  • 1 个回答
  • 42 Views

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    连接到 PostgreSQL 服务器:致命:主机没有 pg_hba.conf 条目

    • 12 个回答
  • Marko Smith

    如何让sqlplus的输出出现在一行中?

    • 3 个回答
  • Marko Smith

    选择具有最大日期或最晚日期的日期

    • 3 个回答
  • Marko Smith

    如何列出 PostgreSQL 中的所有模式?

    • 4 个回答
  • Marko Smith

    列出指定表的所有列

    • 5 个回答
  • Marko Smith

    如何在不修改我自己的 tnsnames.ora 的情况下使用 sqlplus 连接到位于另一台主机上的 Oracle 数据库

    • 4 个回答
  • Marko Smith

    你如何mysqldump特定的表?

    • 4 个回答
  • Marko Smith

    使用 psql 列出数据库权限

    • 10 个回答
  • Marko Smith

    如何从 PostgreSQL 中的选择查询中将值插入表中?

    • 4 个回答
  • Marko Smith

    如何使用 psql 列出所有数据库和表?

    • 7 个回答
  • Martin Hope
    Jin 连接到 PostgreSQL 服务器:致命:主机没有 pg_hba.conf 条目 2014-12-02 02:54:58 +0800 CST
  • Martin Hope
    Stéphane 如何列出 PostgreSQL 中的所有模式? 2013-04-16 11:19:16 +0800 CST
  • Martin Hope
    Mike Walsh 为什么事务日志不断增长或空间不足? 2012-12-05 18:11:22 +0800 CST
  • Martin Hope
    Stephane Rolland 列出指定表的所有列 2012-08-14 04:44:44 +0800 CST
  • Martin Hope
    haxney MySQL 能否合理地对数十亿行执行查询? 2012-07-03 11:36:13 +0800 CST
  • Martin Hope
    qazwsx 如何监控大型 .sql 文件的导入进度? 2012-05-03 08:54:41 +0800 CST
  • Martin Hope
    markdorison 你如何mysqldump特定的表? 2011-12-17 12:39:37 +0800 CST
  • Martin Hope
    Jonas 如何使用 psql 对 SQL 查询进行计时? 2011-06-04 02:22:54 +0800 CST
  • Martin Hope
    Jonas 如何从 PostgreSQL 中的选择查询中将值插入表中? 2011-05-28 00:33:05 +0800 CST
  • Martin Hope
    Jonas 如何使用 psql 列出所有数据库和表? 2011-02-18 00:45:49 +0800 CST

热门标签

sql-server mysql postgresql sql-server-2014 sql-server-2016 oracle sql-server-2008 database-design query-performance sql-server-2017

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve