sql query to get most recent row - optimized for Vertica DB
Question
I have a table here that has millions of entries. The table stores event and each event has a timestamp. Given some WHERE clause params, I want the most recent event.
This is what we came up with:
SELECT *
FROM eventTable
WHERE timestamp_date >= '2012-07-16' AND
eventType = 1 AND someOtherField = 'value'
ORDER BY timestamp DESC
LIMIT 1;
Currently this is taking way too long to return. Is there a way to alter the SQL to give me what we want a lot faster? Also, is there a projection strategy for things like this?
thanks jose
La solution
Essentially, you want at least one of your projections to be optimized according to the timestamp column in the order by clause. If you do your projections properly, this should be quite fast.
Edit: Just a note of clarification. I didn't mean to imply that the timestamp column should be the only field in the order by clause. Also, as also mentioned in the comments below, a query-specific run on the Database Designer would not be a bad thing in this case. I believe there is an option to optimize for speed. Since you are doing "select *" the output should probably serve as the super and buddy projections.