Calculating total “on row” bytes for each row … the easy waySQL Database size doesn't match with the...

How do I know my password or backup information is not being shared when creating a new wallet?

Coworker asking me to not bring cakes due to self control issue. What should I do?

Empty optional argument or Not giving optional argument at all?

How to achieve physical gender equality?

How can I make my enemies feel real and make combat more engaging?

How to modify 'inter arma enim silent leges' to mean 'in a time of crisis, the law falls silent'?

How can guns be countered by melee combat without raw-ability or exceptional explanations?

Aliased pipeline using head and cut

How can a kingdom keep the secret of a missing monarch from the public?

Is there a way to pause a running process on Linux systems and resume later?

Why do we interpret the accelerated expansion of the universe as the proof for the existence of dark energy?

STM32 PWM problem

Multiplying elements of a list

TikZtree with asymmetric siblings

How can I portray body horror and still be sensitive to people with disabilities?

Using font-relative distances in tikzpictures

Can I legally make a website about boycotting a certain company?

What is formjacking?

Is opening /proc/meminfo faster than reading variable content?

How do I write a maintainable, fast, compile-time bit-mask in C++?

Why is quixotic not Quixotic (a proper adjective)?

How can I use a Module anonymously as the function for /@?

Does the Holy Ark weigh 4 tons?

Multiple null checks in Java 8



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


SQL Database size doesn't match with the total table sizes in the databaseDo you know an easy way to generate one record for each hour of the past 12 hours?Calculate number for each rowRunning total to the previous rowChoosing the right storage block size for sql serverCalculating Running Total After ResetSQL Server 2016 Maximum Bytes/RowWhat's the best way to iterate through the table to update a column in each row?sql server getting total for each groupSQL Server chooses Nested Loop join with dimensional table and make seek for each row













0















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



























    0















    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

























      0












      0








      0








      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














      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












      share










      share



      share










      asked 3 mins ago









      UnLogicGuysUnLogicGuys

      15718




      15718






















          0






          active

          oldest

          votes











          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

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          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

          ORA-01691 (unable to extend lob segment) even though my tablespace has AUTOEXTEND onORA-01692: unable to...

          Always On Availability groups resolving state after failover - Remote harden of transaction...

          Circunscripción electoral de Guipúzcoa Referencias Menú de navegaciónLas claves del sistema electoral en...