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...

In reply to Re: Best way to implement a stats counter ("this week" AND "all-time") by isync
in thread Best way to implement a stats counter ("this week" AND "all-time") by isync

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.