If a VARCHAR(MAX) column is included in an index, is the entire value always stored in the index page(s)?Why does sql server prefer the nonclustered index over the clustered index?varchar performance impactAren't two writes required to update a clustered index recordChanging TEXT to VARCHARUsing wildcards in a like statement on an unindexed VARCHAR(MAX) column with more than 1 million recordsStorage size for varchar length in RedshiftWhy SQL Server has 900 byte index size limitSlow DELETEs of LOB data in SQL ServerHow do I compare large stored procedures?What are the current best practices concerning varchar sizing in SQL Server?Convert varbinary(max) with CONVERT(nvarchar/varchar(max) ,value,0) gives no logic results

How effective are nunchaku as a choking weapon?

Test if two food are the same

SSH From a shared workplace computer

Why is it so hard to land on The Moon?

Water Bottle Rocket Thrust - two calculation methods not matching

A fantasy saga with big bad trees that suddenly appear, and make demons appear

Why didn't Kes send Voyager home?

What is the meaning of "log" in "hours logged"?

What term would be used for words that are borrowed from Japanese and used in other languages?

Why did Batman design Robin's suit with only the underwear without pants?

Installing Proprietary Windows Drivers on Linux

Is sleeping on the ground in cold weather better than on an air mattress?

How can I curtail abuse of the Illusion wizard's Illusory Reality feature?

How to discipline overeager engineer

Should a grammatical article be a part of a web link anchor

Any historical references to links between JOSS and BASIC?

Looking for PC graphics demo software from the early 90s called "Unreal"

Is It Possible to Make a Virus That Acts as an Anti-virus?

Collect results of a map operation in a Map using Collectors.toMap or groupingBy

Does the Creighton Method of Natural Family Planning have a failure rate of 3.2% or less?

What is this cast-iron device on my water supply pipe?

How to execute a project with two resources where you need three resources?

How to make a gift without seeming creepy?

Is any device installed on airplane to measure wind speed relative to the ground, and its direction?



If a VARCHAR(MAX) column is included in an index, is the entire value always stored in the index page(s)?


Why does sql server prefer the nonclustered index over the clustered index?varchar performance impactAren't two writes required to update a clustered index recordChanging TEXT to VARCHARUsing wildcards in a like statement on an unindexed VARCHAR(MAX) column with more than 1 million recordsStorage size for varchar length in RedshiftWhy SQL Server has 900 byte index size limitSlow DELETEs of LOB data in SQL ServerHow do I compare large stored procedures?What are the current best practices concerning varchar sizing in SQL Server?Convert varbinary(max) with CONVERT(nvarchar/varchar(max) ,value,0) gives no logic results






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









11

















I'm asking this out of curiosity, being inspired by this question.



We know that VARCHAR(MAX) values longer than 8000 bytes are not stored in rows, but in separate LOB pages. Subsequently retrieving a row with such value requires two or more logical IO operations (essentially, one more than otherwise would theoretically be necessary).



We can add a VARCHAR(MAX) column, as INCLUDEd, to a unique index, as demonstrated in the linked question. If this column has values that exceed 8000 bytes in length, would such values still be stored "inline" in the index leaf pages, or would they also be moved to LOB pages?










