How to get SEEK accessing converted ID via viewIndexing - Uniqueidentifier Foreign Key or Intermediary...

How to efficiently calculate prefix sum of frequencies of characters in a string?

What happened to Rhaegal?

Copy line and insert it in a new position with sed or awk

Is Cola "probably the best-known" Latin word in the world? If not, which might it be?

Remove empty geometries from geodataframe

Identity of Polynomials in positive charcteristic

How do Captain Marvel's powers work in Endgame?

You look catfish vs You look like a catfish?

How did Captain America use this power?

Does what Captain America does at the end of Endgame contradict what we've heard before in the film?

Copying spell into spellbook time required, consecutive or disparate?

Field Length Validation for Desktop Application which has maximum 1000 characters

Why was the pattern string not followed in this code?

How do I tell my manager that he's wrong?

If Melisandre foresaw another character closing blue eyes, why did she follow Stannis?

Can a creature tell when it has been affected by a Divination wizard's Portent?

Floor tile layout process?

Sudo command gets executed as root instead of specified user

How did Arya manage the sneak attack?

Feels like I am getting dragged in office politics

Why is the SNP putting so much emphasis on currency plans?

Game of Life meets Chaos Theory

Loading but not using TikZ changes a file

Method and way to highlight the validation message in the web page by using Selenium IDE



How to get SEEK accessing converted ID via view


Indexing - Uniqueidentifier Foreign Key or Intermediary mapping table?Best approach to have a Live copy of a table in the same databasedeteriorating stored procedure running timesMySQL query taking too longSQL 2005 Unused proceduresWhy does a WHERE clause with an indexed, computed datetime column on an indexed view seek the clustered index on the original tableWhy don't I get an index seek?Investigating errors from strange queryHow do you create a view with SNAPSHOT_MATERIALIZATION in SQL Server 2017?INSERT/SELECT xml column from one table to another






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}







1















Assume I have a table:



-- just for test purposes
CREATE TABLE SomeTable (
ID INT IDENTITY(1,1) NOT NULL CONSTRAINT PK__SomeTable__ID PRIMARY KEY CLUSTERED
,SomeColumn1 NVARCHAR(50) NULL
,SomeColumn2 DATETIME NULL
);

-- populate table with some rows
INSERT INTO SomeTable DEFAULT VALUES;
GO 1000


