Home » Performance » MySQL » View today / this week / this month / all time
View today / this week / this month / all time [message #1899] Wed, 19 September 2007 05:01 Go to next message
wesleyb  is currently offline wesleyb
Messages: 1
Registered: September 2007
Junior Member
These different ways of viewing content can be seen more and more on "web 2.0" sites.

An example here:
http://www.shadowness.com/explore/topfavorites

You can show just the items favorited today, this week, this month, or all time.

So what is the best SQL to do this?

I assume I need a separate table for the favorites:

ITEM_FAVORITED
-------------
item_id
user_id
when

Here I will have to do a join + a count of the nr of rows where when < X (today, this week,..)

or

ITEM_FAVORITED
-------------
item_id
today
this_week
this_month
all_time

Problem with this scenario is that everything needs to be recalculated every day.

---

in both cases I would probably have to update the items table as well for easy access:

ITEMS
-----
id
all_time_favorited

Is there any better way? Anyone already implemented this?

Thank you.
Re: View today / this week / this month / all time [message #1911 is a reply to message #1899 ] Wed, 19 September 2007 19:24 Go to previous message
SabreCEO  is currently offline SabreCEO
Messages: 4
Registered: September 2007
Junior Member
ITEM_FAVORITED
-------------
item_id
user_id
when

should work good. You'd probably want a key on item_id

The other option wouldn't work because say you have:

item_id 50
today 35
this_week 250
this_month 1500
all_time 8000

When the next day comes along, you'd want to add today to this_week, and subtract the results from the 8th day back. However, you can't know how many that is. It could be anywhere from 0-250, there is no way to know which counts are from what days. So you'd have to add a when field anyway, or a count for every single day...

-Mark
Previous Topic:Query Optimization (Large Results)
Next Topic:InnoDB insert lock
Goto Forum:
  


Current Time: Fri Jul 10 03:44:25 EDT 2009

Total time taken to generate the page: 0.01081 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 2.7.7.
Copyright ©2001-2007 FUD Forum Bulletin Board Software

MySQL is a trademark of Sun Microsystems.
InnoDB is a trademark of Oracle Corp.

Percona Performance Forums are a service of Percona, Inc.
Not affiliated with Sun Microsystems or Oracle Corp.