Solved

finding space used by a query in several tables.

Posted on 2014-04-23
5
293 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
Comment Utility
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
Comment Utility
>>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
Comment Utility
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
Comment Utility
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
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

772 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

11 Experts available now in Live!

Get 1:1 Help Now