title: What is the best way to store historical data in SQL Server 2005/2008?
body: <p>My simplified and contrived example is the following:-</p>
  
  <p>Lets say that I want to measure and store the temperature (and other values) of all the worlds' towns on a daily basis. I am looking for an optimal way of storing the data so that it is just as easy to get the current temperature in all the towns, as it is to get all the temperature historically in one town.</p>
  
  <p>It is an easy enough problem to solve, but I am looking for the best solution.</p>
  
  <p>The 2 main options I can think of are as follows:-</p>
  
  <h1>Option 1 - Same table stores current and historical records</h1>
  
  <p>Store all the current and archive records in the same table.</p>
  
  <p>i.e.</p>
  
  <pre><code>CREATE TABLE [dbo].[WeatherMeasurement](
    MeasurementID [int] Identity(1,1) NOT Null,
    TownID [int] Not Null,
    Temp [int] NOT Null,
    Date [datetime] NOT Null,
  )
  </code></pre>
  
  <p>This would keep everything simple, but what would be the most efficient query to get a list of towns and there current temperature? Would this scale once the table has millions of rows in? Is there anything to be gained by having some sort of IsCurrent flag in the table?</p>
  
  <h1>Option 2 - Store all archive records in a separate table</h1>
  
  <p>There would be a table to store the current live measurements in</p>
  
  <pre><code>CREATE TABLE [dbo].[WeatherMeasurement](
    MeasurementID [int] Identity(1,1) NOT Null,
    TownID [int] Not Null,
    Temp [int] NOT Null,
    Date [datetime] NOT Null,
  )
  </code></pre>
  
  <p>And a table to store historical archived date (inserted by a trigger perhaps)</p>
  
  <pre><code>CREATE TABLE [dbo].[WeatherMeasurementHistory](
    MeasurementID [int] Identity(1,1) NOT Null,
    TownID [int] Not Null,
    Temp [int] NOT Null,
    Date [datetime] NOT Null,
  )
  </code></pre>
  
  <p>This has the advantages of keeping the main current data lean, and very efficient to query, at the expense of making the schema more complex and inserting data more expensive.</p>
  
  <p>Which is the best option? Are there better options I haven't mentioned?</p>
  
  <p>NOTE: I have simplified the schema to help focus my question better, but assume there will be alot of data inserted each day (100,000s of records), and data is current for one day. The current data is just as likely to be queried as the historical.</p>