Search code examples
sqlsql-serverjoinviewarchive

Joining a current table to an archived table with exact columns but continuing dates


I split a table with 3 billion records into 2 because of slow response. I don't need data often from before 2015. The Archived table ranges from 2011 to 2014-12-31 and the current table carries from 2015-01-01 onward. I want to create a view that connects both tables so I can:

A. Access data from the past when I need to

B. I don't compromise on performance when I only need current data

C. I can optimize or adjust the view all I want without having to change code in all apps accessing these tables.

Problem: While creating the view I create the join based on the current table and then I connect the archive table with matching fields. That seems straightforward enough except that when I put a date in the where clause that's beyond the current table date the view returns nothing. How can I connect the two tables so the view can access the two tables as if they are the same tables? Thanks in advance. PS.: Please do suggest a better way of what I am trying to achieve if you know so.

Create view XXX....
FROM    [Database].[dbo].[DA] AS DA 
        JOIN [Databse].[dbo].[DA_Archive] AS DA_Arc ON
            DA.TradeDate = DA_Arc.TradeDate 
            AND DA.Node = DA_Arc.Node 
            AND DA.HE = DA_Arc.HE

Solution

  • Try this:

    SELECT * FROM [Database].[dbo].[DA]
    UNION ALL
    SELECT * FROM [Databse].[dbo].[DA_Archive]