Calculating total “on row” bytes for each row … the easy way
Clash Royale CLAN TAG#URR8PPP
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
add a comment |
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
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
add a comment |
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
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
sql-server sql-server-2017 storage dbcc size
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
add a comment |
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
add a comment |
1 Answer
1
active
oldest
votes
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.
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
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%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
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.
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
add a comment |
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.
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
add a comment |
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.
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.
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
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