sql - 如何提高将数据插入到组合它的多个表中的查询性能
问题描述
所以在我的第一个表中,我只检索 ID。然后创建和填充多个表变量并通过以下方式对其进行过滤:
where exists(select * from @ID d where d.ID = @TableData.ID)
所以结构看起来像这样:
--------------------------------------------
declare @DateFrom datetime
@DateTo datetime
--table that contain IDs only
declare @ID table (ID int)
insert into @ID
select ID
from Table
where dates between @DateFrom and @DateTo
--table with data
declare @TableData
select
case...
case...
case...
case...
from Table1
join Table1
join Table1
join Table1
where exists(select * from @ID d where d.ID = @TableData.ID) --filtering only match IDs from @ID table
------------------------------------------------------------------
有什么方法可以以某种方式提高性能吗?
我尝试创建#TempTable,但仍然没有改进。
我想也许有一种方法可以在表变量或临时表上创建索引?
解决方案
您可以使用主键创建表变量,但不能使用非聚集索引。
如果你需要在临时表上使用索引,你应该使用#tempTables。然后,您可以在这些上创建索引。在处理大型数据集时,# 的性能优于 @。
推荐阅读
- azure - 我可以在没有公共 IP 的情况下为 Azure VM 提供 Internet 访问权限吗?
- python - 将字节 csv 字符串转换为 pandas 数据帧
- sql - 如何将另一列的值赋予表函数的参数?(或者,具有多个 table_names 的 Snowflake copy_history)
- r - R Shiny - 在更新相互依赖的输入时了解观察和观察事件之间的区别
- jquery - Laravel - 如何使用 jQuery 使用 keyup 验证 Laravel 文本输入
- python - os.listdir 只显示隐藏文件?
- android - 使用通知 Android 销毁当前活动
- javascript - React.forwardRef 与流中的通用 Props 类型
- c# - 如何进行 T-SQL 查询,其中输出的第一个字母等于一个字符
- firebase - 获取多个用户