Kris --

Note that this isn't strictly a perl nor dbi question.  Haing said
that, the most efficient way is probably to calculate the expiration
time local to the perl script, then compare that constant against the
db field.  That is:

|    my $now = time();
|    my $two_weeks_ago = $now - 14*24*60*60; # ignore leap seconds 
|                                            # or use date::calc etc
| 
|    my $two_weeks_ago_iso = strftime '%Y-%m-%d %T', localtime $two_weeks_ago;
| 
|    my $sql = <<SQL;
| SELECT *
|   FROM my_table
|   WHERE my_date_field < '$two_weeks_ago_iso'
| SQL

The reason that this should be more efficient than your proposed:

   WHERE (startdate + $days) < now()

Is that the addition will only be done once.  Further, if there is an
index on startdate, then using it inside a function will sometimes
thwart the optimizer's ability to use that index.  (Although I've
mostly been doing Oracle of late, so other DBs might be smarter or
dumber in this regard.)

Using a bind var (instead of the quoted $two_weeks_ago_iso) might be
even faster, but hopefully the db at hand is smart enough to do the
conversion only once and then compare natively after that.

One other thing to think about is, instead of storing startdate (or
perhaps in addition to that), you could store an "expire date" as
well.  This could be useful for displays to user (your account expires
$whenver), and it could allow for things to expire at different times,
etc.  Just an idea, and a reminder to step back and think outside the
current sql box.

t.

Reply via email to