Optimizing TSQL
up vote
0
down vote
favorite
There is a select query that runs repeatedly with the first column in the select clause being the only thing which gets replaced by other column and the rest of the query structure remain the same.
So that this query plan get cached, is there any way that we can parametrize the first column.
Select c1,p,a
From table1,
Select c2,p,a
From table1
Select c3,p,a
From table1
Select c4,p,a
From table1
Every time the query runs ,only the first column is the one changing . Is there any way to optimize this kind of query?
tsql optimization
New contributor
add a comment |
up vote
0
down vote
favorite
There is a select query that runs repeatedly with the first column in the select clause being the only thing which gets replaced by other column and the rest of the query structure remain the same.
So that this query plan get cached, is there any way that we can parametrize the first column.
Select c1,p,a
From table1,
Select c2,p,a
From table1
Select c3,p,a
From table1
Select c4,p,a
From table1
Every time the query runs ,only the first column is the one changing . Is there any way to optimize this kind of query?
tsql optimization
New contributor
1
I don't think there's a way to "parameterise" this. You don't have awhere
clause, so you're getting all records regardless. Since you're getting all records and not ordering / anything special, they'll just be returned from the table itself (i.e. there's no benefit looking for these values in indexes), so this is the best you'll get.
– JohnLBevan
19 hours ago
If this is really your query, I doubt you will gain anything IF query caching is possible. The query optimizer takes a decent time when it has to decide one among multiple ways of executing the query; A simple select columns from table has no alternatives, maybe an index, but it won't take too much.
– George Menoutis
18 hours ago
@JohnLBevan I'm pretty sure that the above was shortened for brevity... I take "the rest of the query structure remain the same" as "there is a rather complex query behind"... What you cannot parameterise will be the column's alias, but the content should be rather easy...
– Shnugo
16 hours ago
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
There is a select query that runs repeatedly with the first column in the select clause being the only thing which gets replaced by other column and the rest of the query structure remain the same.
So that this query plan get cached, is there any way that we can parametrize the first column.
Select c1,p,a
From table1,
Select c2,p,a
From table1
Select c3,p,a
From table1
Select c4,p,a
From table1
Every time the query runs ,only the first column is the one changing . Is there any way to optimize this kind of query?
tsql optimization
New contributor
There is a select query that runs repeatedly with the first column in the select clause being the only thing which gets replaced by other column and the rest of the query structure remain the same.
So that this query plan get cached, is there any way that we can parametrize the first column.
Select c1,p,a
From table1,
Select c2,p,a
From table1
Select c3,p,a
From table1
Select c4,p,a
From table1
Every time the query runs ,only the first column is the one changing . Is there any way to optimize this kind of query?
tsql optimization
tsql optimization
New contributor
New contributor
New contributor
asked 19 hours ago
M. Sol
11
11
New contributor
New contributor
1
I don't think there's a way to "parameterise" this. You don't have awhere
clause, so you're getting all records regardless. Since you're getting all records and not ordering / anything special, they'll just be returned from the table itself (i.e. there's no benefit looking for these values in indexes), so this is the best you'll get.
– JohnLBevan
19 hours ago
If this is really your query, I doubt you will gain anything IF query caching is possible. The query optimizer takes a decent time when it has to decide one among multiple ways of executing the query; A simple select columns from table has no alternatives, maybe an index, but it won't take too much.
– George Menoutis
18 hours ago
@JohnLBevan I'm pretty sure that the above was shortened for brevity... I take "the rest of the query structure remain the same" as "there is a rather complex query behind"... What you cannot parameterise will be the column's alias, but the content should be rather easy...
– Shnugo
16 hours ago
add a comment |
1
I don't think there's a way to "parameterise" this. You don't have awhere
clause, so you're getting all records regardless. Since you're getting all records and not ordering / anything special, they'll just be returned from the table itself (i.e. there's no benefit looking for these values in indexes), so this is the best you'll get.
– JohnLBevan
19 hours ago
If this is really your query, I doubt you will gain anything IF query caching is possible. The query optimizer takes a decent time when it has to decide one among multiple ways of executing the query; A simple select columns from table has no alternatives, maybe an index, but it won't take too much.
– George Menoutis
18 hours ago
@JohnLBevan I'm pretty sure that the above was shortened for brevity... I take "the rest of the query structure remain the same" as "there is a rather complex query behind"... What you cannot parameterise will be the column's alias, but the content should be rather easy...
– Shnugo
16 hours ago
1
1
I don't think there's a way to "parameterise" this. You don't have a
where
clause, so you're getting all records regardless. Since you're getting all records and not ordering / anything special, they'll just be returned from the table itself (i.e. there's no benefit looking for these values in indexes), so this is the best you'll get.– JohnLBevan
19 hours ago
I don't think there's a way to "parameterise" this. You don't have a
where
clause, so you're getting all records regardless. Since you're getting all records and not ordering / anything special, they'll just be returned from the table itself (i.e. there's no benefit looking for these values in indexes), so this is the best you'll get.– JohnLBevan
19 hours ago
If this is really your query, I doubt you will gain anything IF query caching is possible. The query optimizer takes a decent time when it has to decide one among multiple ways of executing the query; A simple select columns from table has no alternatives, maybe an index, but it won't take too much.
– George Menoutis
18 hours ago
If this is really your query, I doubt you will gain anything IF query caching is possible. The query optimizer takes a decent time when it has to decide one among multiple ways of executing the query; A simple select columns from table has no alternatives, maybe an index, but it won't take too much.
– George Menoutis
18 hours ago
@JohnLBevan I'm pretty sure that the above was shortened for brevity... I take "the rest of the query structure remain the same" as "there is a rather complex query behind"... What you cannot parameterise will be the column's alias, but the content should be rather easy...
– Shnugo
16 hours ago
@JohnLBevan I'm pretty sure that the above was shortened for brevity... I take "the rest of the query structure remain the same" as "there is a rather complex query behind"... What you cannot parameterise will be the column's alias, but the content should be rather easy...
– Shnugo
16 hours ago
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
I must admit, that this smells a bit. Might be that there are better approaches, but we do not know your use cases.
You can try this:
DECLARE @mockup TABLE(c1 INT, c2 INT, c3 INT, p VARCHAR(100),a VARCHAR(100));
INSERT INTO @mockup VALUES(1,2,3,'Row 1','blah 1')
,(11,22,33,'Row 2','blah 2')
,(111,222,333,'Row 3','blah 3');
DECLARE @FirstColumn VARCHAR(10)='c3';
SELECT CASE @FirstColumn WHEN 'c1' THEN c1
WHEN 'c2' THEN c2
WHEN 'c3' THEN c3
ELSE NULL END AS DynamicFirstColumn
,p
,a
FROM @mockup;
The idea is to use a CASE
to decide which column is seen in the first place following a parameter. If you want to see a varying column name too (I used DynamicFirstColumn
) you can
- create a VIEW for each case or
- use dynamically created SQL.
As a workaround you can include the handed in parameter and return it with the result set. In this case the consumer would know, which column was used...
SELECT CASE @FirstColumn WHEN 'c1' THEN c1
WHEN 'c2' THEN c2
WHEN 'c3' THEN c3
ELSE NULL END AS DynamicFirstColumn
,p
,a
,@FirstColumn --<-- Here we include the source we use above
FROM @mockup
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
I must admit, that this smells a bit. Might be that there are better approaches, but we do not know your use cases.
You can try this:
DECLARE @mockup TABLE(c1 INT, c2 INT, c3 INT, p VARCHAR(100),a VARCHAR(100));
INSERT INTO @mockup VALUES(1,2,3,'Row 1','blah 1')
,(11,22,33,'Row 2','blah 2')
,(111,222,333,'Row 3','blah 3');
DECLARE @FirstColumn VARCHAR(10)='c3';
SELECT CASE @FirstColumn WHEN 'c1' THEN c1
WHEN 'c2' THEN c2
WHEN 'c3' THEN c3
ELSE NULL END AS DynamicFirstColumn
,p
,a
FROM @mockup;
The idea is to use a CASE
to decide which column is seen in the first place following a parameter. If you want to see a varying column name too (I used DynamicFirstColumn
) you can
- create a VIEW for each case or
- use dynamically created SQL.
As a workaround you can include the handed in parameter and return it with the result set. In this case the consumer would know, which column was used...
SELECT CASE @FirstColumn WHEN 'c1' THEN c1
WHEN 'c2' THEN c2
WHEN 'c3' THEN c3
ELSE NULL END AS DynamicFirstColumn
,p
,a
,@FirstColumn --<-- Here we include the source we use above
FROM @mockup
add a comment |
up vote
0
down vote
I must admit, that this smells a bit. Might be that there are better approaches, but we do not know your use cases.
You can try this:
DECLARE @mockup TABLE(c1 INT, c2 INT, c3 INT, p VARCHAR(100),a VARCHAR(100));
INSERT INTO @mockup VALUES(1,2,3,'Row 1','blah 1')
,(11,22,33,'Row 2','blah 2')
,(111,222,333,'Row 3','blah 3');
DECLARE @FirstColumn VARCHAR(10)='c3';
SELECT CASE @FirstColumn WHEN 'c1' THEN c1
WHEN 'c2' THEN c2
WHEN 'c3' THEN c3
ELSE NULL END AS DynamicFirstColumn
,p
,a
FROM @mockup;
The idea is to use a CASE
to decide which column is seen in the first place following a parameter. If you want to see a varying column name too (I used DynamicFirstColumn
) you can
- create a VIEW for each case or
- use dynamically created SQL.
As a workaround you can include the handed in parameter and return it with the result set. In this case the consumer would know, which column was used...
SELECT CASE @FirstColumn WHEN 'c1' THEN c1
WHEN 'c2' THEN c2
WHEN 'c3' THEN c3
ELSE NULL END AS DynamicFirstColumn
,p
,a
,@FirstColumn --<-- Here we include the source we use above
FROM @mockup
add a comment |
up vote
0
down vote
up vote
0
down vote
I must admit, that this smells a bit. Might be that there are better approaches, but we do not know your use cases.
You can try this:
DECLARE @mockup TABLE(c1 INT, c2 INT, c3 INT, p VARCHAR(100),a VARCHAR(100));
INSERT INTO @mockup VALUES(1,2,3,'Row 1','blah 1')
,(11,22,33,'Row 2','blah 2')
,(111,222,333,'Row 3','blah 3');
DECLARE @FirstColumn VARCHAR(10)='c3';
SELECT CASE @FirstColumn WHEN 'c1' THEN c1
WHEN 'c2' THEN c2
WHEN 'c3' THEN c3
ELSE NULL END AS DynamicFirstColumn
,p
,a
FROM @mockup;
The idea is to use a CASE
to decide which column is seen in the first place following a parameter. If you want to see a varying column name too (I used DynamicFirstColumn
) you can
- create a VIEW for each case or
- use dynamically created SQL.
As a workaround you can include the handed in parameter and return it with the result set. In this case the consumer would know, which column was used...
SELECT CASE @FirstColumn WHEN 'c1' THEN c1
WHEN 'c2' THEN c2
WHEN 'c3' THEN c3
ELSE NULL END AS DynamicFirstColumn
,p
,a
,@FirstColumn --<-- Here we include the source we use above
FROM @mockup
I must admit, that this smells a bit. Might be that there are better approaches, but we do not know your use cases.
You can try this:
DECLARE @mockup TABLE(c1 INT, c2 INT, c3 INT, p VARCHAR(100),a VARCHAR(100));
INSERT INTO @mockup VALUES(1,2,3,'Row 1','blah 1')
,(11,22,33,'Row 2','blah 2')
,(111,222,333,'Row 3','blah 3');
DECLARE @FirstColumn VARCHAR(10)='c3';
SELECT CASE @FirstColumn WHEN 'c1' THEN c1
WHEN 'c2' THEN c2
WHEN 'c3' THEN c3
ELSE NULL END AS DynamicFirstColumn
,p
,a
FROM @mockup;
The idea is to use a CASE
to decide which column is seen in the first place following a parameter. If you want to see a varying column name too (I used DynamicFirstColumn
) you can
- create a VIEW for each case or
- use dynamically created SQL.
As a workaround you can include the handed in parameter and return it with the result set. In this case the consumer would know, which column was used...
SELECT CASE @FirstColumn WHEN 'c1' THEN c1
WHEN 'c2' THEN c2
WHEN 'c3' THEN c3
ELSE NULL END AS DynamicFirstColumn
,p
,a
,@FirstColumn --<-- Here we include the source we use above
FROM @mockup
answered 16 hours ago
Shnugo
47.3k72466
47.3k72466
add a comment |
add a comment |
M. Sol is a new contributor. Be nice, and check out our Code of Conduct.
M. Sol is a new contributor. Be nice, and check out our Code of Conduct.
M. Sol is a new contributor. Be nice, and check out our Code of Conduct.
M. Sol is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53370876%2foptimizing-tsql%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
I don't think there's a way to "parameterise" this. You don't have a
where
clause, so you're getting all records regardless. Since you're getting all records and not ordering / anything special, they'll just be returned from the table itself (i.e. there's no benefit looking for these values in indexes), so this is the best you'll get.– JohnLBevan
19 hours ago
If this is really your query, I doubt you will gain anything IF query caching is possible. The query optimizer takes a decent time when it has to decide one among multiple ways of executing the query; A simple select columns from table has no alternatives, maybe an index, but it won't take too much.
– George Menoutis
18 hours ago
@JohnLBevan I'm pretty sure that the above was shortened for brevity... I take "the rest of the query structure remain the same" as "there is a rather complex query behind"... What you cannot parameterise will be the column's alias, but the content should be rather easy...
– Shnugo
16 hours ago