dbTalk Databases Forums  

faster count(*) or alternative

comp.databases.ms-sqlserver comp.databases.ms-sqlserver


Discuss faster count(*) or alternative in the comp.databases.ms-sqlserver forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
C10B
 
Posts: n/a

Default faster count(*) or alternative - 02-26-2007 , 03:08 AM






hi,

I have a table with several million rows.
Each row is simply the date and time a certain page was viewed.

eg
page1 1-1-00
page2 2-1-00
page1 16-1-00
page1 17-1-00
page2 19-1-00

I now need to find the most popular pages so I do this...

SELECT TOP 10 COUNT(*) AS mycount FROM tblPageViews
GROUP BY place ORDER BY COUNT(place) DESC

....which gives me the top 10 most viewed pages

big problem - this is getting slower and slower as the table grows and
grows.

what should I do?

is there an alternative?

I think I need all the rows (rather than a simple incrementing
counter) because I might want to say "what was the most popular page
on a certain day or within a certain period"

tia

Tim


Reply With Quote
  #2  
Old   
C10B
 
Posts: n/a

Default Re: faster count(*) or alternative - 02-26-2007 , 03:14 AM






not entirely sure why my browser posted my original message again!
sorry.

thanks guys for your help so far.
yes I have indexes on the columns

erland - I don't quite get what you are doing with creating and
dropping a table and a view?


Tim


Reply With Quote
  #3  
Old   
Erland Sommarskog
 
Posts: n/a

Default Re: faster count(*) or alternative - 02-26-2007 , 04:30 AM



C10B (tswalton (AT) gmail (DOT) com) writes:
Quote:
erland - I don't quite get what you are doing with creating and
dropping a table and a view?
The DROP at the end is just cleanup. The script was meant to show how
you create an indexed view and how to use it.


--
Erland Sommarskog, SQL Server MVP, esquel (AT) sommarskog (DOT) se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx


Reply With Quote
  #4  
Old   
C10B
 
Posts: n/a

Default Re: faster count(*) or alternative - 02-26-2007 , 06:59 AM



I'm still mildly puzzled as to how an indexed view would be quicker to
function than going straight to the table. Surely there's exactly the
same amount of work to be done?
I'll check it out though and let you know if it's any quicker.

Thanks

Tim


Reply With Quote
  #5  
Old   
Greg D. Moore \(Strider\)
 
Posts: n/a

Default Re: faster count(*) or alternative - 02-26-2007 , 08:47 AM



"C10B" <tswalton (AT) gmail (DOT) com> wrote

Quote:
I'm still mildly puzzled as to how an indexed view would be quicker to
function than going straight to the table. Surely there's exactly the
same amount of work to be done?
I'll check it out though and let you know if it's any quicker.

Thanks

Tim


If you want more help, I suggest you give us a complete DDL of your
database, some real example data and perhaps we can do better.



--
Greg Moore
SQL Server DBA Consulting
sql (at) greenms.com http://www.greenms.com




Reply With Quote
  #6  
Old   
C10B
 
Posts: n/a

Default Re: faster count(*) or alternative - 02-26-2007 , 09:04 AM




Quote:
If you want more help, I suggest you give us a complete DDL of your
database, some real example data and perhaps we can do better.

The requirement is simply this...

I have a lot of pages and I want to record when each is visited.
I then need to be able to answer questions like this...

"Which was the most popular page in January 07?"
"Which are the top 10 most viewed pages between 2 and 3 in the
afternoon"

with unlimited segmenting of the data really.

So I thought I would record an entry in a table everytime a page is
viewed, along with the date and time.
Using some simple queries using "count(*)", a where clause and a group
by clause I was answering all the questions.
Trouble is the speed. Several million records (and growing rapidly)
and count(*) gets slow.

Just while I was writing this I had an idea - I could move a finished
month's data to another table, so each month has a table of data. It
might still be a million records though, but at least it wont get
worse and worse over time. This would make some results harder to get
like "most viewed page of all time" but my monthy, hourly, weekly
figures would be quicker.