share|improve this question


































    11

















    I'm asking this out of curiosity, being inspired by this question.



    We know that VARCHAR(MAX) values longer than 8000 bytes are not stored in rows, but in separate LOB pages. Subsequently retrieving a row with such value requires two or more logical IO operations (essentially, one more than otherwise would theoretically be necessary).



    We can add a VARCHAR(MAX) column, as INCLUDEd, to a unique index, as demonstrated in the linked question. If this column has values that exceed 8000 bytes in length, would such values still be stored "inline" in the index leaf pages, or would they also be moved to LOB pages?










    share|improve this question






























      11












      11








      11


      2






      I'm asking this out of curiosity, being inspired by this question.



      We know that VARCHAR(MAX) values longer than 8000 bytes are not stored in rows, but in separate LOB pages. Subsequently retrieving a row with such value requires two or more logical IO operations (essentially, one more than otherwise would theoretically be necessary).



      We can add a VARCHAR(MAX) column, as INCLUDEd, to a unique index, as demonstrated in the linked question. If this column has values that exceed 8000 bytes in length, would such values still be stored "inline" in the index leaf pages, or would they also be moved to LOB pages?










      share|improve this question
















      I'm asking this out of curiosity, being inspired by this question.



      We know that VARCHAR(MAX) values longer than 8000 bytes are not stored in rows, but in separate LOB pages. Subsequently retrieving a row with such value requires two or more logical IO operations (essentially, one more than otherwise would theoretically be necessary).



      We can add a VARCHAR(MAX) column, as INCLUDEd, to a unique index, as demonstrated in the linked question. If this column has values that exceed 8000 bytes in length, would such values still be stored "inline" in the index leaf pages, or would they also be moved to LOB pages?







      sql-server varchar






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question



      share|improve this question








      edited Jun 20 at 12:47







      mustaccio

















      asked Apr 17 at 22:51









      mustacciomustaccio

      12.4k9 gold badges30 silver badges46 bronze badges




      12.4k9 gold badges30 silver badges46 bronze badges























          1 Answer
          1






          active

          oldest

          votes


















          15


















          Values that exceed 8000 bytes cannot be stored "inline". They are stored on LOB pages. You can see this with sys.dm_db_index_physical_stats. Start with a simple table:



          USE tempdb;

          DROP TABLE IF EXISTS #LOB_FOR_ME;

          CREATE TABLE #LOB_FOR_ME (
          ID BIGINT,
          MAX_VERNON_WAS_HERE VARCHAR(MAX)
          );

          CREATE INDEX IX ON #LOB_FOR_ME (ID) INCLUDE (MAX_VERNON_WAS_HERE);


          Now insert some rows with values that take 8000 bytes for the VARCHAR(MAX) column and check out the DMF:



          USE tempdb;

          INSERT INTO #LOB_FOR_ME
          SELECT 1, REPLICATE('Z', 8000)
          FROM master..spt_values;

          SELECT index_level, index_type_desc, alloc_unit_type_desc, page_count, record_count
          FROM sys.dm_db_index_physical_stats(DB_ID(), OBJECT_ID('#LOB_FOR_ME'), 2, NULL , 'DETAILED');


          There are no LOB pages in the index:



          ╔═════════════╦════════════════════╦══════════════════════╦════════════╦══════════════╗
          ║ index_level ║ index_type_desc ║ alloc_unit_type_desc ║ page_count ║ record_count ║
          ╠═════════════╬════════════════════╬══════════════════════╬════════════╬══════════════╣
          ║ 0 ║ NONCLUSTERED INDEX ║ IN_ROW_DATA ║ 2540 ║ 2540 ║
          ║ 1 ║ NONCLUSTERED INDEX ║ IN_ROW_DATA ║ 18 ║ 2540 ║
          ║ 2 ║ NONCLUSTERED INDEX ║ IN_ROW_DATA ║ 1 ║ 18 ║
          ╚═════════════╩════════════════════╩══════════════════════╩════════════╩══════════════╝


          But if I add rows with values that take 8001 bytes:



          USE tempdb;

          INSERT INTO #LOB_FOR_ME
          SELECT 2, REPLICATE(CAST('Z' AS VARCHAR(MAX)), 8001)
          FROM master..spt_values;

          SELECT index_level, index_type_desc, alloc_unit_type_desc, page_count, record_count
          FROM sys.dm_db_index_physical_stats(DB_ID(), OBJECT_ID('#LOB_FOR_ME'), 2, NULL , 'DETAILED');


          Now I have 1 LOB page in the index for every row that I just inserted:



          ╔═════════════╦════════════════════╦══════════════════════╦════════════╦══════════════╗
          ║ index_level ║ index_type_desc ║ alloc_unit_type_desc ║ page_count ║ record_count ║
          ╠═════════════╬════════════════════╬══════════════════════╬════════════╬══════════════╣
          ║ 0 ║ NONCLUSTERED INDEX ║ IN_ROW_DATA ║ 2556 ║ 5080 ║
          ║ 1 ║ NONCLUSTERED INDEX ║ IN_ROW_DATA ║ 18 ║ 2556 ║
          ║ 2 ║ NONCLUSTERED INDEX ║ IN_ROW_DATA ║ 1 ║ 18 ║
          ║ 0 ║ NONCLUSTERED INDEX ║ LOB_DATA ║ 2540 ║ 2540 ║
          ╚═════════════╩════════════════════╩══════════════════════╩════════════╩══════════════╝


          You can also see this with SET STATISTICS IO ON; and the right query. Consider the following query that only looks at rows with 8000 bytes:



          SELECT SUM(LEN(MAX_VERNON_WAS_HERE))
          FROM #LOB_FOR_ME
          WHERE ID = 1;


          Results upon executing:




          Scan count 1, logical reads 2560, physical reads 0, read-ahead reads
          0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.




          If I instead query the rows with 8001 bytes:



          SELECT SUM(LEN(MAX_VERNON_WAS_HERE))
          FROM #LOB_FOR_ME
          WHERE ID = 2;


          Now I see lob reads:




          Scan count 1, logical reads 20, physical reads 0, read-ahead reads 0,
          lob logical reads 5080, lob physical reads 0, lob read-ahead reads 0.







          share|improve this answer





























            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/4.0/"u003ecc by-sa 4.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%2f235102%2fif-a-varcharmax-column-is-included-in-an-index-is-the-entire-value-always-sto%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









            15


















            Values that exceed 8000 bytes cannot be stored "inline". They are stored on LOB pages. You can see this with sys.dm_db_index_physical_stats. Start with a simple table:



            USE tempdb;

            DROP TABLE IF EXISTS #LOB_FOR_ME;

            CREATE TABLE #LOB_FOR_ME (
            ID BIGINT,
            MAX_VERNON_WAS_HERE VARCHAR(MAX)
            );

            CREATE INDEX IX ON #LOB_FOR_ME (ID) INCLUDE (MAX_VERNON_WAS_HERE);


            Now insert some rows with values that take 8000 bytes for the VARCHAR(MAX) column and check out the DMF:



            USE tempdb;

            INSERT INTO #LOB_FOR_ME
            SELECT 1, REPLICATE('Z', 8000)
            FROM master..spt_values;

            SELECT index_level, index_type_desc, alloc_unit_type_desc, page_count, record_count
            FROM sys.dm_db_index_physical_stats(DB_ID(), OBJECT_ID('#LOB_FOR_ME'), 2, NULL , 'DETAILED');


            There are no LOB pages in the index:



            ╔═════════════╦════════════════════╦══════════════════════╦════════════╦══════════════╗
            ║ index_level ║ index_type_desc ║ alloc_unit_type_desc ║ page_count ║ record_count ║
            ╠═════════════╬════════════════════╬══════════════════════╬════════════╬══════════════╣
            ║ 0 ║ NONCLUSTERED INDEX ║ IN_ROW_DATA ║ 2540 ║ 2540 ║
            ║ 1 ║ NONCLUSTERED INDEX ║ IN_ROW_DATA ║ 18 ║ 2540 ║
            ║ 2 ║ NONCLUSTERED INDEX ║ IN_ROW_DATA ║ 1 ║ 18 ║
            ╚═════════════╩════════════════════╩══════════════════════╩════════════╩══════════════╝


            But if I add rows with values that take 8001 bytes:



            USE tempdb;

            INSERT INTO #LOB_FOR_ME
            SELECT 2, REPLICATE(CAST('Z' AS VARCHAR(MAX)), 8001)
            FROM master..spt_values;

            SELECT index_level, index_type_desc, alloc_unit_type_desc, page_count, record_count
            FROM sys.dm_db_index_physical_stats(DB_ID(), OBJECT_ID('#LOB_FOR_ME'), 2, NULL , 'DETAILED');


            Now I have 1 LOB page in the index for every row that I just inserted:



            ╔═════════════╦════════════════════╦══════════════════════╦════════════╦══════════════╗
            ║ index_level ║ index_type_desc ║ alloc_unit_type_desc ║ page_count ║ record_count ║
            ╠═════════════╬════════════════════╬══════════════════════╬════════════╬══════════════╣
            ║ 0 ║ NONCLUSTERED INDEX ║ IN_ROW_DATA ║ 2556 ║ 5080 ║
            ║ 1 ║ NONCLUSTERED INDEX ║ IN_ROW_DATA ║ 18 ║ 2556 ║
            ║ 2 ║ NONCLUSTERED INDEX ║ IN_ROW_DATA ║ 1 ║ 18 ║
            ║ 0 ║ NONCLUSTERED INDEX ║ LOB_DATA ║ 2540 ║ 2540 ║
            ╚═════════════╩════════════════════╩══════════════════════╩════════════╩══════════════╝


            You can also see this with SET STATISTICS IO ON; and the right query. Consider the following query that only looks at rows with 8000 bytes:



            SELECT SUM(LEN(MAX_VERNON_WAS_HERE))
            FROM #LOB_FOR_ME
            WHERE ID = 1;


            Results upon executing:




            Scan count 1, logical reads 2560, physical reads 0, read-ahead reads
            0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.




            If I instead query the rows with 8001 bytes:



            SELECT SUM(LEN(MAX_VERNON_WAS_HERE))
            FROM #LOB_FOR_ME
            WHERE ID = 2;


            Now I see lob reads:




            Scan count 1, logical reads 20, physical reads 0, read-ahead reads 0,
            lob logical reads 5080, lob physical reads 0, lob read-ahead reads 0.







            share|improve this answer
































              15


















              Values that exceed 8000 bytes cannot be stored "inline". They are stored on LOB pages. You can see this with sys.dm_db_index_physical_stats. Start with a simple table:



              USE tempdb;

              DROP TABLE IF EXISTS #LOB_FOR_ME;

              CREATE TABLE #LOB_FOR_ME (
              ID BIGINT,
              MAX_VERNON_WAS_HERE VARCHAR(MAX)
              );

              CREATE INDEX IX ON #LOB_FOR_ME (ID) INCLUDE (MAX_VERNON_WAS_HERE);


              Now insert some rows with values that take 8000 bytes for the VARCHAR(MAX) column and check out the DMF:



              USE tempdb;

              INSERT INTO #LOB_FOR_ME
              SELECT 1, REPLICATE('Z', 8000)
              FROM master..spt_values;

              SELECT index_level, index_type_desc, alloc_unit_type_desc, page_count, record_count
              FROM sys.dm_db_index_physical_stats(DB_ID(), OBJECT_ID('#LOB_FOR_ME'), 2, NULL , 'DETAILED');


              There are no LOB pages in the index:



              ╔═════════════╦════════════════════╦══════════════════════╦════════════╦══════════════╗
              ║ index_level ║ index_type_desc ║ alloc_unit_type_desc ║ page_count ║ record_count ║
              ╠═════════════╬════════════════════╬══════════════════════╬════════════╬══════════════╣
              ║ 0 ║ NONCLUSTERED INDEX ║ IN_ROW_DATA ║ 2540 ║ 2540 ║
              ║ 1 ║ NONCLUSTERED INDEX ║ IN_ROW_DATA ║ 18 ║ 2540 ║
              ║ 2 ║ NONCLUSTERED INDEX ║ IN_ROW_DATA ║ 1 ║ 18 ║
              ╚═════════════╩════════════════════╩══════════════════════╩════════════╩══════════════╝


              But if I add rows with values that take 8001 bytes:



              USE tempdb;

              INSERT INTO #LOB_FOR_ME
              SELECT 2, REPLICATE(CAST('Z' AS VARCHAR(MAX)), 8001)
              FROM master..spt_values;

              SELECT index_level, index_type_desc, alloc_unit_type_desc, page_count, record_count
              FROM sys.dm_db_index_physical_stats(DB_ID(), OBJECT_ID('#LOB_FOR_ME'), 2, NULL , 'DETAILED');


              Now I have 1 LOB page in the index for every row that I just inserted:



              ╔═════════════╦════════════════════╦══════════════════════╦════════════╦══════════════╗
              ║ index_level ║ index_type_desc ║ alloc_unit_type_desc ║ page_count ║ record_count ║
              ╠═════════════╬════════════════════╬══════════════════════╬════════════╬══════════════╣
              ║ 0 ║ NONCLUSTERED INDEX ║ IN_ROW_DATA ║ 2556 ║ 5080 ║
              ║ 1 ║ NONCLUSTERED INDEX ║ IN_ROW_DATA ║ 18 ║ 2556 ║
              ║ 2 ║ NONCLUSTERED INDEX ║ IN_ROW_DATA ║ 1 ║ 18 ║
              ║ 0 ║ NONCLUSTERED INDEX ║ LOB_DATA ║ 2540 ║ 2540 ║
              ╚═════════════╩════════════════════╩══════════════════════╩════════════╩══════════════╝


              You can also see this with SET STATISTICS IO ON; and the right query. Consider the following query that only looks at rows with 8000 bytes:



              SELECT SUM(LEN(MAX_VERNON_WAS_HERE))
              FROM #LOB_FOR_ME
              WHERE ID = 1;


              Results upon executing:




              Scan count 1, logical reads 2560, physical reads 0, read-ahead reads
              0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.




              If I instead query the rows with 8001 bytes:



              SELECT SUM(LEN(MAX_VERNON_WAS_HERE))
              FROM #LOB_FOR_ME
              WHERE ID = 2;


              Now I see lob reads:




              Scan count 1, logical reads 20, physical reads 0, read-ahead reads 0,
              lob logical reads 5080, lob physical reads 0, lob read-ahead reads 0.







              share|improve this answer






























                15














                15










                15









                Values that exceed 8000 bytes cannot be stored "inline". They are stored on LOB pages. You can see this with sys.dm_db_index_physical_stats. Start with a simple table:



                USE tempdb;

                DROP TABLE IF EXISTS #LOB_FOR_ME;

                CREATE TABLE #LOB_FOR_ME (
                ID BIGINT,
                MAX_VERNON_WAS_HERE VARCHAR(MAX)
                );

                CREATE INDEX IX ON #LOB_FOR_ME (ID) INCLUDE (MAX_VERNON_WAS_HERE);


                Now insert some rows with values that take 8000 bytes for the VARCHAR(MAX) column and check out the DMF:



                USE tempdb;

                INSERT INTO #LOB_FOR_ME
                SELECT 1, REPLICATE('Z', 8000)
                FROM master..spt_values;

                SELECT index_level, index_type_desc, alloc_unit_type_desc, page_count, record_count
                FROM sys.dm_db_index_physical_stats(DB_ID(), OBJECT_ID('#LOB_FOR_ME'), 2, NULL , 'DETAILED');


                There are no LOB pages in the index:



                ╔═════════════╦════════════════════╦══════════════════════╦════════════╦══════════════╗
                ║ index_level ║ index_type_desc ║ alloc_unit_type_desc ║ page_count ║ record_count ║
                ╠═════════════╬════════════════════╬══════════════════════╬════════════╬══════════════╣
                ║ 0 ║ NONCLUSTERED INDEX ║ IN_ROW_DATA ║ 2540 ║ 2540 ║
                ║ 1 ║ NONCLUSTERED INDEX ║ IN_ROW_DATA ║ 18 ║ 2540 ║
                ║ 2 ║ NONCLUSTERED INDEX ║ IN_ROW_DATA ║ 1 ║ 18 ║
                ╚═════════════╩════════════════════╩══════════════════════╩════════════╩══════════════╝


                But if I add rows with values that take 8001 bytes:



                USE tempdb;

                INSERT INTO #LOB_FOR_ME
                SELECT 2, REPLICATE(CAST('Z' AS VARCHAR(MAX)), 8001)
                FROM master..spt_values;

                SELECT index_level, index_type_desc, alloc_unit_type_desc, page_count, record_count
                FROM sys.dm_db_index_physical_stats(DB_ID(), OBJECT_ID('#LOB_FOR_ME'), 2, NULL , 'DETAILED');


                Now I have 1 LOB page in the index for every row that I just inserted:



                ╔═════════════╦════════════════════╦══════════════════════╦════════════╦══════════════╗
                ║ index_level ║ index_type_desc ║ alloc_unit_type_desc ║ page_count ║ record_count ║
                ╠═════════════╬════════════════════╬══════════════════════╬════════════╬══════════════╣
                ║ 0 ║ NONCLUSTERED INDEX ║ IN_ROW_DATA ║ 2556 ║ 5080 ║
                ║ 1 ║ NONCLUSTERED INDEX ║ IN_ROW_DATA ║ 18 ║ 2556 ║
                ║ 2 ║ NONCLUSTERED INDEX ║ IN_ROW_DATA ║ 1 ║ 18 ║
                ║ 0 ║ NONCLUSTERED INDEX ║ LOB_DATA ║ 2540 ║ 2540 ║
                ╚═════════════╩════════════════════╩══════════════════════╩════════════╩══════════════╝


                You can also see this with SET STATISTICS IO ON; and the right query. Consider the following query that only looks at rows with 8000 bytes:



                SELECT SUM(LEN(MAX_VERNON_WAS_HERE))
                FROM #LOB_FOR_ME
                WHERE ID = 1;


                Results upon executing:




                Scan count 1, logical reads 2560, physical reads 0, read-ahead reads
                0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.




                If I instead query the rows with 8001 bytes:



                SELECT SUM(LEN(MAX_VERNON_WAS_HERE))
                FROM #LOB_FOR_ME
                WHERE ID = 2;


                Now I see lob reads:




                Scan count 1, logical reads 20, physical reads 0, read-ahead reads 0,
                lob logical reads 5080, lob physical reads 0, lob read-ahead reads 0.







                share|improve this answer
















                Values that exceed 8000 bytes cannot be stored "inline". They are stored on LOB pages. You can see this with sys.dm_db_index_physical_stats. Start with a simple table:



                USE tempdb;

                DROP TABLE IF EXISTS #LOB_FOR_ME;

                CREATE TABLE #LOB_FOR_ME (
                ID BIGINT,
                MAX_VERNON_WAS_HERE VARCHAR(MAX)
                );

                CREATE INDEX IX ON #LOB_FOR_ME (ID) INCLUDE (MAX_VERNON_WAS_HERE);


                Now insert some rows with values that take 8000 bytes for the VARCHAR(MAX) column and check out the DMF:



                USE tempdb;

                INSERT INTO #LOB_FOR_ME
                SELECT 1, REPLICATE('Z', 8000)
                FROM master..spt_values;

                SELECT index_level, index_type_desc, alloc_unit_type_desc, page_count, record_count
                FROM sys.dm_db_index_physical_stats(DB_ID(), OBJECT_ID('#LOB_FOR_ME'), 2, NULL , 'DETAILED');


                There are no LOB pages in the index:



                ╔═════════════╦════════════════════╦══════════════════════╦════════════╦══════════════╗
                ║ index_level ║ index_type_desc ║ alloc_unit_type_desc ║ page_count ║ record_count ║
                ╠═════════════╬════════════════════╬══════════════════════╬════════════╬══════════════╣
                ║ 0 ║ NONCLUSTERED INDEX ║ IN_ROW_DATA ║ 2540 ║ 2540 ║
                ║ 1 ║ NONCLUSTERED INDEX ║ IN_ROW_DATA ║ 18 ║ 2540 ║
                ║ 2 ║ NONCLUSTERED INDEX ║ IN_ROW_DATA ║ 1 ║ 18 ║
                ╚═════════════╩════════════════════╩══════════════════════╩════════════╩══════════════╝


                But if I add rows with values that take 8001 bytes:



                USE tempdb;

                INSERT INTO #LOB_FOR_ME
                SELECT 2, REPLICATE(CAST('Z' AS VARCHAR(MAX)), 8001)
                FROM master..spt_values;

                SELECT index_level, index_type_desc, alloc_unit_type_desc, page_count, record_count
                FROM sys.dm_db_index_physical_stats(DB_ID(), OBJECT_ID('#LOB_FOR_ME'), 2, NULL , 'DETAILED');


                Now I have 1 LOB page in the index for every row that I just inserted:



                ╔═════════════╦════════════════════╦══════════════════════╦════════════╦══════════════╗
                ║ index_level ║ index_type_desc ║ alloc_unit_type_desc ║ page_count ║ record_count ║
                ╠═════════════╬════════════════════╬══════════════════════╬════════════╬══════════════╣
                ║ 0 ║ NONCLUSTERED INDEX ║ IN_ROW_DATA ║ 2556 ║ 5080 ║
                ║ 1 ║ NONCLUSTERED INDEX ║ IN_ROW_DATA ║ 18 ║ 2556 ║
                ║ 2 ║ NONCLUSTERED INDEX ║ IN_ROW_DATA ║ 1 ║ 18 ║
                ║ 0 ║ NONCLUSTERED INDEX ║ LOB_DATA ║ 2540 ║ 2540 ║
                ╚═════════════╩════════════════════╩══════════════════════╩════════════╩══════════════╝


                You can also see this with SET STATISTICS IO ON; and the right query. Consider the following query that only looks at rows with 8000 bytes:



                SELECT SUM(LEN(MAX_VERNON_WAS_HERE))
                FROM #LOB_FOR_ME
                WHERE ID = 1;


                Results upon executing:




                Scan count 1, logical reads 2560, physical reads 0, read-ahead reads
                0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.




                If I instead query the rows with 8001 bytes:



                SELECT SUM(LEN(MAX_VERNON_WAS_HERE))
                FROM #LOB_FOR_ME
                WHERE ID = 2;


                Now I see lob reads:




                Scan count 1, logical reads 20, physical reads 0, read-ahead reads 0,
                lob logical reads 5080, lob physical reads 0, lob read-ahead reads 0.








                share|improve this answer















                share|improve this answer




                share|improve this answer



                share|improve this answer








                edited Apr 18 at 16:10

























                answered Apr 17 at 23:35









                Joe ObbishJoe Obbish

                24.6k4 gold badges40 silver badges107 bronze badges




                24.6k4 gold badges40 silver badges107 bronze badges































                    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%2f235102%2fif-a-varcharmax-column-is-included-in-an-index-is-the-entire-value-always-sto%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

                    Tamil (spriik) Luke uk diar | Nawigatjuun

                    Align equal signs while including text over equalitiesAMS align: left aligned text/math plus multicolumn alignmentMultiple alignmentsAligning equations in multiple placesNumbering and aligning an equation with multiple columnsHow to align one equation with another multline equationUsing \ in environments inside the begintabularxNumber equations and preserving alignment of equal signsHow can I align equations to the left and to the right?Double equation alignment problem within align enviromentAligned within align: Why are they right-aligned?

                    Where does the image of a data connector as a sharp metal spike originate from?Where does the concept of infected people turning into zombies only after death originate from?Where does the motif of a reanimated human head originate?Where did the notion that Dragons could speak originate?Where does the archetypal image of the 'Grey' alien come from?Where did the suffix '-Man' originate?Where does the notion of being injured or killed by an illusion originate?Where did the term “sophont” originate?Where does the trope of magic spells being driven by advanced technology originate from?Where did the term “the living impaired” originate?