I have a 5 year data set partitioned into quarterly tables. I also have a master view that joins them all together. When a user needs more than one quarter of data they often use the master view instead of joining multiple quarters together.
My question is, would a table valued function which accepts a date range and returns only the records from the necessary partitions be faster than querying the entire master view?
This is my current view definition:
ALTER VIEW [dbo].[loandetails_test]
AS
SELECT *
FROM loandetails05
where year(date) = 2005
UNION
SELECT *
FROM loandetails06
where year(date) = 2006
UNION
SELECT *
FROM loandetails07
where year(date) = 2007
UNION
SELECT *
FROM loandetails08
where year(date) = 2008
UNION
SELECT *
FROM loandetails1q09
where date >= '1/1/2009' and date < '4/1/2009'
UNION
SELECT *
FROM loandetails2q09
where date >= '4/1/2009' and date < '7/1/2009'
UNION
SELECT *
FROM loandetails3q09
where date >= '7/1/2009' and date < '10/1/2009'
UNION
SELECT *
FROM loandetails4q09
where date >= '10/1/2009' and date < '1/1/2010'
UNION
SELECT *
FROM loandetails1q10
where date >= '1/1/2010' and date < '4/1/2010'
UNION
SELECT *
FROM loandetails2q10
where date >= '4/1/2010' and date < '7/1/2010'
UNION
SELECT *
FROM loandetails3q10
where date >= '7/1/2010' and date < '10/1/2010'
UNION
SELECT *
FROM loandetails4q10
where date >= '10/1/2010' and date < '1/1/2011'
union
SELECT *
FROM loandetails_CURRENT
where date >= '1/1/2011' and date < '4/1/2011'
GO
The answer should be a solid no.
Partitions are set up with implicit criteria, so if you are doing it by date (quarter), SQL Server already knows which partitions will satisfy the query (assuming the query will have a date filter). Check the execution plan which will confirm a stream-merge between two (or as many as involved) partitions.
I have a case where tables from N databases (yes one per silo) are joined in a master view, like you have. The
master viewuses a filter for each one, specifically it looks like thisAny query that asks for
where source in (2,3)automatically recognizes that only 2 dbs need to be searched, and the execution plan reveals as much.If you manually created date-partitioned queries, you can
Here is a working example (even without indexes). Notice that Q1 and Q4 are not even showing in the plan. Disclosure: SQL Server 2008 R2 Express
Query Plan
Re: Updated question
When dealing with date ranges, NEVER (with few exceptions) use a function on the date column. This requires the function to be run against ALL records in the table before comparing to the other side.
Better to write as