So in my first table I am retrieving only ID's. Then creating and populating multiple table variable and filtering it by :
where exists(select * from @ID d where d.ID = @TableData.ID)
so structure looks like this:
--------------------------------------------
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
------------------------------------------------------------------
Is any way to improve performance somehow?
I tried to create #TempTable instead but still, no improvements.
I thought maybe there is a way to create index on table variables or temp tables?
You can create table variables with a primary key but you cannot use non-clustered indexes.
If you need to use indexes on temporary tables you should use #tempTables instead. You can then create indexes on those. When working with large data sets the # performs better than the @.