|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Counting rows in an SQL tableHi,
I'm trying to count rows in each of the tables in an SQL DB. I tried the following ExecuteScalar command: "Select Count(*) from " & mTableName The problem is that one of the tables is huge and I'm getting a time out. I thought I could do this with a stored procedure using the TableName as a parmeter, but I couldn't get this to work. I thought that I could somehow use the metadata (maybe from GetSchema) to just ask for the row count. I couldn't figure that out either. Can anyone help me out with this? Art Art,
Try the following query. SELECT rowcnt FROM sysindexes WHERE indid <= 1 and [id] = OBJECT_ID('YourTableNameGoesHere') I don't believe the value you get back is guarenteed to be accurate though. You may have to execute DBCC UPDATEUSAGE to get the value to match a SELECT COUNT(*) statement. You'd get a better response if you posted this question in a SQL Server group instead. Brian Art wrote: Show quoteHide quote > Hi, > > I'm trying to count rows in each of the tables in an SQL DB. I tried the > following ExecuteScalar command: > > "Select Count(*) from " & mTableName > > The problem is that one of the tables is huge and I'm getting a time out. > > I thought I could do this with a stored procedure using the TableName as a > parmeter, but I couldn't get this to work. I thought that I could somehow > use the metadata (maybe from GetSchema) to just ask for the row count. I > couldn't figure that out either. > > Can anyone help me out with this? > > Art Brian,
Thanks -- I'll give it a try. The reason I didn't post this in a SQL Server forum is that I thought that the answer might be a .net ADO method. For my current purpose I just need orders of magnitude of the tables, so that if your method gives me close answers, that will be good enough. Thanks again. Show quoteHide quote "Brian Gideon" wrote: > Art, > > Try the following query. > > SELECT > rowcnt > FROM > sysindexes > WHERE > indid <= 1 > and [id] = OBJECT_ID('YourTableNameGoesHere') > > I don't believe the value you get back is guarenteed to be accurate > though. You may have to execute DBCC UPDATEUSAGE to get the value to > match a SELECT COUNT(*) statement. > > You'd get a better response if you posted this question in a SQL Server > group instead. > > Brian > > > Art wrote: > > Hi, > > > > I'm trying to count rows in each of the tables in an SQL DB. I tried the > > following ExecuteScalar command: > > > > "Select Count(*) from " & mTableName > > > > The problem is that one of the tables is huge and I'm getting a time out. > > > > I thought I could do this with a stored procedure using the TableName as a > > parmeter, but I couldn't get this to work. I thought that I could somehow > > use the metadata (maybe from GetSchema) to just ask for the row count. I > > couldn't figure that out either. > > > > Can anyone help me out with this? > > > > Art > > Brian,
I just wanted to let you know that I tried it and it appears to work great! Thanks again. Art Art wrote:
> I just wanted to let you know that I tried it and it appears to work Although you've found a suitable method, it might be worth pointing out that > great! Thanks again. you don't have to retrieve everything from the database to count it the rows: count(1) gives the same answer as count(*). Andrew Andrew,
SQL Server generates the exact same execution plan for either one. At least it did for me and I tried it on a dozen tables each with different kinds of indexes. It was always doing an index or table scan. Brian Andrew Morton wrote: Show quoteHide quote > Although you've found a suitable method, it might be worth pointing out that > you don't have to retrieve everything from the database to count it the > rows: count(1) gives the same answer as count(*). > > Andrew Brian Gideon wrote:
> Andrew, Agreed. I can't remember exactly where I read about using count(1) instead > > SQL Server generates the exact same execution plan for either one. At > least it did for me and I tried it on a dozen tables each with > different kinds of indexes. It was always doing an index or table > scan. of count(*), and I was unfortunate to find it rather than an article saying "don't bother". Apparently it's written in quite a lot of places. I, for one, won't be writing it again. Andrew Andrew Morton wrote:
> Although you've found a suitable method, it might be worth pointing Hmmm... that may not be at all useful. I tried Query Analyzer with both > out that you don't have to retrieve everything from the database to > count it the rows: count(1) gives the same answer as count(*). methods with "set statistics io on" and there didn't seem to be any difference. Andrew
Oracle read only transaction in VB .NET?
Security Exception when deploying a VB.NET 2003 solution. Concurrency question retrieve key from collection making a string var like "nr1 nr2 nr3" windows Registry parameter "any" VB 6 (what is your equivalent in vb 2005 ?) Importing Text File With Diacritical Marks breaking information out of a string Viewing Data in a DataSet during debug |
|||||||||||||||||||||||