Any other tips?

Thanks



Reply With Quote
  #7  
Old   
Erland Sommarskog
 
Posts: n/a

Default Re: faster count(*) or alternative - 02-26-2007 , 09:38 AM



C10B (tswalton (AT) gmail (DOT) com) writes:
Quote:
I'm still mildly puzzled as to how an indexed view would be quicker to
function than going straight to the table. Surely there's exactly the
same amount of work to be done?
No. An indexed view is materialised, and this particular view has one
row per webpage with the count of visitors. An alternative would be have
a separate table with the counts, and update that table through a trigger.
But why roll our own when SQL Server can do the job for us?


--
Erland Sommarskog, SQL Server MVP, esquel (AT) sommarskog (DOT) se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx


Reply With Quote
  #8  
Old   
Greg D. Moore \(Strider\)
 
Posts: n/a

Default Re: faster count(*) or alternative - 02-26-2007 , 10:26 AM



"C10B" <tswalton (AT) gmail (DOT) com> wrote

Quote:
If you want more help, I suggest you give us a complete DDL of your
database, some real example data and perhaps we can do better.


The requirement is simply this...

I have a lot of pages and I want to record when each is visited.
I then need to be able to answer questions like this...

Yes, we understand the requirements. That doesn't eliminate the value of a
DDL and sample data.




Quote:
"Which was the most popular page in January 07?"
"Which are the top 10 most viewed pages between 2 and 3 in the
afternoon"

with unlimited segmenting of the data really.

So I thought I would record an entry in a table everytime a page is
viewed, along with the date and time.
Using some simple queries using "count(*)", a where clause and a group
by clause I was answering all the questions.
Trouble is the speed. Several million records (and growing rapidly)
and count(*) gets slow.

Define slow?

(my former employer did queries similar to this in subsecond times.)


Quote:
Just while I was writing this I had an idea - I could move a finished
month's data to another table, so each month has a table of data. It
might still be a million records though, but at least it wont get
worse and worse over time. This would make some results harder to get
like "most viewed page of all time" but my monthy, hourly, weekly
figures would be quicker.

Any other tips?
Yes, post a full DDL.


Quote:
Thanks


--
Greg Moore
SQL Server DBA Consulting
sql (at) greenms.com http://www.greenms.com




Reply With Quote
  #9  
Old   
Erland Sommarskog
 
Posts: n/a

Default Re: faster count(*) or alternative - 02-26-2007 , 04:23 PM



C10B (tswalton (AT) gmail (DOT) com) writes:
Quote:
I have a lot of pages and I want to record when each is visited.
I then need to be able to answer questions like this...

"Which was the most popular page in January 07?"
"Which are the top 10 most viewed pages between 2 and 3 in the
afternoon"

with unlimited segmenting of the data really.

So I thought I would record an entry in a table everytime a page is
viewed, along with the date and time.
Using some simple queries using "count(*)", a where clause and a group
by clause I was answering all the questions.
Trouble is the speed. Several million records (and growing rapidly)
and count(*) gets slow.
You did not say anything about wanting to get the counts for a certain
day, so I interpreted your question that you wanted total counts of
the entire table. Whence my suggestion of an indexed view.

If you are not able to relate clearly what you want to do, then the
answers you get will be equally imprecise.

Quote:
Just while I was writing this I had an idea - I could move a finished
month's data to another table, so each month has a table of data. It
might still be a million records though, but at least it wont get
worse and worse over time. This would make some results harder to get
like "most viewed page of all time" but my monthy, hourly, weekly
figures would be quicker.
There is no need for that. If you always want to run these count
queries for a certain period, make sure that there is a clustered
index on the datetime column in your table.



--
Erland Sommarskog, SQL Server MVP, esquel (AT) sommarskog (DOT) se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.