I need to find the most recently created record in a table/Rails model, and I'd like to do it in an efficient manner. My primary key is a UUID so using the Rails Samples.last
method won't work. Googling for this, the top Rails answers use a sort ordered in descending order with a limit of 1. Of course this isn't the only way to do it, and when I took databases classes a few years ago we frequently did this sort of thing with a left outer join.
I tried both methods out in SQLServer but I'm not that familiar with explain and can't tell which is better. I tried:
SELECT TOP 1 * FROM samples ORDER BY created_at DESC;
This explain showed a sort using a clustered index scan.
SELECT * FROM samples s1
LEFT OUTER JOIN samples s2 ON s1.created_at < s2.created_at
WHERE s2.user_id IS NULL;
The explain for that one shows a filter with a nested loop using two clustered index scans.
Which of these is better, or are they comparable? Or is there a different method I should be using? The sorting version seems clearer to me, so my inclination is to go for that one (other developers on my team know even less about SQL and databases than I do), but this will be used in large tables so it needs to be efficient.
Also if it's important I used SQLServer for this but I also have MySQL databases so I'm hoping for a database-agnostic answer, if possible.