Solved

finding space used by a query in several tables.

Posted on 2014-04-23
5
295 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 142

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 142

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:
ScottPletcher 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Excel conversion issue with Sql server 14 46
Benefits of SMB Fileshare 3 63
How to use odbc in vb to connect to ms sql 14 34
SQL profiler equivalent in MS-Access 3 42
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…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

911 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now