I've got a sqlite table actions
that looks something like this:
uuid varchar (36)
actiondate int
username varchar (16)
mood int
bonus int
status varchar (80)
... bunch of other similar fields (all short varchar or int fields)
This design seems to be sufficiently performant for most types of queries, but struggles a bit with a particular scenario, where I need to get some data about the latest action performed by each user as of a given date.
I was hoping to be able to do something like this:
SELECT status, actiondate
FROM actions WHERE actiondate < 20061231
GROUP BY username
ORDER BY actiondate DESC
However, the aggregation is not done with respect to the order clause, the order clause just determines what order the results are returned in, which makes sense.
So, I have this:
SELECT actiondate, status FROM actions
WHERE actiondate < 20061231 and
uuid = (SELECT uuid from actions as alt
WHERE alt.username = actions.username
ORDER BY actiondate DESC LIMIT 1)
Is there a better way of doing this sort of query? A better table layout? Currently this sort of query is taking ~400ms on my development box, and it'd be nice if I could shave 100ms or so off it (my target time is actually 100ms, but I'm skeptical as to whether that's manageable).
I've obviously got indexes on username and date (I've actually got several: one which is which seems to fit the slow query quite well; one on username; one on date ASC; one on date DESC and one on uuid).
FWIW, the action
table's likely to have somewhere between 100 and 30,000 rows in it.