Sunday, February 26, 2012

Get size of a table/query

Is there a way to get the size of a query of a table?

I know I can use DATALENGTH on every column in my query, but I thought there might be an easier way.what exacly do you mean by size?, number of rows?|||see sp_spaceused in BOL|||sp_spaceused will get the size of the table, but I would like to get the size of results from a query. Would I need to create a temp table from my query and then use sp_spaceused to get the size of the temp table?|||I think it is a possibility
Also you can look through the sp_apceused code to see how they do it|||Yes, it does appear to be possible. I wonder though, it seems a lot more "expensive" to create a temporary table and then get the size of it than to use SUM(DATALENGTH(mycolumn)), but I could be wrong. The reason I say that is one of the tables I'm using this on contains image(blob) data which can be very large. If I have to copy all of that data into a temp table, that could be a bit sluggish. Anyone know performance wise which is better to use?|||you are right
it is definitely non performant

why do you want to measure the query ?
may be there is another way to do what you are trying to do ?|||I have a CMS. Multiple clients use the same tables in my CMS but I need to discover how much space each individual client is taking up. The only way to do this that I've found is to measure the size of the query data.|||it is not good way.
You can just have clientid in each row in the tables (and/or separate clients table)
and count rows each client has (anyway counting rows)|||I assume you mean getting the size of the entire table and calculating client size based on their number of rows times and average size/row. I don't think this is an accurate assessment of how much space a client is consuming especially when one of the columns in the table is a blob field. One client could be consuming most of the space in the table because of a huge value for the blob field in a single record. Using the SUM of DATALENGTH of the blob field works for me. I already have each record assigned a clientID so I can get an accurate count of how much space they are taking up. I just thought there might be a better way other than using SUM of DATALENGTH.

No comments:

Post a Comment