Calculating total “on row” bytes for each row … the easy way

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP












2















We want to calculate the total "on row" storage bytes for each row in the table. As we understand it, we must add up the DATALENGTH() of each column while also accounting for NULLs and things like VARCHAR(MAX) which only have a 24-byte pointer "on row". We are aware there is also some overhead for each row which is not accounted for in the query below.



SELECT ROW_ID,

CASE
WHEN COLUMNPROPERTY(OBJECT_ID('EXAMPLE_TABLE'),'COL1','PRECISION') = -1 THEN 24
ELSE ISNULL(DATALENGTH(COL1), 1)
END
+
CASE
WHEN COLUMNPROPERTY(OBJECT_ID('EXAMPLE_TABLE'),'COL2','PRECISION') = -1 THEN 24
ELSE ISNULL(DATALENGTH(COL2), 1)
END
+
CASE
WHEN COLUMNPROPERTY(OBJECT_ID('EXAMPLE_TABLE'),'COL3','PRECISION') = -1 THEN 24
ELSE ISNULL(DATALENGTH(COL3), 1)
END
+
...
...
AS ROW_SIZE

FROM EXAMPLE_TABLE

ORDER BY ROW_SIZE DESC
;


What a beast! And it's only an approximation.



Then we discovered



DBCC SHOWCONTIG ('EXAMPLE_TABLE') WITH TABLERESULTS


which returns MaximumRecordSize. This reveals that there is already an algorithm buried somewhere within SQL Server which is capable of calculating the exact size of a row.



How can we access that algorithm directly?










share|improve this question

















  • 3





    sys.dm_db_index_physical_stats is probably better to try working off of, especially since that DBCC command is deprecated.

    – LowlyDBA
    Feb 21 at 19:47
















2















We want to calculate the total "on row" storage bytes for each row in the table. As we understand it, we must add up the DATALENGTH() of each column while also accounting for NULLs and things like VARCHAR(MAX) which only have a 24-byte pointer "on row". We are aware there is also some overhead for each row which is not accounted for in the query below.



SELECT ROW_ID,

CASE
WHEN COLUMNPROPERTY(OBJECT_ID('EXAMPLE_TABLE'),'COL1','PRECISION') = -1 THEN 24
ELSE ISNULL(DATALENGTH(COL1), 1)
END
+
CASE
WHEN COLUMNPROPERTY(OBJECT_ID('EXAMPLE_TABLE'),'COL2','PRECISION') = -1 THEN 24
ELSE ISNULL(DATALENGTH(COL2), 1)
END
+
CASE
WHEN COLUMNPROPERTY(OBJECT_ID('EXAMPLE_TABLE'),'COL3','PRECISION') = -1 THEN 24
ELSE ISNULL(DATALENGTH(COL3), 1)
END
+
...
...
AS ROW_SIZE

FROM EXAMPLE_TABLE

ORDER BY ROW_SIZE DESC
;


What a beast! And it's only an approximation.



Then we discovered



DBCC SHOWCONTIG ('EXAMPLE_TABLE') WITH TABLERESULTS


which returns MaximumRecordSize. This reveals that there is already an algorithm buried somewhere within SQL Server which is capable of calculating the exact size of a row.



How can we access that algorithm directly?










share|improve this question

















  • 3





    sys.dm_db_index_physical_stats is probably better to try working off of, especially since that DBCC command is deprecated.

    – LowlyDBA
    Feb 21 at 19:47














2












2








2


1






We want to calculate the total "on row" storage bytes for each row in the table. As we understand it, we must add up the DATALENGTH() of each column while also accounting for NULLs and things like VARCHAR(MAX) which only have a 24-byte pointer "on row". We are aware there is also some overhead for each row which is not accounted for in the query below.



SELECT ROW_ID,

CASE
WHEN COLUMNPROPERTY(OBJECT_ID('EXAMPLE_TABLE'),'COL1','PRECISION') = -1 THEN 24
ELSE ISNULL(DATALENGTH(COL1), 1)
END
+
CASE
WHEN COLUMNPROPERTY(OBJECT_ID('EXAMPLE_TABLE'),'COL2','PRECISION') = -1 THEN 24
ELSE ISNULL(DATALENGTH(COL2), 1)
END
+
CASE
WHEN COLUMNPROPERTY(OBJECT_ID('EXAMPLE_TABLE'),'COL3','PRECISION') = -1 THEN 24
ELSE ISNULL(DATALENGTH(COL3), 1)
END
+
...
...
AS ROW_SIZE

