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?










share|improve this question







New contributor




M. Sol is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
















  • 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















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?










share|improve this question







New contributor




M. Sol is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
















  • 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













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?










share|improve this question







New contributor




M. Sol is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











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






share|improve this question







New contributor




M. Sol is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question







New contributor




M. Sol is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question






New contributor




M. Sol is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 19 hours ago









M. Sol

11




11




New contributor




M. Sol is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





M. Sol is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






M. Sol is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.








  • 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














  • 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








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












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





share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });






    M. Sol is a new contributor. Be nice, and check out our Code of Conduct.










     

    draft saved


    draft discarded


















    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

























    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





    share|improve this answer

























      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





      share|improve this answer























        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





        share|improve this answer












        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






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered 16 hours ago









        Shnugo

        47.3k72466




        47.3k72466






















            M. Sol is a new contributor. Be nice, and check out our Code of Conduct.










             

            draft saved


            draft discarded


















            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.















             


            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            404 Error Contact Form 7 ajax form submitting

            How to know if a Active Directory user can login interactively

            TypeError: fit_transform() missing 1 required positional argument: 'X'