Database Research & Development

  • Home
  • NoSQL
    • NoSQL
    • Cassandra
  • Databases
    • Database Theory
    • Database Designing
    • SQL Server Coding Standards
    • SQL Server
    • PostgreSQL
    • MySQL
    • Greenplum
    • Linux
  • Interviews
    • SQL Server Interviews
    • MySQL Interviews
    • SQL Puzzles
  • DBA Scripts
    • SQL Server DBA Scripts
    • PostgreSQL DBA Scripts
    • MySQL DBA Scripts
    • Greenplum DBA Scripts
  • Home
  • Blog Archives !
  • (: Laugh@dbrnd :)
  • Contact Me !
sqlserverinterviews
Home 2017 September SQL Server: Implement Table Partition in Non Enterprise Edition (Use Partitioned View)

SQL Server: Implement Table Partition in Non Enterprise Edition (Use Partitioned View)

This article is half-done without your Comment! *** Please share your thoughts via Comment ***

In this post, I am sharing a full demonstration of the concept of Partitioned view in SQL Server.

Using Partitioned view, you can implement Table Partitioning in the Non-Enterprise Edition of SQL Server because Table Partition is the feature of SQL Server Enterprise Edition.

Many small level companies are searching for this kind of solution, and the concept of Partitioned view is also giving a good performance.

We can also say that this is a manual table partitioning where you must create separate physical tables basis on your require partition ranges and require to apply proper CHECK constraint for validating data ranges.

After this, using one view, you can access all table and accordingly query execution plan also access only require table which reduces the unnecessary load on all or whole table.

Please check the below demonstration and try it your self.

Create a SEQUENCE object:

1
2
3
4
CREATE SEQUENCE dbo.seq_ProductHistory
START WITH 1
INCREMENT BY 1 ;
GO

Create four physical tables:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
CREATE TABLE dbo.tbl_ProductHistory_Q1
(
ProductID INT
,ProductName VARCHAR(20)
,SoldDate DATE
,ProductSoldQuarter TINYINT CHECK (ProductSoldQuarter = 1)
,CONSTRAINT pk_tbl_ProductHistory_Q1_ProductID_ProductSoldQuarter PRIMARY KEY(ProductID, ProductSoldQuarter)
)
GO
 
CREATE TABLE dbo.tbl_ProductHistory_Q2
(
ProductID INT
,ProductName VARCHAR(20)
,SoldDate DATE
,ProductSoldQuarter TINYINT CHECK (ProductSoldQuarter = 2)
,CONSTRAINT pk_tbl_ProductHistory_Q2_ProductID_ProductSoldQuarter PRIMARY KEY(ProductID, ProductSoldQuarter)
)
GO
 
CREATE TABLE dbo.tbl_ProductHistory_Q3
(
ProductID INT
,ProductName VARCHAR(20)
,SoldDate DATE
,ProductSoldQuarter TINYINT CHECK (ProductSoldQuarter = 3)
,CONSTRAINT pk_tbl_ProductHistory_Q3_ProductID_ProductSoldQuarter PRIMARY KEY(ProductID, ProductSoldQuarter)
)
GO
 
CREATE TABLE dbo.tbl_ProductHistory_Q4
(
ProductID INT
,ProductName VARCHAR(20)
,SoldDate DATE
,ProductSoldQuarter TINYINT CHECK (ProductSoldQuarter = 4)
,CONSTRAINT pk_tbl_ProductHistory_Q4_ProductID_ProductSoldQuarter PRIMARY KEY(ProductID, ProductSoldQuarter)
)
GO

Create a Partitioned View:

1
2
3
4
5
6
7
8
9
10
11
CREATE VIEW dbo.vw_ProductHistory
WITH SCHEMABINDING
AS
SELECT ProductID, ProductName, SoldDate, ProductSoldQuarter FROM dbo.tbl_ProductHistory_Q1
UNION ALL
SELECT ProductID, ProductName, SoldDate, ProductSoldQuarter FROM dbo.tbl_ProductHistory_Q2
UNION ALL
SELECT ProductID, ProductName, SoldDate, ProductSoldQuarter FROM dbo.tbl_ProductHistory_Q3
UNION ALL
SELECT ProductID, ProductName, SoldDate, ProductSoldQuarter FROM dbo.tbl_ProductHistory_Q4
GO

Use view and Insert data into four tables:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
INSERT INTO dbo.vw_ProductHistory
VALUES (NEXT VALUE FOR dbo.seq_ProductHistory, 'Computer', '2017-01-08', 1)
GO
 
INSERT INTO dbo.vw_ProductHistory
VALUES (NEXT VALUE FOR dbo.seq_ProductHistory, 'Laptop', '2017-04-14', 2)
GO
 
INSERT INTO dbo.vw_ProductHistory
VALUES (NEXT VALUE FOR dbo.seq_ProductHistory, 'Pendrive', '2017-08-16', 3)
GO
 
