Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Counts from hundreds of tables

Author  Topic 

carriehoff
Starting Member

29 Posts

Posted - 2011-05-26 : 13:44:36
Hi all,

Here's my situation:
We have about 600 structurally identical tables (I cannot change the setup - it's handled by another department), and I need to produce a report with counts from various fields.

What I'm currently doing is getting a list of the tablenames, and then without using a cursor, but a temporary table, looping through the list running dynamic sql to retrieve the counts:

declare @Column1 int
declare @name varchar(60)
declare @column2 int
Declare @Count int
Declare @SQL VarChar(3000)
Declare @NumberRecords int
Declare @RowCount int

create table #dbs (
RowID int Identity(1,1),
dbname varchar(60),
)

create table #Results (
[DBName] varchar(60),
[Count1] int,
[Count2] int,
[Count3] int,
[Count4] int,
[Count5] int,
[Count6] int,
[Count7] int,
[Count8] int,
[Count9] int,
[Count10] int,
[Count11] int,
[Count12] int,
)

INSERT INTO #dbs
SELECT db
from dblist
SET @NumberRecords = @@ROWCOUNT

SET @RowCount = 1
WHILE @RowCount <= @NumberRecords
BEGIN

SELECT @Name = db from #dbs where rowID = @RowCount

Select @SQL = 'Select CAST(''' + @Name + ''' AS VARCHAR(60)) AS ''Db Name'', count(recordsource) as count1,
SUM(case when field IN (''9'', ''Q'') then 1 else 0 end) as count2 ,
SUM(case when field IN (''B'', ''P'') then 1 else 0 end) as count3 ,
SUM(case when field =''1'' then 1 else 0 end) AS count4,
SUM(case when field =''2'' then 1 else 0 end) AS count5,
SUM(case when field =''4'' then 1 else 0 end) AS count6,
SUM(case when field IN (''6'') then 1 else 0 end) AS count7,
SUM(case when field IN (''7'', ''S'') then 1 else 0 end) AS count8,
SUM(case when field Not In (''Q'',''B'', ''9'',''1'',''2'', ''4'', ''6'', ''P'', ''7'', ''S'') then 1 else 0 end) AS count9,
(Select Count(distinct field2 ) from ' + @name + ' where field2 <> '''') As count10,
(Select Count(distinct field3 ) from ' + @name + ' where source2=1) As count11',
SUM(case when field4=1 then 1 else 0 end) as count12
from ' + @name

Insert #Results
Exec(@SQL)

Set @RowCount = @RowCount + 1

end

go
--select * from #dbs

select * from #Results
drop table #results
drop table #dbs


There's probably about a million records in all of the tables combined. Maybe more . . .
For the entire code block to run in SQL Manager, it takes over 5 min.

I'd like to greatly reduce that time.

I thought about trying to union all of the tables and then doing a group by - but before I trek down that road, I thought I'd ask if anybody else can see a faster way to do what I'm trying to do.

Thank you,
Carrie

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-05-26 : 15:40:37
Check the execution plans for each query that gets run. You'll likely find that you are missing indexes for some of these queries. Add indexes where appropriate, and you should be able to get the time down.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

carriehoff
Starting Member

29 Posts

Posted - 2011-05-26 : 16:34:25
Thank you. As a matter of fact, I asked about that, and no - there weren't indexes on the fields I'm selecting. I'll let you know how things turn out once they get added.

Thank you,
Carrie
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-05-26 : 16:50:08


Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page
   

- Advertisement -