Author |
Topic |
JeffK627
Yak Posting Veteran
50 Posts |
Posted - 2010-07-29 : 12:51:40
|
I need to determine whether the record count for a View is > 0. SELECT COUNT(*) takes forever and SELECT rows FROM sysindexes WHERE id = OBJECT_ID('View Name') doesn't work for Views. Does anyone know a fast way to find out if a view is empty or not? I don't even need the actual count, just whether there is at least 1 row. |
|
X002548
Not Just a Number
15586 Posts |
|
JeffK627
Yak Posting Veteran
50 Posts |
Posted - 2010-07-29 : 12:58:00
|
Tried that - it takes as long as COUNT(*). |
 |
|
X002548
Not Just a Number
15586 Posts |
|
JeffK627
Yak Posting Veteran
50 Posts |
Posted - 2010-07-29 : 15:00:13
|
Long as in I went to lunch and it was still running when I came back.I found an alternative though - turns out I can just check for 0 records in one of the tables the view pulls from. |
 |
|
russell
Pyro-ma-ni-yak
5072 Posts |
Posted - 2010-07-29 : 15:39:55
|
that view needs to be optimized |
 |
|
Kristen
Test
22859 Posts |
Posted - 2010-07-30 : 05:56:13
|
... or rebuild indexes / update statistics on the underlying tables (which may be the same thing as Russell said) |
 |
|
X002548
Not Just a Number
15586 Posts |
|
JeffK627
Yak Posting Veteran
50 Posts |
Posted - 2010-07-30 : 09:56:58
|
Unfortunately the tables themselves are not mine to index or otherwise change. But I did find a workaround, so it's OK for now. I'll ask the owner of the tables if he would be willing to rebuild the indexes. |
 |
|
Kristen
Test
22859 Posts |
Posted - 2010-07-30 : 17:07:12
|
"Unfortunately the tables themselves are not mine to index or otherwise change"My suggestion was not to change them, but to check that the Housekeeping is running regularly - if not then all/most queries will be/become slow. |
 |
|
X002548
Not Just a Number
15586 Posts |
|
konark
Yak Posting Veteran
60 Posts |
Posted - 2010-08-02 : 20:35:46
|
Use count(1) instead of count( *)Chandragupta MouryaTakhyashila |
 |
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2010-08-03 : 05:44:14
|
quote: Originally posted by konark Use count(1) instead of count( *)Chandragupta MouryaTakhyashila
I dont think that will be fasterMadhivananFailing to plan is Planning to fail |
 |
|
X002548
Not Just a Number
15586 Posts |
Posted - 2010-08-03 : 09:40:42
|
quote: Originally posted by konark Use count(1) instead of count( *)Chandragupta MouryaTakhyashila
That "Trick" was no longer need back in 7 when the SQL Server "developmwent" team realized they should optimize SELECT COUNT(*)Most the trick was meant for sub queries that used SELECT *...SELECT 1 in 6.5 gave an advatage, because the "Development" team at the time return all the rows to the sub query, when they could NEVER be usedBrett8-)Hint: Want your questions answered fast? Follow the direction in this linkhttp://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspxAdd yourself!http://www.frappr.com/sqlteam |
 |
|
JeffK627
Yak Posting Veteran
50 Posts |
Posted - 2010-08-03 : 10:29:08
|
I discovered that I didn't have to check whether the view itself was empty, I only need to check one of the tables the view uses. That was more than fast enough for my needs. |
 |
|
|