Isaac
Lifelong Learner
- Local time
- Today, 15:36
- Joined
- Mar 14, 2017
- Messages
- 10,996
Many times in my T-SQL code I use variables, such as @DateStart and @DateEnd. The general purpose is of course to use them in a WHERE clause, such as:
where (columnname between @DateStart and @DateEnd)
Also, it's quite often that the query may take enough time to run at least enough so that I wish to run it in chunks as I am developing, testing, etc.
But if I'm running a chunk of code that's rather late in the query, the @DateStart type variables are of course out of scope, so I'd have to re-run the whole thing.
To solve this, I often use code like this near the beginning:
drop table if exists #DateParameters
Create table #DateParameters (DateStart datetime, DateEnd datetime)
Insert #DateParameters (DateStart, DateEnd) values (@DateStart, @DateEnd)
... So that later in my code I can instead simply run this:
from table1
inner join #DateParameters dp on (table1.columnname between dp.DateStart and dp.DateEnd)
My question is, do you think one or the other is necessarily better or worse for performance? Of course there is only one record in #DateParameters, but there are usually millions if not more in the large tables.
I already know I can "try and test it"; however, that's easier generalized than it is done, as I often run and re-run code many times in the same connection, and there may be QP's created and/or data cached, so even though I can try to test it, I'm interested in thoughts or insight nonetheless.
Thanks!
where (columnname between @DateStart and @DateEnd)
Also, it's quite often that the query may take enough time to run at least enough so that I wish to run it in chunks as I am developing, testing, etc.
But if I'm running a chunk of code that's rather late in the query, the @DateStart type variables are of course out of scope, so I'd have to re-run the whole thing.
To solve this, I often use code like this near the beginning:
drop table if exists #DateParameters
Create table #DateParameters (DateStart datetime, DateEnd datetime)
Insert #DateParameters (DateStart, DateEnd) values (@DateStart, @DateEnd)
... So that later in my code I can instead simply run this:
from table1
inner join #DateParameters dp on (table1.columnname between dp.DateStart and dp.DateEnd)
My question is, do you think one or the other is necessarily better or worse for performance? Of course there is only one record in #DateParameters, but there are usually millions if not more in the large tables.
I already know I can "try and test it"; however, that's easier generalized than it is done, as I often run and re-run code many times in the same connection, and there may be QP's created and/or data cached, so even though I can try to test it, I'm interested in thoughts or insight nonetheless.
Thanks!