Because a third-party application there is a view converting ID column of a table from INT to NVARCHAR (assume it's a must):



CREATE VIEW ThirdPartyView AS
SELECT
ID = CAST(ID as NVARCHAR(10))
,C1 = SomeColumn1
,C2 = SomeColumn2
FROM SomeTable;


Then when I access one row by ID, I get an INDEX SCAN:



SELECT *
FROM ThirdPartyView
WHERE ID = N'1'


enter image description here



I understand why.



What can I do to get INDEX SEEK outside of a query?



EDITORIAL / Circumstances:




  • base table (SomeTable) definition could NOT be changed (cannot add columns)

  • view has to have the same columns (cannot add columns)

  • define any indexes is possible

  • I can make the view an indexed view but prefer to avoid that option










share|improve this question































    1















    Assume I have a table:



    -- just for test purposes
    CREATE TABLE SomeTable (
    ID INT IDENTITY(1,1) NOT NULL CONSTRAINT PK__SomeTable__ID PRIMARY KEY CLUSTERED
    ,SomeColumn1 NVARCHAR(50) NULL
    ,SomeColumn2 DATETIME NULL
    );

    -- populate table with some rows
    INSERT INTO SomeTable DEFAULT VALUES;
    GO 1000


    Because a third-party application there is a view converting ID column of a table from INT to NVARCHAR (assume it's a must):



    CREATE VIEW ThirdPartyView AS
    SELECT
    ID = CAST(ID as NVARCHAR(10))
    ,C1 = SomeColumn1
    ,C2 = SomeColumn2
    FROM SomeTable;


    Then when I access one row by ID, I get an INDEX SCAN:



    SELECT *
    FROM ThirdPartyView
    WHERE ID = N'1'


    enter image description here



    I understand why.



    What can I do to get INDEX SEEK outside of a query?



    EDITORIAL / Circumstances:




    • base table (SomeTable) definition could NOT be changed (cannot add columns)

    • view has to have the same columns (cannot add columns)

    • define any indexes is possible

    • I can make the view an indexed view but prefer to avoid that option










    share|improve this question



























      1












      1








      1


      0






      Assume I have a table:



      -- just for test purposes
      CREATE TABLE SomeTable (
      ID INT IDENTITY(1,1) NOT NULL CONSTRAINT PK__SomeTable__ID PRIMARY KEY CLUSTERED
      ,SomeColumn1 NVARCHAR(50) NULL
      ,SomeColumn2 DATETIME NULL
      );

      -- populate table with some rows
      INSERT INTO SomeTable DEFAULT VALUES;
      GO 1000


      Because a third-party application there is a view converting ID column of a table from INT to NVARCHAR (assume it's a must):



      CREATE VIEW ThirdPartyView AS
      SELECT
      ID = CAST(ID as NVARCHAR(10))
      ,C1 = SomeColumn1
      ,C2 = SomeColumn2
      FROM SomeTable;


      Then when I access one row by ID, I get an INDEX SCAN:



      SELECT *
      FROM ThirdPartyView
      WHERE ID = N'1'


      enter image description here



      I understand why.



      What can I do to get INDEX SEEK outside of a query?



      EDITORIAL / Circumstances:




      • base table (SomeTable) definition could NOT be changed (cannot add columns)

      • view has to have the same columns (cannot add columns)

      • define any indexes is possible

      • I can make the view an indexed view but prefer to avoid that option










      share|improve this question
















      Assume I have a table:



      -- just for test purposes
      CREATE TABLE SomeTable (
      ID INT IDENTITY(1,1) NOT NULL CONSTRAINT PK__SomeTable__ID PRIMARY KEY CLUSTERED
      ,SomeColumn1 NVARCHAR(50) NULL
      ,SomeColumn2 DATETIME NULL
      );

      -- populate table with some rows
      INSERT INTO SomeTable DEFAULT VALUES;
      GO 1000


      Because a third-party application there is a view converting ID column of a table from INT to NVARCHAR (assume it's a must):



      CREATE VIEW ThirdPartyView AS
      SELECT
      ID = CAST(ID as NVARCHAR(10))
      ,C1 = SomeColumn1
      ,C2 = SomeColumn2
      FROM SomeTable;


      Then when I access one row by ID, I get an INDEX SCAN:



      SELECT *
      FROM ThirdPartyView
      WHERE ID = N'1'


      enter image description here



      I understand why.



      What can I do to get INDEX SEEK outside of a query?



      EDITORIAL / Circumstances:




      • base table (SomeTable) definition could NOT be changed (cannot add columns)

      • view has to have the same columns (cannot add columns)

      • define any indexes is possible

      • I can make the view an indexed view but prefer to avoid that option







      sql-server query-performance execution-plan






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 1 hour ago







      jerik1

















      asked 2 hours ago









      jerik1jerik1

      1608




      1608






















          2 Answers
          2






          active

          oldest

          votes


















          4














          In general you could create either an indexed view or a new index on the base table referencing a computed column but both seem quite suboptimal compared with removing the CAST from the view so the existing index can be seeked.



          Computed Column



          You could create a computed column on SomeTable with definition CAST(ID as NVARCHAR(10)) and then index that.



          ALTER TABLE SomeTable ADD strID AS CAST(ID as NVARCHAR(10));    

          CREATE INDEX IX ON SomeTable (strID ) INCLUDE (ID, SomeColumn1, SomeColumn2);

          SELECT *
          FROM ThirdPartyView
          WHERE ID = N'1';


          enter image description here



          Indexed view



          Given the restrictions in the question the computed column idea seems ruled out. An alternative will be to create an indexed view but likely you will need to change the query text to get this to work.




          • In all editions except Enterprise Edition the NOEXPAND hint will be needed to get the indexed view to be matched.

          • If you are on Enterprise Edition the automatic matching in principle would work whether you indexed the original view or created a copy...

          • ... but index view matching will only be considered at later stages of optimisation. If the query is cheap enough optimisation will end before it gets to that point. In your case in particular you will also need to fight against trivial plan. I added a million rows of data to the table and the indexed view still wasn't hit - as the plan was below the cost threshold for parallelism and it didn't proceed to further optimisation phases beyond trivial.






          share|improve this answer


























          • Many thanks. May I circumvent NOEXPAND problem with view over view? NOEXPAND clause will be inside "inner" while "outer" view will refer to "inner" one. Will that works? I could test, thought, but you know...it's easier to ask :-)

            – jerik1
            10 mins ago








          • 1





            The NOEXPAND would need to be next to the name of the view that is indexed. So would need to be in the outer view assuming the inner one was the indexed view. But if you can add a wrapper view of a different name I don't understand why this still needs to call the problem view with the CAST

            – Martin Smith
            6 mins ago



















          1














          Why don't you alter the 3rd party view, or rename it and create your own, so that it doesn't perform this silly convert? If you can add an index to the view without breaking your support agreement, surely you can change the definition of the view.



          ALTER VIEW dbo.ThirdPartyView
          AS
          SELECT ID
          ,C1 = SomeColumn1
          ,C2 = SomeColumn2
          FROM SomeTable;


          Thanks to data type precedence rules, your query doesn't have to change to get a seek, but you should change it anyway:



          SELECT ID, C1, C2
          FROM dbo.ThirdPartyView
          WHERE ID = N'1';


          Plan:




          enter image description here







          share|improve this answer
























          • Well, sometimes you just can't change 3rd party SW and their documentation where they rely on pre-defined / kinda "universal" structure and data types of the views.

            – jerik1
            20 mins ago













          • @jerik1 Of course, but adding an indexed view will also require changing the view definition, so... if that was your concern it wouldn't be a consideration in your question. And you pay this 3rd party, right? This is brain-dead basic stuff and they should be giving you some justification for why they are doing this absolutely wrong, or they should fix their folly.

            – Aaron Bertrand
            18 mins ago













          • The definition (SELECT) of the view is in my hands but the structure and data types not. May I circumvent NOEXPAND problém with view over view, thought?

            – jerik1
            14 mins ago











          • @jerik1 If the definition of the view is in your hands, then as my answer suggests, all you have to do is remove the unnecessary CAST. You are overthinking the problem.

            – Aaron Bertrand
            10 mins ago













          • But the other side expects the data type of the ID column is NVARCHAR. I have to meet this conditions, unfortunately. Or I've been told to meet them. Probably their application would fail if it's not. Otherwise I would not ask this question at all.

            – jerik1
            6 mins ago












          Your Answer








          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "182"
          };
          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',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          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
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f236954%2fhow-to-get-seek-accessing-converted-id-via-view%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          4














          In general you could create either an indexed view or a new index on the base table referencing a computed column but both seem quite suboptimal compared with removing the CAST from the view so the existing index can be seeked.



          Computed Column



          You could create a computed column on SomeTable with definition CAST(ID as NVARCHAR(10)) and then index that.



          ALTER TABLE SomeTable ADD strID AS CAST(ID as NVARCHAR(10));    

          CREATE INDEX IX ON SomeTable (strID ) INCLUDE (ID, SomeColumn1, SomeColumn2);

          SELECT *
          FROM ThirdPartyView
          WHERE ID = N'1';


          enter image description here



          Indexed view



          Given the restrictions in the question the computed column idea seems ruled out. An alternative will be to create an indexed view but likely you will need to change the query text to get this to work.




          • In all editions except Enterprise Edition the NOEXPAND hint will be needed to get the indexed view to be matched.

          • If you are on Enterprise Edition the automatic matching in principle would work whether you indexed the original view or created a copy...

          • ... but index view matching will only be considered at later stages of optimisation. If the query is cheap enough optimisation will end before it gets to that point. In your case in particular you will also need to fight against trivial plan. I added a million rows of data to the table and the indexed view still wasn't hit - as the plan was below the cost threshold for parallelism and it didn't proceed to further optimisation phases beyond trivial.






          share|improve this answer


























          • Many thanks. May I circumvent NOEXPAND problem with view over view? NOEXPAND clause will be inside "inner" while "outer" view will refer to "inner" one. Will that works? I could test, thought, but you know...it's easier to ask :-)

            – jerik1
            10 mins ago








          • 1





            The NOEXPAND would need to be next to the name of the view that is indexed. So would need to be in the outer view assuming the inner one was the indexed view. But if you can add a wrapper view of a different name I don't understand why this still needs to call the problem view with the CAST

            – Martin Smith
            6 mins ago
















          4














          In general you could create either an indexed view or a new index on the base table referencing a computed column but both seem quite suboptimal compared with removing the CAST from the view so the existing index can be seeked.



          Computed Column



          You could create a computed column on SomeTable with definition CAST(ID as NVARCHAR(10)) and then index that.



          ALTER TABLE SomeTable ADD strID AS CAST(ID as NVARCHAR(10));    

          CREATE INDEX IX ON SomeTable (strID ) INCLUDE (ID, SomeColumn1, SomeColumn2);

          SELECT *
          FROM ThirdPartyView
          WHERE ID = N'1';


          enter image description here



          Indexed view



          Given the restrictions in the question the computed column idea seems ruled out. An alternative will be to create an indexed view but likely you will need to change the query text to get this to work.




          • In all editions except Enterprise Edition the NOEXPAND hint will be needed to get the indexed view to be matched.

          • If you are on Enterprise Edition the automatic matching in principle would work whether you indexed the original view or created a copy...

          • ... but index view matching will only be considered at later stages of optimisation. If the query is cheap enough optimisation will end before it gets to that point. In your case in particular you will also need to fight against trivial plan. I added a million rows of data to the table and the indexed view still wasn't hit - as the plan was below the cost threshold for parallelism and it didn't proceed to further optimisation phases beyond trivial.






          share|improve this answer


























          • Many thanks. May I circumvent NOEXPAND problem with view over view? NOEXPAND clause will be inside "inner" while "outer" view will refer to "inner" one. Will that works? I could test, thought, but you know...it's easier to ask :-)

            – jerik1
            10 mins ago








          • 1





            The NOEXPAND would need to be next to the name of the view that is indexed. So would need to be in the outer view assuming the inner one was the indexed view. But if you can add a wrapper view of a different name I don't understand why this still needs to call the problem view with the CAST

            – Martin Smith
            6 mins ago














          4












          4








          4







          In general you could create either an indexed view or a new index on the base table referencing a computed column but both seem quite suboptimal compared with removing the CAST from the view so the existing index can be seeked.



          Computed Column



          You could create a computed column on SomeTable with definition CAST(ID as NVARCHAR(10)) and then index that.



          ALTER TABLE SomeTable ADD strID AS CAST(ID as NVARCHAR(10));    

          CREATE INDEX IX ON SomeTable (strID ) INCLUDE (ID, SomeColumn1, SomeColumn2);

          SELECT *
          FROM ThirdPartyView
          WHERE ID = N'1';


          enter image description here



          Indexed view



          Given the restrictions in the question the computed column idea seems ruled out. An alternative will be to create an indexed view but likely you will need to change the query text to get this to work.




          • In all editions except Enterprise Edition the NOEXPAND hint will be needed to get the indexed view to be matched.

          • If you are on Enterprise Edition the automatic matching in principle would work whether you indexed the original view or created a copy...

          • ... but index view matching will only be considered at later stages of optimisation. If the query is cheap enough optimisation will end before it gets to that point. In your case in particular you will also need to fight against trivial plan. I added a million rows of data to the table and the indexed view still wasn't hit - as the plan was below the cost threshold for parallelism and it didn't proceed to further optimisation phases beyond trivial.






          share|improve this answer















          In general you could create either an indexed view or a new index on the base table referencing a computed column but both seem quite suboptimal compared with removing the CAST from the view so the existing index can be seeked.



          Computed Column



          You could create a computed column on SomeTable with definition CAST(ID as NVARCHAR(10)) and then index that.



          ALTER TABLE SomeTable ADD strID AS CAST(ID as NVARCHAR(10));    

          CREATE INDEX IX ON SomeTable (strID ) INCLUDE (ID, SomeColumn1, SomeColumn2);

          SELECT *
          FROM ThirdPartyView
          WHERE ID = N'1';


          enter image description here



          Indexed view



          Given the restrictions in the question the computed column idea seems ruled out. An alternative will be to create an indexed view but likely you will need to change the query text to get this to work.




          • In all editions except Enterprise Edition the NOEXPAND hint will be needed to get the indexed view to be matched.

          • If you are on Enterprise Edition the automatic matching in principle would work whether you indexed the original view or created a copy...

          • ... but index view matching will only be considered at later stages of optimisation. If the query is cheap enough optimisation will end before it gets to that point. In your case in particular you will also need to fight against trivial plan. I added a million rows of data to the table and the indexed view still wasn't hit - as the plan was below the cost threshold for parallelism and it didn't proceed to further optimisation phases beyond trivial.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited 10 mins ago

























          answered 1 hour ago









          Martin SmithMartin Smith

          65k10176262




          65k10176262













          • Many thanks. May I circumvent NOEXPAND problem with view over view? NOEXPAND clause will be inside "inner" while "outer" view will refer to "inner" one. Will that works? I could test, thought, but you know...it's easier to ask :-)

            – jerik1
            10 mins ago








          • 1





            The NOEXPAND would need to be next to the name of the view that is indexed. So would need to be in the outer view assuming the inner one was the indexed view. But if you can add a wrapper view of a different name I don't understand why this still needs to call the problem view with the CAST

            – Martin Smith
            6 mins ago



















          • Many thanks. May I circumvent NOEXPAND problem with view over view? NOEXPAND clause will be inside "inner" while "outer" view will refer to "inner" one. Will that works? I could test, thought, but you know...it's easier to ask :-)

            – jerik1
            10 mins ago








          • 1





            The NOEXPAND would need to be next to the name of the view that is indexed. So would need to be in the outer view assuming the inner one was the indexed view. But if you can add a wrapper view of a different name I don't understand why this still needs to call the problem view with the CAST

            – Martin Smith
            6 mins ago

















          Many thanks. May I circumvent NOEXPAND problem with view over view? NOEXPAND clause will be inside "inner" while "outer" view will refer to "inner" one. Will that works? I could test, thought, but you know...it's easier to ask :-)

          – jerik1
          10 mins ago







          Many thanks. May I circumvent NOEXPAND problem with view over view? NOEXPAND clause will be inside "inner" while "outer" view will refer to "inner" one. Will that works? I could test, thought, but you know...it's easier to ask :-)

          – jerik1
          10 mins ago






          1




          1





          The NOEXPAND would need to be next to the name of the view that is indexed. So would need to be in the outer view assuming the inner one was the indexed view. But if you can add a wrapper view of a different name I don't understand why this still needs to call the problem view with the CAST

          – Martin Smith
          6 mins ago





          The NOEXPAND would need to be next to the name of the view that is indexed. So would need to be in the outer view assuming the inner one was the indexed view. But if you can add a wrapper view of a different name I don't understand why this still needs to call the problem view with the CAST

          – Martin Smith
          6 mins ago













          1














          Why don't you alter the 3rd party view, or rename it and create your own, so that it doesn't perform this silly convert? If you can add an index to the view without breaking your support agreement, surely you can change the definition of the view.



          ALTER VIEW dbo.ThirdPartyView
          AS
          SELECT ID
          ,C1 = SomeColumn1
          ,C2 = SomeColumn2
          FROM SomeTable;


          Thanks to data type precedence rules, your query doesn't have to change to get a seek, but you should change it anyway:



          SELECT ID, C1, C2
          FROM dbo.ThirdPartyView
          WHERE ID = N'1';


          Plan:




          enter image description here







          share|improve this answer
























          • Well, sometimes you just can't change 3rd party SW and their documentation where they rely on pre-defined / kinda "universal" structure and data types of the views.

            – jerik1
            20 mins ago













          • @jerik1 Of course, but adding an indexed view will also require changing the view definition, so... if that was your concern it wouldn't be a consideration in your question. And you pay this 3rd party, right? This is brain-dead basic stuff and they should be giving you some justification for why they are doing this absolutely wrong, or they should fix their folly.

            – Aaron Bertrand
            18 mins ago













          • The definition (SELECT) of the view is in my hands but the structure and data types not. May I circumvent NOEXPAND problém with view over view, thought?

            – jerik1
            14 mins ago











          • @jerik1 If the definition of the view is in your hands, then as my answer suggests, all you have to do is remove the unnecessary CAST. You are overthinking the problem.

            – Aaron Bertrand
            10 mins ago













          • But the other side expects the data type of the ID column is NVARCHAR. I have to meet this conditions, unfortunately. Or I've been told to meet them. Probably their application would fail if it's not. Otherwise I would not ask this question at all.

            – jerik1
            6 mins ago
















          1














          Why don't you alter the 3rd party view, or rename it and create your own, so that it doesn't perform this silly convert? If you can add an index to the view without breaking your support agreement, surely you can change the definition of the view.



          ALTER VIEW dbo.ThirdPartyView
          AS
          SELECT ID
          ,C1 = SomeColumn1
          ,C2 = SomeColumn2
          FROM SomeTable;


          Thanks to data type precedence rules, your query doesn't have to change to get a seek, but you should change it anyway:



          SELECT ID, C1, C2
          FROM dbo.ThirdPartyView
          WHERE ID = N'1';


          Plan:




          enter image description here







          share|improve this answer
























          • Well, sometimes you just can't change 3rd party SW and their documentation where they rely on pre-defined / kinda "universal" structure and data types of the views.

            – jerik1
            20 mins ago













          • @jerik1 Of course, but adding an indexed view will also require changing the view definition, so... if that was your concern it wouldn't be a consideration in your question. And you pay this 3rd party, right? This is brain-dead basic stuff and they should be giving you some justification for why they are doing this absolutely wrong, or they should fix their folly.

            – Aaron Bertrand
            18 mins ago













          • The definition (SELECT) of the view is in my hands but the structure and data types not. May I circumvent NOEXPAND problém with view over view, thought?

            – jerik1
            14 mins ago











          • @jerik1 If the definition of the view is in your hands, then as my answer suggests, all you have to do is remove the unnecessary CAST. You are overthinking the problem.

            – Aaron Bertrand
            10 mins ago













          • But the other side expects the data type of the ID column is NVARCHAR. I have to meet this conditions, unfortunately. Or I've been told to meet them. Probably their application would fail if it's not. Otherwise I would not ask this question at all.

            – jerik1
            6 mins ago














          1












          1








          1







          Why don't you alter the 3rd party view, or rename it and create your own, so that it doesn't perform this silly convert? If you can add an index to the view without breaking your support agreement, surely you can change the definition of the view.



          ALTER VIEW dbo.ThirdPartyView
          AS
          SELECT ID
          ,C1 = SomeColumn1
          ,C2 = SomeColumn2
          FROM SomeTable;


          Thanks to data type precedence rules, your query doesn't have to change to get a seek, but you should change it anyway:



          SELECT ID, C1, C2
          FROM dbo.ThirdPartyView
          WHERE ID = N'1';


          Plan:




          enter image description here







          share|improve this answer













          Why don't you alter the 3rd party view, or rename it and create your own, so that it doesn't perform this silly convert? If you can add an index to the view without breaking your support agreement, surely you can change the definition of the view.



          ALTER VIEW dbo.ThirdPartyView
          AS
          SELECT ID
          ,C1 = SomeColumn1
          ,C2 = SomeColumn2
          FROM SomeTable;


          Thanks to data type precedence rules, your query doesn't have to change to get a seek, but you should change it anyway:



          SELECT ID, C1, C2
          FROM dbo.ThirdPartyView
          WHERE ID = N'1';


          Plan:




          enter image description here








          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered 32 mins ago









          Aaron BertrandAaron Bertrand

          155k18301497




          155k18301497













          • Well, sometimes you just can't change 3rd party SW and their documentation where they rely on pre-defined / kinda "universal" structure and data types of the views.

            – jerik1
            20 mins ago













          • @jerik1 Of course, but adding an indexed view will also require changing the view definition, so... if that was your concern it wouldn't be a consideration in your question. And you pay this 3rd party, right? This is brain-dead basic stuff and they should be giving you some justification for why they are doing this absolutely wrong, or they should fix their folly.

            – Aaron Bertrand
            18 mins ago













          • The definition (SELECT) of the view is in my hands but the structure and data types not. May I circumvent NOEXPAND problém with view over view, thought?

            – jerik1
            14 mins ago











          • @jerik1 If the definition of the view is in your hands, then as my answer suggests, all you have to do is remove the unnecessary CAST. You are overthinking the problem.

            – Aaron Bertrand
            10 mins ago













          • But the other side expects the data type of the ID column is NVARCHAR. I have to meet this conditions, unfortunately. Or I've been told to meet them. Probably their application would fail if it's not. Otherwise I would not ask this question at all.

            – jerik1
            6 mins ago



















          • Well, sometimes you just can't change 3rd party SW and their documentation where they rely on pre-defined / kinda "universal" structure and data types of the views.

            – jerik1
            20 mins ago













          • @jerik1 Of course, but adding an indexed view will also require changing the view definition, so... if that was your concern it wouldn't be a consideration in your question. And you pay this 3rd party, right? This is brain-dead basic stuff and they should be giving you some justification for why they are doing this absolutely wrong, or they should fix their folly.

            – Aaron Bertrand
            18 mins ago













          • The definition (SELECT) of the view is in my hands but the structure and data types not. May I circumvent NOEXPAND problém with view over view, thought?

            – jerik1
            14 mins ago











          • @jerik1 If the definition of the view is in your hands, then as my answer suggests, all you have to do is remove the unnecessary CAST. You are overthinking the problem.

            – Aaron Bertrand
            10 mins ago













          • But the other side expects the data type of the ID column is NVARCHAR. I have to meet this conditions, unfortunately. Or I've been told to meet them. Probably their application would fail if it's not. Otherwise I would not ask this question at all.

            – jerik1
            6 mins ago

















          Well, sometimes you just can't change 3rd party SW and their documentation where they rely on pre-defined / kinda "universal" structure and data types of the views.

          – jerik1
          20 mins ago







          Well, sometimes you just can't change 3rd party SW and their documentation where they rely on pre-defined / kinda "universal" structure and data types of the views.

          – jerik1
          20 mins ago















          @jerik1 Of course, but adding an indexed view will also require changing the view definition, so... if that was your concern it wouldn't be a consideration in your question. And you pay this 3rd party, right? This is brain-dead basic stuff and they should be giving you some justification for why they are doing this absolutely wrong, or they should fix their folly.

          – Aaron Bertrand
          18 mins ago







          @jerik1 Of course, but adding an indexed view will also require changing the view definition, so... if that was your concern it wouldn't be a consideration in your question. And you pay this 3rd party, right? This is brain-dead basic stuff and they should be giving you some justification for why they are doing this absolutely wrong, or they should fix their folly.

          – Aaron Bertrand
          18 mins ago















          The definition (SELECT) of the view is in my hands but the structure and data types not. May I circumvent NOEXPAND problém with view over view, thought?

          – jerik1
          14 mins ago





          The definition (SELECT) of the view is in my hands but the structure and data types not. May I circumvent NOEXPAND problém with view over view, thought?

          – jerik1
          14 mins ago













          @jerik1 If the definition of the view is in your hands, then as my answer suggests, all you have to do is remove the unnecessary CAST. You are overthinking the problem.

          – Aaron Bertrand
          10 mins ago







          @jerik1 If the definition of the view is in your hands, then as my answer suggests, all you have to do is remove the unnecessary CAST. You are overthinking the problem.

          – Aaron Bertrand
          10 mins ago















          But the other side expects the data type of the ID column is NVARCHAR. I have to meet this conditions, unfortunately. Or I've been told to meet them. Probably their application would fail if it's not. Otherwise I would not ask this question at all.

          – jerik1
          6 mins ago





          But the other side expects the data type of the ID column is NVARCHAR. I have to meet this conditions, unfortunately. Or I've been told to meet them. Probably their application would fail if it's not. Otherwise I would not ask this question at all.

          – jerik1
          6 mins ago


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Database Administrators Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f236954%2fhow-to-get-seek-accessing-converted-id-via-view%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

          Gersau Kjelder | Navigasjonsmeny46°59′0″N 8°31′0″E46°59′0″N...

          Nässjö kommun Tettstader | Kjelder | NavigasjonsmenyeVIAFISNIGeoNamesMusicBrainz (area)

          Kvitkval Innhaldsliste Taksonomi og utvikling | Utsjånad og levevis | Utbreiing | Åtferd |...