Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

finding space used by a query in several tables.

Posted on 2014-04-23
5
Medium Priority
?
308 Views
Last Modified: 2014-05-22
I can generate queries like this:

select * from table1 where PRO_KEY = '326' (PRO_KEY is numeric data type)
select * from table2 where PRO_KEY = '326'
select * from table3 where PRO_KEY = '326'
select * from table4 where PRO_KEY = '326'
select * from table5 where PRO_KEY = '326'
..................................................
select * from table170 where PRO_KEY = '326'

The goal is to find what is the total amount of space (in MB/GB) occupied in the database for PRO_KEY 326 in the entire database for the 170 tables.

for example, table1 size is 1.5 GB, and PRO_KEY = '326' is 0.9 GB and PRO_KEY <> '326' is 0.5GB and PRO_KEY = '326' NULL is 0.1 GB.

(we are only interested in the SUM of all the PRO_KEY = '326' data for the 170 tables)

the 170 tables can be derived this way "select * from sys.columns where name ='pro_key'.

right now i can check the total table size by this way:      

SELECT  
object_name(so.object_id) AS TableName,
CAST(SUM(ps.reserved_page_count) * 8.0 / (1024.) as Decimal(12,0)) AS Size_MB
FROM    
sys.dm_db_partition_stats ps JOIN sys.indexes i
ON i.object_id = ps.object_id AND i.index_id = ps.index_id
JOIN sys.objects so
ON      i.object_id      = so.object_id
WHERE so.type = 'U'
GROUP BY so.object_id
ORDER BY 2 DESC

but does not help me in this quest as there are several pro_key's. The management wants to know how much disk they are going to need if they want to bring another set of pro_key '326' in another environment (these are pretty huge databases)

Thanks.
0
Comment
Question by:25112
  • 2
  • 2
5 Comments
 
LVL 143

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 1332 total points
ID: 40019778
this is not possible, as the used space in databases is only indicated and readable by table, and not by row.
you can only do estimates by comparing the row size for all those rows to the average row size of the table and the row count for the relevant key to the total row count.

a quick estimate would be this simple expression:
SUM [  table size / ( total row count on table ) * (count of rows for the specific key on table ) ]

taking the actual row sizes will be quite complex (average row size is available in the table stats), and will normally not change the above estimation a lot, unless the key has really untypical row sizes
0
 
LVL 5

Author Comment

by:25112
ID: 40020185
>>SUM [  table size / ( total row count on table ) * (count of rows for the specific key on table ) ]
any suggestions how to write this query? all the table size in one temp table? all the table rows in temp table and "count of rows for the specific key on table" in one temp and then join them?

>>average row size is available in the table stats
which stats are you referring to?
0
 
LVL 143

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 1332 total points
ID: 40020532
to get the total rows numbers from table, add this to the above "join":
select object_id, rows from sys.partitions

for the row numbers per "KEY", you have to loop indeed the tables above.
something like his should do:
declare @pro_key varchar(100)
set @pro_key = 326

declare @t table ( tablename sysname  
, Size_MB decimal(12,0) 
, total_rows bigint
, key_rows bigint
)

insert into @t ( tablename, Size_MB, total_rows )
SELECT  object_name(so.object_id) AS TableName
, CAST(SUM(ps.reserved_page_count) * 8.0 / (1024.) as Decimal(12,0)) AS Size_MB
, ( select max( x.rows) from sys.partitions x where x.object_id = so.object_id ) total_rows
FROM  sys.dm_db_partition_stats ps 
JOIN sys.indexes i
  ON i.object_id = ps.object_id
 AND i.index_id = ps.index_id
JOIN sys.objects so
ON      i.object_id      = so.object_id
WHERE so.type = 'U'
GROUP BY so.object_id

declare @sql_template nvarchar(1000)
declare @sql nvarchar(1000)
set @sql_template = 'select @rc = count(*) from [$table$] where PRO_KEY = @pro_key_var '

declare @tablename  sysname
declare @r int 
declare @rc bigint

select @r = count(*) from @t
while @r > 0
begin
  select top 1 @tablename = tablename from @t where key_rows is null
  if @@rowcount > 0
  begin
     set @rc = null
     set @sql = replace(@sql_template, '$table$', @tablename)
     exec sp_executesql @sql, N'@pro_key_var varchar(100), @rc bigint output' , @pro_key, @rc output

      UPDATE @t set  key_rows = isnull(@rc, 0) where tablename = @tablename
  end
  set @r = @r - 1

end

select * from @t

Open in new window

0
 
LVL 70

Accepted Solution

by:
Scott Pletcher earned 668 total points
ID: 40027755
I believe you could get a (really) good estimate by summing up the size of the '326' row columns and the non-'326' row columns, then splitting any remaining table size (which is almost all overhead), proportionally between the two.

If you want code like that, just let me know.  (I'm not posting it immediately because it's involved code, so I don't want to write it unless you really want it / are willing to try it).
0
 
LVL 5

Author Comment

by:25112
ID: 40052568
a3, thanks.. i took your lead and have approx figure.. i appreciate it.. but today i realized we are getting a even (much more) bigger db to analyze.. i have a place to start..

Scott, i was watching the scope of this project, but only today realized there is another project we need to project like these, and that db is even bigger.. so yes, i would appreciate if you could share the logic how you could get more finer details on this.. if you are still available and interested to give this a shot, please let me know.. thanks.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
One of the most important things in an application is the query performance. This article intends to give you good tips to improve the performance of your queries.
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…

782 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question