1

How do I find the space consumed by specific row in a SQL table.

As an example, consider the following example: There is customer and related orders . There is customer table and order table.

Table : Customer
Columns : Id, Name, Address, Occupation
PK : Id

Table : Order
Columns : OrderId, CustomerId, OrderDescription, OrderCost
PK : OrderId, CustomerId

At runtime there are two customers and one customer has 10 orders and another customer has 100 orders.

I need to write a query where given the customer id, I should return the total bytes consumed by this specific customer in Sql.

Any suggestions or inputs would be greatly helpful.

gbn
  • 422,506
  • 82
  • 585
  • 676
Venki
  • 2,129
  • 6
  • 32
  • 54

1 Answers1

1

You can work out the row length for any row based on these answers:

Then it is a a questions of adding up the bytes per row. The same row structures technique applies to any indexes too.

I'm not writing the full query for you. It is quite straightforward if you know the length of all variable length fields. Note: use DATALENGTH, not LEN because of trailing spaces and ANSI PADDING and you need bytes not characters.

Community
  • 1
  • 1
gbn
  • 422,506
  • 82
  • 585
  • 676