title: Do I need to have a secondary index to improve a query?
body: <p>Lets say i have the following table</p>
  
  <pre><code>---------------------------------------
   ID   FOREIGN_KEY      TIMESTAMP
  ---------------------------------------
   1          1      2014-01-01 12:00:00
   2          2      2014-01-06 12:00:00
   3          2      2014-01-05 12:00:00
   4          1      2014-01-12 12:00:00
   5          3      2014-01-09 12:00:00
  </code></pre>
  
  <p>I run a query to get <code>FOREIGN_KEY=1</code> and <code>MAX(timestamp)</code>. This will return row with ID = 4 in the above table.</p>
  
  <p>To give some context, there are only ever likely to be max 1000 duplicate foreign keys in the table each with a unique timestamp. There are likely to be many thousands of unique foreign keys on the foreign keys parent table.</p>
  
  <p>My question is, if I have an index on the FOREIGN_KEY, is it worth also having one on the timestamp for this query? </p>
  
  <p>I've assumed that when this query is run, the database first gets all the records matching the foreign key (this should be quick as its indexed) and then just does a linear search on the returned rows to find the max timestamp. Is this correct?</p>
  
  <p>If yes, I see no point in indexing the timestamp column, as a linear search on max 1000 rows shouldn't take too long. Adding an index wouldn't improve the query much, but would consume additional storage.</p>
  
  <p>Are my assumptions correct?</p>