Drop Table

Support Forum for database administrators and web based access to important newsgroups related to databases
Register on Database Support Forum Edit your profileCalendarFind other Database Support forum membersFrequently Asked QuestionsSearch this forum -> 
For Database admins: Free Database-related Magazines Now Free shipping to Texas


Post New Thread










Thread
Author

Retrieve list of largest tables in a database
Hello,

is there an easy way to retrieve a list of the largest tables in a
given database? I know how to use stored procedures to retrieve the
size of a single table, but I was wondering if there was a query that
could be used to obtain a list of the 20 (for instance) top largest
tables. I know this can be coded on the client side in the application
by retrieving the size of all the tables in a database and then
selecting only the largest n tables, but I was looking for something
more efficient and just a query if possible.

Thanks in advance.


Report this thread to moderator Post Follow-up to this message
Old Post
ozonefilter@gmail.com
12-14-06 12:12 AM


Re: Retrieve list of largest tables in a database
If you don't mind missing out on 100% accuracy,

select top 20  object_name(id),rowc
nt
from sysindexes
where indid in (0,1)
order by rowcnt desc




<ozonefilter@gmail.com> wrote in message
news:1166050186.506361.21040@80g2000cwy.googlegroups.com...
> Hello,
>
> is there an easy way to retrieve a list of the largest tables in a
> given database? I know how to use stored procedures to retrieve the
> size of a single table, but I was wondering if there was a query that
> could be used to obtain a list of the 20 (for instance) top largest
> tables. I know this can be coded on the client side in the application
> by retrieving the size of all the tables in a database and then
> selecting only the largest n tables, but I was looking for something
> more efficient and just a query if possible.
>
> Thanks in advance.
>



Report this thread to moderator Post Follow-up to this message
Old Post
Aaron Bertrand [SQL Server MVP]
12-14-06 12:12 AM


Re: Retrieve list of largest tables in a database
Aaron Bertrand [SQL Server MVP]  wrote:
> If you don't mind missing out on 100% accuracy,
>
> select top 20  object_name(id),rowc
nt
> from sysindexes
> where indid in (0,1)
> order by rowcnt desc

Hi Aaron,

thank you for your answer. If I'm not mistaken, the query above would
only provide me with the list of the tables having the greatest
cardinality. While I do not need 100% accuracy, very large tables (in
bytes) may have a relatively small amount of rows, while smaller tables
may have many tiny records. In fact I've tested it against a real
database and only 40% of the top 20 tables seems to be reported. Any
idea how to get a better approximation?

Thanks.


Report this thread to moderator Post Follow-up to this message
Old Post
ozonefilter@gmail.com
12-14-06 12:12 AM


Re: Retrieve list of largest tables in a database
> thank you for your answer. If I'm not mistaken, the query above  would

> only provide me with the list of the tables having the greatest
> cardinality.

Maybe I'm not sure what you mean by "cardinality" because the query I
provided has nothing to do with cardinality.

I may also be confused by what you mean by "largest".  The query I provided
gives you the top 20 tables in terms of # of rows.  If you want those that
are occupying the largest amount of space, maybe try this:



create table #foo
(
[name] sysname,
rows INT,
reserved nvarchar(32),
data nvarchar(32),
index_size nvarchar(32),
unused nvarchar(32)
);
set nocount on;

exec sp_msForEachTable 'INSERT #foo EXEC sp_spaceused ''?'';';

select top 20 *
from #foo
order by
convert(int, replace(reserved, N' KB', '')) desc;

drop table #foo;



Report this thread to moderator Post Follow-up to this message
Old Post
Aaron Bertrand [SQL Server MVP]
12-14-06 05:12 AM


Re: Retrieve list of largest tables in a database
Take a look at
http://vyaskn.tripod.com/ sp_show_b...bl
es.htm





<ozonefilter@gmail.com> wrote in message
news:1166050186.506361.21040@80g2000cwy.googlegroups.com...
> Hello,
>
> is there an easy way to retrieve a list of the largest tables in a
> given database? I know how to use stored procedures to retrieve the
> size of a single table, but I was wondering if there was a query that
> could be used to obtain a list of the 20 (for instance) top largest
> tables. I know this can be coded on the client side in the application
> by retrieving the size of all the tables in a database and then
> selecting only the largest n tables, but I was looking for something
> more efficient and just a query if possible.
>
> Thanks in advance.
>



Report this thread to moderator Post Follow-up to this message
Old Post
Uri Dimant
12-14-06 05:12 AM


Re: Retrieve list of largest tables in a database
You can use the dpages, used or reserved columns instead of rowcnt, in the s
ysindexes table.

--
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www. solidqualitylearning
.com/


<ozonefilter@gmail.com> wrote in message
news:1166055677.196013.32640@l12g2000cwl.googlegroups.com...
> Aaron Bertrand [SQL Server MVP] wrote: 
>
> Hi Aaron,
>
> thank you for your answer. If I'm not mistaken, the query above would
> only provide me with the list of the tables having the greatest
> cardinality. While I do not need 100% accuracy, very large tables (in
> bytes) may have a relatively small amount of rows, while smaller tables
> may have many tiny records. In fact I've tested it against a real
> database and only 40% of the top 20 tables seems to be reported. Any
> idea how to get a better approximation?
>
> Thanks.
>



Report this thread to moderator Post Follow-up to this message
Old Post
Tibor Karaszi
12-14-06 10:24 AM


Sponsored Links





Last Thread Next Thread
Post New Thread

MS SQL Server archive

Show a Printable Version Email This Page to Someone! Receive updates to this thread
Microsoft SQL Server
Access database support
PostgreSQL Replication
SQL Server ODBC
FoxPro Support
PostgreSQL pgAdmin
SQL Server Clustering
MySQL ODBC
Web Applications with dBASE
SQL Server CE
MySQL++
Sybase Database Support
MS SQL Full Text Search
PostgreSQL Administration
SQL Anywhere support
DB2 UDB Database
Paradox Database Support
Filemaker Database
Berkley DB
SQL 2000/2000i database
ASE Database
Forum Jump:
All times are GMT. The time now is 10:08 PM.

 
Mobile devices forum | Database support forum archive




Copyrights DropTable.com Database Support Forum 2004 - 2006