1

I am trying to figure out the size of the payload that gets returned from SQL Server. The code I have is using ADO.NET:

using (var command = new SqlCommand("SELECT * FROM [dbo].[Products]", connection))
{
  SqlDataReader reader = command.ExecuteReader(); 
  // find # of bytes of payload returned

However, I am unsure how to get the size of what got returned from SQL server. Is there a way using the SqlDataReader I can do that?

3
  • In many cases you can't -- because the reader will stream data as it's read. Commented Mar 19, 2014 at 20:56
  • I have never tried this. I would use count(*) instead of select * from products instead. Then, estimate the memory based on the data types of the columns ? Commented Mar 19, 2014 at 20:56
  • It looks like it will help. Thanks! Commented Mar 20, 2014 at 23:02

1 Answer 1

1

Try the query below. Replace the shop by your table name and id by your column name.

DECLARE @maxRowSize int
SET @maxRowSize =
(
SELECT SUM(DataSize) AS MaxRowSize
FROM
(
SELECT COLUMN_NAME, DATALENGTH(COLUMN_NAME) as DataSize
from information_schema.columns 
where table_name = 'shop' -- enter table name only, do NOT enter the schema
) AS SCHEM
)
SELECT COUNT(Id) as NumOfRows, --optional
@maxRowSize as MaxRowBytes, --optional
COUNT(id) * @maxRowSize as MaxQueryBytes
FROM Shop -- Enter schema name if needed
Sign up to request clarification or add additional context in comments.

2 Comments

Another query to get the table info - SELECT column_name as 'Column Name', data_type as 'Data Type', character_maximum_length as 'Max Length' FROM information_schema.columns WHERE table_name = 'shop'
I created a related question here - stackoverflow.com/questions/22518384/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.