如何提高将数据插入到组合它的多个表中的查询性能
How to improve query performance that inserts data into multiple tables that combines it
所以在我的第一个 table 中,我只检索 ID。
然后创建并填充多个 table 变量并按 :
过滤它
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,但仍然没有任何改进。
我想也许有一种方法可以在 table 变量或临时 tables 上创建索引?
您可以使用主键创建 table 个变量,但不能使用非聚集索引。
如果您需要在临时 table 上使用索引,您应该改用#tempTables。然后您可以在这些上创建索引。在处理大型数据集时,# 比 @.
表现更好
所以在我的第一个 table 中,我只检索 ID。 然后创建并填充多个 table 变量并按 :
过滤它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,但仍然没有任何改进。
我想也许有一种方法可以在 table 变量或临时 tables 上创建索引?
您可以使用主键创建 table 个变量,但不能使用非聚集索引。
如果您需要在临时 table 上使用索引,您应该改用#tempTables。然后您可以在这些上创建索引。在处理大型数据集时,# 比 @.
表现更好