FROM EXAMPLE_TABLE

ORDER BY ROW_SIZE DESC
;


What a beast! And it's only an approximation.



Then we discovered



DBCC SHOWCONTIG ('EXAMPLE_TABLE') WITH TABLERESULTS


which returns MaximumRecordSize. This reveals that there is already an algorithm buried somewhere within SQL Server which is capable of calculating the exact size of a row.



How can we access that algorithm directly?










share|improve this question














We want to calculate the total "on row" storage bytes for each row in the table. As we understand it, we must add up the DATALENGTH() of each column while also accounting for NULLs and things like VARCHAR(MAX) which only have a 24-byte pointer "on row". We are aware there is also some overhead for each row which is not accounted for in the query below.



SELECT ROW_ID,

CASE
WHEN COLUMNPROPERTY(OBJECT_ID('EXAMPLE_TABLE'),'COL1','PRECISION') = -1 THEN 24
ELSE ISNULL(DATALENGTH(COL1), 1)
END
+
CASE
WHEN COLUMNPROPERTY(OBJECT_ID('EXAMPLE_TABLE'),'COL2','PRECISION') = -1 THEN 24
ELSE ISNULL(DATALENGTH(COL2), 1)
END
+
CASE
WHEN COLUMNPROPERTY(OBJECT_ID('EXAMPLE_TABLE'),'COL3','PRECISION') = -1 THEN 24
ELSE ISNULL(DATALENGTH(COL3), 1)
END
+
...
...
AS ROW_SIZE

FROM EXAMPLE_TABLE

ORDER BY ROW_SIZE DESC
;


What a beast! And it's only an approximation.



Then we discovered



DBCC SHOWCONTIG ('EXAMPLE_TABLE') WITH TABLERESULTS


which returns MaximumRecordSize. This reveals that there is already an algorithm buried somewhere within SQL Server which is capable of calculating the exact size of a row.



How can we access that algorithm directly?







sql-server sql-server-2017 storage dbcc size






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Feb 21 at 19:38









UnLogicGuysUnLogicGuys

16918




16918







  • 3





    sys.dm_db_index_physical_stats is probably better to try working off of, especially since that DBCC command is deprecated.

    – LowlyDBA
    Feb 21 at 19:47













  • 3





    sys.dm_db_index_physical_stats is probably better to try working off of, especially since that DBCC command is deprecated.

    – LowlyDBA
    Feb 21 at 19:47








3




3





sys.dm_db_index_physical_stats is probably better to try working off of, especially since that DBCC command is deprecated.

– LowlyDBA
Feb 21 at 19:47






sys.dm_db_index_physical_stats is probably better to try working off of, especially since that DBCC command is deprecated.

– LowlyDBA
Feb 21 at 19:47











1 Answer
1






active

oldest

votes


















6















How can we access that algorithm directly?




In so far as the answer to this question, specifically, you can't access it directly. There is nothing where you can say SELECT GetMeMaxRowSize(MyTable, MyPartition, MyIndex).



However, as LowlyDBA has pointed out, you can use sys.dm_db_index_physical_stats to give you more information than ShowContig. The quite interesting thing is that if you run DBCC SHOWCONTIG(), capturing deprecated information, you should see a message to use the aforementioned DMV in place of the ShowContig command.






share|improve this answer























  • That's neat! Any idea if we'll see more explicit nudging around deprecated features, like said message, going forwards?

    – LowlyDBA
    Feb 21 at 20:28











  • @LowlyDBA Unfortunately I do not, but we try to point people in the right direction :) If you think this should be updated or put anywhere in Docs, please let me know.

    – Sean Gallardy
    Feb 21 at 20:38










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%2f230422%2fcalculating-total-on-row-bytes-for-each-row-the-easy-way%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









6















How can we access that algorithm directly?




In so far as the answer to this question, specifically, you can't access it directly. There is nothing where you can say SELECT GetMeMaxRowSize(MyTable, MyPartition, MyIndex).



