Question

I have dates stored as bigint in my table, and I'm trying to select records older than 30 days. I have seen a ton of questions on SO and Google related to this question, but I'm not finding what I need.

Heres what I have, which seems very inefficient:

SELECT
  COUNT(*) 
FROM 
  alert
WHERE 
  ('1969-12-31 19:00:00 GMT'::timestamp + (alert.mytstamp::text)::interval) < (localtimestamp - INTERVAL '30 days')

From what I understand, it is converting the bigint mytstamp field to a timestamp in order to compare it to the "30 days ago" timestamp. It does this for every record in the table :(. It seems more efficient to convert the current time stamp - 30 days to a bigint ONE TIME, then compare that to all my bigint dates.

My SQL skills are weak, so go easy :). Thanks for the help.

Was it helpful?

Solution

Once again I found a solution right after posting to SO. Its a good luck charm I guess. Anyway, this seems to be what I'm looking for, and is much more efficient:

SELECT
  COUNT(*) 
FROM 
  alert
WHERE 
  alert.timestamp < extract('epoch' from (CURRENT_TIMESTAMP  - INTERVAL '10 days'))::bigint

I'm wondering if postgres does the calculation for extract('epoch' from (CURRENT_TIMESTAMP - INTERVAL '10 days'))::bigint once, or for every record comparison though.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top