Distinct on one column but retrieve all
<p>I have got a table with the following syntax:</p>

<pre><code>ID, VALUE, TIMESTAMP
</code></pre>

<p><code>(ID,TIMESTAMP)</code> is primary key, so there might be more than one row for each ID. There are 5000 unique ids.</p>

<p>I want to retrieve the most recent entry for each ID.</p>

<p>My naive way to do was:
 <code>SELECT * FROM table ORDER BY TIMESTAMP DESC LIMIT 5000;</code> </p>

<p>For most cases this will give the correct result, but it is not guaranteed.</p>

<p>Because there might be 100k to 500k entries in the Table I would like to take performance into account.</p>

<p>What do you suggest?</p>