in reply to Best way to implement a stats counter ("this week" AND "all-time")

Thanks everyone!

UPDATE 2012-08-03
Today, I had to implement this logic and it would be great to get some feedback, as I only got it working in a quite hackish way - especially I couldn't get the shorthand SQL part working so I had to resort to an if/else switch...
## SQL table schema: $sql = "create table if not exists views ( bucket integer, id integer null, views integer DEFAULT (0) );";
sub api_get_views { my $id = shift; my $increment = shift; ## get views so far my $views = database->prepare("SELECT * FROM views WHERE id = ?; " +) or error database->errstr; $views->execute( $id ); my ($today_bucket) = split(/\s/, HTTP::Date::time2iso()); $today_bucket =~ s/-//g; my $cnt=0; my $bucket_exists; while( my $bucket = $views->fetchrow_hashref ){ $cnt += $bucket->{views}; $bucket_exists = 1 if $bucket->{bucket} == $today_bucket; } if($increment){ if($bucket_exists){ my $views = database->prepare("UPDATE views SET views = vi +ews + 1 WHERE bucket = ? AND id = ?; ") or error database->errstr; $views->execute( $today_bucket, $id ); }else{ my $views = database->prepare("INSERT INTO views (bucket,i +d,views) VALUES (?,?,?); ") or error database->errstr; $views->execute( $today_bucket, $id, 1 ); } $cnt++; } return $cnt; }
The code addresses a number of problems/requirements:
  1. Allow us to consolidate the buckets into a bucket "00000000" at any point where we feel we've got too many per-day buckets, possibly only reducing granularity to weeks, then months, etc.
  2. Select views per-object-id. Having the id in the bucket name, possibly as ID-YYYYMMDD, would do the trick db-storage-wise, but wouldn't allow us to extract just views for one id without CPU-intensive REGEX SQL tricks.
  3. The assumption is that looping over a few (even hundred) rows to sum up views isn't too much overhead; while we get the win of keeping granularity high for recent buckets.
  4. The loop allows us to see if we need to UPDATE or INSERT
Would it help to have an INDEX on something? Or would it hurt performance? I'm unsure...