Solved

finding space used by a query in several tables.

Posted on 2014-04-23
5
302 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 143

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 333 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 333 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 69

Accepted Solution

by:
Scott Pletcher earned 167 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

632 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