INSERT INTO dbo.vw_ProductHistory
VALUES (NEXT VALUE FOR dbo.seq_ProductHistory, 'Mobile', '2017-12-26', 4)
GO

SELECT all records from all four tables via view:
Check the execution plan, where you can see four different access on physical tables.

1
SELECT *FROM dbo.vw_ProductHistory

Result:

1
2
3
4
5
6
ProductID ProductName SoldDate ProductSoldQuarter
----------- -------------------- ---------- ------------------
1 Computer 2017-01-08 1
2 Laptop 2017-04-14 2
3 Pendrive 2017-08-16 3
4 Mobile 2017-12-26 4

Check the execution plan: It is accessing all four physical tables

SELECT few records:
Check the execution plan, where you can find only access of Quarter-4 table because we are selecting data only for it.

1
2
SELECT *FROM dbo.vw_ProductHistory
WHERE ProductSoldQuarter = 4

Result:

1
2
3
ProductID ProductName SoldDate ProductSoldQuarter
----------- -------------------- ---------- ------------------
4 Mobile 2017-12-26 4

Check the execution plan: It is accessing only Quarter-4 table.

Sep 3, 2017Anvesh Patel
SQL Server: Use sp_MSforeachtable to apply a T-SQL command to every table of current DatabaseSQL Server 2016: Use dm_exec_function_stats to check Number of Execution Count of Function
Comments: 4
  1. O Mine
    October 23, 2017 at 6:54 pm

    Is there a way to also implement an “INSTEAD OF INSERT trigger” on the view that eliminates the need to specify “NEXT VALUE FOR dbo.seq_ProductHistory” in the insert statements? Trying to retrofit partitioned view onto a database with a 1TB table (not DB, table) and would like for existing code to not need to change. Existing code thinks of the ID column as an identity and doesn’t specify a value for it….

    ReplyCancel
    • Anvesh Patel
      Anvesh Patel
      October 23, 2017 at 7:23 pm

      Yes possible.
      Please visit this official – https://msdn.microsoft.com/en-us/library/def01zh2.aspx

      ReplyCancel
  2. Kevin
    November 7, 2017 at 11:07 pm

    Anvesh — I have the same question as O_Mine, but the link you provided doesn’t really answer the question. It shows how to put an INSTEAD OF trigger on a view, but it doesn’t provide an illustration involving a SEQUENCE. Inserting the columns from “inserted” plus the NEXT VALUE FOR [sequence] AS [ID_Column] results in 0 rows inserted every time. What are we doing wrong?

    Thanks for a great article, and any help you can provide.

    ReplyCancel
    • Anvesh Patel
      Anvesh Patel
      November 8, 2017 at 6:48 am

      Now, I have to check in detail…

      ReplyCancel

Leave a Reply Cancel reply

CAPTCHA
Refresh

*

Anvesh Patel
Anvesh Patel

Database Engineer

September 3, 2017 4 Comments Database Designing, SQL ServerAnvesh Patel, database, database research and development, dbrnd, Partitioned View, SQL Query, SQL Server, SQL Server Administrator, SQL Server Error, SQL Server Monitoring, SQL Server Performance Tuning, SQL Server Programming, SQL Server Tips and Tricks, Table Partition, table partitioning, TSQL
About Me!

I'm Anvesh Patel, a Database Engineer certified by Oracle and IBM. I'm working as a Database Architect, Database Optimizer, Database Administrator, Database Developer. Providing the best articles and solutions for different problems in the best manner through my blogs is my passion. I have more than six years of experience with various RDBMS products like MSSQL Server, PostgreSQL, MySQL, Greenplum and currently learning and doing research on BIGData and NoSQL technology. -- Hyderabad, India.

About DBRND !

dbrnd

This is a personal blog (www.dbrnd.com).

Any views or opinions represented in this blog are personal and belong solely to the blog owner and do not represent those of people, institutions or organizations that the owner may or may not be associated with in professional or personal capacity, unless explicitly stated.

Feel free to challenge me, disagree with me, or tell me I’m completely nuts in the comments section of each blog entry, but I reserve the right to delete any comment for any reason whatsoever (abusive, profane, rude, or anonymous comments) - so keep it polite.

The content of this website is protected by copyright. No portion of this website may be copied or replicated in any form without the written consent of the website owner.

Recent Comments !
  • Anvesh Patel { Sure will do... } – May 27, 12:43 PM
  • Anvesh Patel { Great... } – May 27, 12:41 PM
  • Anvesh Patel { Great... } – May 27, 12:39 PM
  • Anvesh Patel { Great... } – May 27, 12:36 PM
  • Anvesh Patel { Great... } – May 27, 12:28 PM
  • Anvesh Patel { Great... } – May 27, 12:27 PM
  • Anvesh Patel { Great... } – May 27, 12:16 PM
  • Older »
Follow Me !
  • facebook
  • linkedin
  • twitter
  • youtube
  • google
  • flickr
© 2015 – 2019 All rights reserved. Database Research & Development (dbrnd.com)
Posting....