However, as LowlyDBA has pointed out, you can use sys.dm_db_index_physical_stats to give you more information than ShowContig. The quite interesting thing is that if you run DBCC SHOWCONTIG(), capturing deprecated information, you should see a message to use the aforementioned DMV in place of the ShowContig command.






share|improve this answer























  • That's neat! Any idea if we'll see more explicit nudging around deprecated features, like said message, going forwards?

    – LowlyDBA
    Feb 21 at 20:28











  • @LowlyDBA Unfortunately I do not, but we try to point people in the right direction :) If you think this should be updated or put anywhere in Docs, please let me know.

    – Sean Gallardy
    Feb 21 at 20:38















6















How can we access that algorithm directly?




In so far as the answer to this question, specifically, you can't access it directly. There is nothing where you can say SELECT GetMeMaxRowSize(MyTable, MyPartition, MyIndex).



However, as LowlyDBA has pointed out, you can use sys.dm_db_index_physical_stats to give you more information than ShowContig. The quite interesting thing is that if you run DBCC SHOWCONTIG(), capturing deprecated information, you should see a message to use the aforementioned DMV in place of the ShowContig command.






share|improve this answer























  • That's neat! Any idea if we'll see more explicit nudging around deprecated features, like said message, going forwards?

    – LowlyDBA
    Feb 21 at 20:28











  • @LowlyDBA Unfortunately I do not, but we try to point people in the right direction :) If you think this should be updated or put anywhere in Docs, please let me know.

    – Sean Gallardy
    Feb 21 at 20:38













6












6








6








How can we access that algorithm directly?




In so far as the answer to this question, specifically, you can't access it directly. There is nothing where you can say SELECT GetMeMaxRowSize(MyTable, MyPartition, MyIndex).



However, as LowlyDBA has pointed out, you can use sys.dm_db_index_physical_stats to give you more information than ShowContig. The quite interesting thing is that if you run DBCC SHOWCONTIG(), capturing deprecated information, you should see a message to use the aforementioned DMV in place of the ShowContig command.






share|improve this answer














How can we access that algorithm directly?




In so far as the answer to this question, specifically, you can't access it directly. There is nothing where you can say SELECT GetMeMaxRowSize(MyTable, MyPartition, MyIndex).



However, as LowlyDBA has pointed out, you can use sys.dm_db_index_physical_stats to give you more information than ShowContig. The quite interesting thing is that if you run DBCC SHOWCONTIG(), capturing deprecated information, you should see a message to use the aforementioned DMV in place of the ShowContig command.







share|improve this answer












share|improve this answer



share|improve this answer










answered Feb 21 at 20:01









Sean GallardySean Gallardy

16.8k22654




16.8k22654












  • That's neat! Any idea if we'll see more explicit nudging around deprecated features, like said message, going forwards?

    – LowlyDBA
    Feb 21 at 20:28











  • @LowlyDBA Unfortunately I do not, but we try to point people in the right direction :) If you think this should be updated or put anywhere in Docs, please let me know.

    – Sean Gallardy
    Feb 21 at 20:38

















  • That's neat! Any idea if we'll see more explicit nudging around deprecated features, like said message, going forwards?

    – LowlyDBA
    Feb 21 at 20:28











  • @LowlyDBA Unfortunately I do not, but we try to point people in the right direction :) If you think this should be updated or put anywhere in Docs, please let me know.

    – Sean Gallardy
    Feb 21 at 20:38
















That's neat! Any idea if we'll see more explicit nudging around deprecated features, like said message, going forwards?

– LowlyDBA
Feb 21 at 20:28





That's neat! Any idea if we'll see more explicit nudging around deprecated features, like said message, going forwards?

– LowlyDBA
Feb 21 at 20:28













@LowlyDBA Unfortunately I do not, but we try to point people in the right direction :) If you think this should be updated or put anywhere in Docs, please let me know.

– Sean Gallardy
Feb 21 at 20:38





@LowlyDBA Unfortunately I do not, but we try to point people in the right direction :) If you think this should be updated or put anywhere in Docs, please let me know.

– Sean Gallardy
Feb 21 at 20:38

















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%2f230422%2fcalculating-total-on-row-bytes-for-each-row-the-easy-way%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

How to check contact read email or not when send email to Individual?

Bahrain

Postfix configuration issue with fips on centos 7; mailgun relay