{"id":3227,"date":"2022-04-13T08:55:10","date_gmt":"2022-04-13T01:55:10","guid":{"rendered":"https:\/\/www.sqlservertutorial.net\/?page_id=3227"},"modified":"2022-04-13T09:08:27","modified_gmt":"2022-04-13T02:08:27","slug":"sql-server-table-partitioning","status":"publish","type":"page","link":"https:\/\/www.sqlservertutorial.net\/sql-server-administration\/sql-server-table-partitioning\/","title":{"rendered":"SQL Server Table Partitioning"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you&#8217;ll learn about SQL Server table partitioning and how to create partitioned tables.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='introduction-to-the-sql-server-partitioning'>Introduction to the SQL Server Partitioning <a href=\"#introduction-to-the-sql-server-partitioning\" class=\"anchor\" id=\"introduction-to-the-sql-server-partitioning\" title=\"Anchor for Introduction to the SQL Server Partitioning\">#<\/a><\/h2>\n\n\n\n<p>Table partitioning allows you to store the data of a table in multiple physical sections or partitions. Each partition has the same columns but different set of rows. <\/p>\n\n\n\n<p>In practice, you use table partitioning for large tables. By doing this, you&#8217;ll get the following benefits:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/www.sqlservertutorial.net\/sql-server-administration\/sql-server-backup-types\/\">Back up<\/a> and maintain one or more partitions more quickly.<\/li><li>Transfer or access subsets of data faster and more efficiently while maintaining the integrity of the whole data collection.<\/li><li>May improve query performance.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id='creating-a-partitioned-table'>Creating a partitioned table <a href=\"#creating-a-partitioned-table\" class=\"anchor\" id=\"creating-a-partitioned-table\" title=\"Anchor for Creating a partitioned table\">#<\/a><\/h2>\n\n\n\n<p>To create a partitioned table, you follow these steps:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Create file groups that hold the partitions of the table.<\/li><li>Create a partition function that maps the rows of the table into partitions based on the values of a specified column.<\/li><li>Create a partition scheme that maps the partition table to the new filegroups<\/li><li>Create the table based on the partition scheme.<\/li><\/ul>\n\n\n\n<p>We&#8217;ll use the <code>BikeStores<\/code> <a href=\"https:\/\/www.sqlservertutorial.net\/sql-server-sample-database\/\">sample database<\/a> for the demonstration. The following query returns the order amount by dates and products:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">WITH<\/span> order_data (order_date, product_name, amount)\n<span class=\"hljs-keyword\">AS<\/span> (\n<span class=\"hljs-keyword\">SELECT<\/span>\n  order_date,\n  product_name,\n  <span class=\"hljs-keyword\">SUM<\/span>(i.quantity * i.list_price * (<span class=\"hljs-number\">1<\/span> - discount))\n<span class=\"hljs-keyword\">FROM<\/span> sales.orders o\n<span class=\"hljs-keyword\">INNER<\/span> <span class=\"hljs-keyword\">JOIN<\/span> sales.order_items i\n  <span class=\"hljs-keyword\">ON<\/span> i.order_id = o.order_id\n<span class=\"hljs-keyword\">INNER<\/span> <span class=\"hljs-keyword\">JOIN<\/span> production.products p\n  <span class=\"hljs-keyword\">ON<\/span> p.product_id = i.product_id\n<span class=\"hljs-keyword\">GROUP<\/span> <span class=\"hljs-keyword\">BY<\/span> order_date,\n         product_name\n)\n\n<span class=\"hljs-keyword\">SELECT<\/span> * <span class=\"hljs-keyword\">FROM<\/span> order_data;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"578\" height=\"353\" src=\"https:\/\/www.sqlservertutorial.net\/wp-content\/uploads\/SQL-Server-Table-Partitioning-order-data.png\" alt=\"\" class=\"wp-image-3233\" srcset=\"https:\/\/www.sqlservertutorial.net\/wp-content\/uploads\/SQL-Server-Table-Partitioning-order-data.png 578w, https:\/\/www.sqlservertutorial.net\/wp-content\/uploads\/SQL-Server-Table-Partitioning-order-data-300x183.png 300w\" sizes=\"auto, (max-width: 578px) 100vw, 578px\" \/><\/figure>\n\n\n\n<p>The following returns the summary of the orders:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">WITH<\/span> order_data (order_date, product_name, amount)\n<span class=\"hljs-keyword\">AS<\/span> (<span class=\"hljs-keyword\">SELECT<\/span>\n  order_date,\n  product_name,\n  <span class=\"hljs-keyword\">SUM<\/span>(i.quantity * i.list_price * (<span class=\"hljs-number\">1<\/span> - discount))\n<span class=\"hljs-keyword\">FROM<\/span> sales.orders o\n<span class=\"hljs-keyword\">INNER<\/span> <span class=\"hljs-keyword\">JOIN<\/span> sales.order_items i\n  <span class=\"hljs-keyword\">ON<\/span> i.order_id = o.order_id\n<span class=\"hljs-keyword\">INNER<\/span> <span class=\"hljs-keyword\">JOIN<\/span> production.products p\n  <span class=\"hljs-keyword\">ON<\/span> p.product_id = i.product_id\n<span class=\"hljs-keyword\">GROUP<\/span> <span class=\"hljs-keyword\">BY<\/span> order_date,\n         product_name)\n\n<span class=\"hljs-keyword\">SELECT<\/span>\n  <span class=\"hljs-keyword\">YEAR<\/span>(order_date) <span class=\"hljs-keyword\">year<\/span>,\n  <span class=\"hljs-keyword\">COUNT<\/span>(*) <span class=\"hljs-keyword\">row_count<\/span>\n<span class=\"hljs-keyword\">FROM<\/span> order_data\n<span class=\"hljs-keyword\">GROUP<\/span> <span class=\"hljs-keyword\">BY<\/span> <span class=\"hljs-keyword\">YEAR<\/span>(order_date);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"165\" height=\"93\" src=\"https:\/\/www.sqlservertutorial.net\/wp-content\/uploads\/SQL-Server-Table-Partitioning-year-and-row-count.png\" alt=\"\" class=\"wp-image-3234\"\/><\/figure>\n\n\n\n<p>We&#8217;ll create a partitioned table called <code>sales.order_reports<\/code> that stores the order data returned by the above query. The <code>sales.order_reports<\/code> table will have three partitions. And each partition will store rows whose order dates will be in 2016, 2017, and 2018.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id='1-creating-file-groups'>1) Creating file groups <a href=\"#1-creating-file-groups\" class=\"anchor\" id=\"1-creating-file-groups\" title=\"Anchor for 1) Creating file groups\">#<\/a><\/h3>\n\n\n\n<p>When creating a database, SQL Server creates at least two files: a data file and a log file:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>The data file contains data and objects like tables, indexes, and views.<\/li><li>The log file contains the information for recovering the transactions in the database.<\/li><\/ul>\n\n\n\n<p>SQL Server allows you to store the data in multiple data files and uses the filegroup to group data files. By default, the data file belongs to the <code>PRIMARY<\/code> filegroup.<\/p>\n\n\n\n<p>To add more filegroups to a database, you use the <code>ALTER DATABASE ... ADD FILEGROUP<\/code> statement. <\/p>\n\n\n\n<p>First, add three filegroups to the <code>BikeStores<\/code> database:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">ALTER<\/span> <span class=\"hljs-keyword\">DATABASE<\/span> bikestores\n<span class=\"hljs-keyword\">ADD<\/span> FILEGROUP orders_2016;\n\n<span class=\"hljs-keyword\">ALTER<\/span> <span class=\"hljs-keyword\">DATABASE<\/span> bikestores\n<span class=\"hljs-keyword\">ADD<\/span> FILEGROUP orders_2017;\n\n<span class=\"hljs-keyword\">ALTER<\/span> <span class=\"hljs-keyword\">DATABASE<\/span> bikestores\n<span class=\"hljs-keyword\">ADD<\/span> FILEGROUP orders_2018;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Second, verify the filegroups of the current database by using the following statement:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span>\n  <span class=\"hljs-keyword\">name<\/span>\n<span class=\"hljs-keyword\">FROM<\/span> sys.filegroups\n<span class=\"hljs-keyword\">WHERE<\/span> <span class=\"hljs-keyword\">type<\/span> = <span class=\"hljs-string\">'FG'<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"144\" height=\"127\" src=\"https:\/\/www.sqlservertutorial.net\/wp-content\/uploads\/SQL-Server-Table-Partitioning-Filegroups.png\" alt=\"\" class=\"wp-image-3235\"\/><\/figure>\n\n\n\n<p>Third, assign physical files to the filegroups:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">ALTER<\/span> <span class=\"hljs-keyword\">DATABASE<\/span> bikestores    \n<span class=\"hljs-keyword\">ADD<\/span> <span class=\"hljs-keyword\">FILE<\/span>     (\n    <span class=\"hljs-keyword\">NAME<\/span> = orders_2016,\n    FILENAME = <span class=\"hljs-string\">'D:\\data\\orders_2016.ndf'<\/span>,\n        <span class=\"hljs-keyword\">SIZE<\/span> = <span class=\"hljs-number\">10<\/span> MB, \n        <span class=\"hljs-keyword\">MAXSIZE<\/span> = <span class=\"hljs-keyword\">UNLIMITED<\/span>, \n        FILEGROWTH = <span class=\"hljs-number\">1024<\/span> KB\n    ) <span class=\"hljs-keyword\">TO<\/span> FILEGROUP orders_2016;\n\n<span class=\"hljs-keyword\">ALTER<\/span> <span class=\"hljs-keyword\">DATABASE<\/span> bikestores    \n<span class=\"hljs-keyword\">ADD<\/span> <span class=\"hljs-keyword\">FILE<\/span>     (\n    <span class=\"hljs-keyword\">NAME<\/span> = orders_2017,\n    FILENAME = <span class=\"hljs-string\">'D:\\data\\orders_2017.ndf'<\/span>,\n        <span class=\"hljs-keyword\">SIZE<\/span> = <span class=\"hljs-number\">10<\/span> MB, \n        <span class=\"hljs-keyword\">MAXSIZE<\/span> = <span class=\"hljs-keyword\">UNLIMITED<\/span>, \n        FILEGROWTH = <span class=\"hljs-number\">1024<\/span> KB\n    ) <span class=\"hljs-keyword\">TO<\/span> FILEGROUP orders_2017;\n\n<span class=\"hljs-keyword\">ALTER<\/span> <span class=\"hljs-keyword\">DATABASE<\/span> bikestores    \n<span class=\"hljs-keyword\">ADD<\/span> <span class=\"hljs-keyword\">FILE<\/span>     (\n    <span class=\"hljs-keyword\">NAME<\/span> = orders_2018,\n    FILENAME = <span class=\"hljs-string\">'D:\\data\\orders_2018.ndf'<\/span>,\n        <span class=\"hljs-keyword\">SIZE<\/span> = <span class=\"hljs-number\">10<\/span> MB, \n        <span class=\"hljs-keyword\">MAXSIZE<\/span> = <span class=\"hljs-keyword\">UNLIMITED<\/span>, \n        FILEGROWTH = <span class=\"hljs-number\">1024<\/span> KB\n    ) <span class=\"hljs-keyword\">TO<\/span> FILEGROUP orders_2018;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this example, we assign three files located in the D:\\data to the filegroups. <\/p>\n\n\n\n<p>Finally, verify the filegroup assignment using the following statement:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span> \n\t<span class=\"hljs-keyword\">name<\/span> <span class=\"hljs-keyword\">as<\/span> filename,\n\tphysical_name <span class=\"hljs-keyword\">as<\/span> file_path\n<span class=\"hljs-keyword\">FROM<\/span> sys.database_files\n<span class=\"hljs-keyword\">where<\/span> type_desc = <span class=\"hljs-string\">'ROWS'<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"843\" height=\"119\" src=\"https:\/\/www.sqlservertutorial.net\/wp-content\/uploads\/SQL-Server-Table-Partitioning-path.png\" alt=\"\" class=\"wp-image-3236\" srcset=\"https:\/\/www.sqlservertutorial.net\/wp-content\/uploads\/SQL-Server-Table-Partitioning-path.png 843w, https:\/\/www.sqlservertutorial.net\/wp-content\/uploads\/SQL-Server-Table-Partitioning-path-300x42.png 300w, https:\/\/www.sqlservertutorial.net\/wp-content\/uploads\/SQL-Server-Table-Partitioning-path-768x108.png 768w\" sizes=\"auto, (max-width: 843px) 100vw, 843px\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id='2-create-a-partition-function'>2) Create a partition function <a href=\"#2-create-a-partition-function\" class=\"anchor\" id=\"2-create-a-partition-function\" title=\"Anchor for 2) Create a partition function\">#<\/a><\/h3>\n\n\n\n<p>A partition function is a database object that maps the rows of a table to partitions based on the values of a specified column. That column is called a partitioning column.<\/p>\n\n\n\n<p>A partition function takes values of the partitioning column and returns a partition value. Also, it defines the number of partitions and partition boundaries.<\/p>\n\n\n\n<p>In our example, the partitions will be based on the values of the <code>order_date<\/code> column. And the partition function will create three partitions:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">CREATE<\/span> <span class=\"hljs-keyword\">PARTITION<\/span> <span class=\"hljs-keyword\">FUNCTION<\/span> order_by_year_function (<span class=\"hljs-built_in\">date<\/span>)\n<span class=\"hljs-keyword\">AS<\/span> <span class=\"hljs-keyword\">RANGE<\/span> <span class=\"hljs-keyword\">LEFT<\/span> \n<span class=\"hljs-keyword\">FOR<\/span> <span class=\"hljs-keyword\">VALUES<\/span> (<span class=\"hljs-string\">'2016-12-31'<\/span>, <span class=\"hljs-string\">'2017-12-31'<\/span>,<span class=\"hljs-string\">'2018-12-31'<\/span>);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this statement:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>The <code>order_by_year_function<\/code> is the name of the partition function. It takes an argument whose data type is <code>DATE<\/code>. <\/li><li>The <code>AS RANGE LEFT FOR VALUES<\/code> specifies three boundaries in which the rows with the date before <code>2016-12-31<\/code> will belong to the partition 1, the rows with the date before <code>2017-12-31<\/code> and after <code>2016-12-31<\/code> will belong to the partition 2, the rows with the date between <code>2017-12-31<\/code> and <code>2018-12-31<\/code> will belong to the partition 3. <\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id='3-creating-a-partition-scheme'>3) Creating a partition scheme <a href=\"#3-creating-a-partition-scheme\" class=\"anchor\" id=\"3-creating-a-partition-scheme\" title=\"Anchor for 3) Creating a partition scheme\">#<\/a><\/h3>\n\n\n\n<p>A partition scheme is a database object that maps the partitions returned by a partition function to filegroups. <\/p>\n\n\n\n<p>The following statement creates a partition scheme that maps the partitions returned by <code>order_by_year_function<\/code> to filegroups:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">CREATE<\/span> <span class=\"hljs-keyword\">PARTITION<\/span> SCHEME order_by_year_scheme\n<span class=\"hljs-keyword\">AS<\/span> <span class=\"hljs-keyword\">PARTITION<\/span> order_by_year_function\n<span class=\"hljs-keyword\">TO<\/span> (&#91;orders_2016], &#91;orders_2017], &#91;orders_2018]);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\" id='4-creating-a-partitioned-table'>4) Creating a partitioned table <a href=\"#4-creating-a-partitioned-table\" class=\"anchor\" id=\"4-creating-a-partitioned-table\" title=\"Anchor for 4) Creating a partitioned table\">#<\/a><\/h3>\n\n\n\n<p>The following statement creates a partitioned table based on the <code>order_by_year_scheme<\/code> partition scheme:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">CREATE<\/span> <span class=\"hljs-keyword\">TABLE<\/span> sales.order_reports (\n  order_date <span class=\"hljs-built_in\">date<\/span>,\n  product_name <span class=\"hljs-built_in\">varchar<\/span>(<span class=\"hljs-number\">255<\/span>),\n  amount <span class=\"hljs-built_in\">decimal<\/span>(<span class=\"hljs-number\">10<\/span>, <span class=\"hljs-number\">2<\/span>) <span class=\"hljs-keyword\">NOT<\/span> <span class=\"hljs-literal\">NULL<\/span> <span class=\"hljs-keyword\">DEFAULT<\/span> <span class=\"hljs-number\">0<\/span>,\n  PRIMARY <span class=\"hljs-keyword\">KEY<\/span> (order_date, product_name)\n) \n<span class=\"hljs-keyword\">ON<\/span> order_by_year_scheme (order_date);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this statement:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>The <code>ON order_by_year_scheme (order_date)<\/code> clause specifies the partition scheme and partition column (<code>order_date<\/code>) for the table.<\/li><li>The partition column must be included in a <a href=\"https:\/\/www.sqlservertutorial.net\/sql-server-indexes\/sql-server-clustered-indexes\/\">clustered index<\/a> or you&#8217;ll get an error.<\/li><\/ul>\n\n\n\n<p>The following <code><a href=\"https:\/\/www.sqlservertutorial.net\/sql-server-basics\/sql-server-insert\/\">INSERT<\/a><\/code> statement loads data into the <code>sales.order_reports<\/code> table:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">INSERT<\/span> <span class=\"hljs-keyword\">INTO<\/span> sales.order_reports (order_date, product_name, amount)\n  <span class=\"hljs-keyword\">SELECT<\/span>\n    order_date,\n    product_name,\n    <span class=\"hljs-keyword\">SUM<\/span>(i.quantity * i.list_price * (<span class=\"hljs-number\">1<\/span> - discount))\n  <span class=\"hljs-keyword\">FROM<\/span> sales.orders o\n  <span class=\"hljs-keyword\">INNER<\/span> <span class=\"hljs-keyword\">JOIN<\/span> sales.order_items i\n    <span class=\"hljs-keyword\">ON<\/span> i.order_id = o.order_id\n  <span class=\"hljs-keyword\">INNER<\/span> <span class=\"hljs-keyword\">JOIN<\/span> production.products p\n    <span class=\"hljs-keyword\">ON<\/span> p.product_id = i.product_id\n  <span class=\"hljs-keyword\">GROUP<\/span> <span class=\"hljs-keyword\">BY<\/span> order_date,\n           product_name;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>To check the rows of each partition, you use the following query:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-11\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span> \n\tp.partition_number <span class=\"hljs-keyword\">AS<\/span> partition_number,\n\tf.name <span class=\"hljs-keyword\">AS<\/span> file_group, \n\tp.rows <span class=\"hljs-keyword\">AS<\/span> <span class=\"hljs-keyword\">row_count<\/span>\n<span class=\"hljs-keyword\">FROM<\/span> sys.partitions p\n<span class=\"hljs-keyword\">JOIN<\/span> sys.destination_data_spaces dds <span class=\"hljs-keyword\">ON<\/span> p.partition_number = dds.destination_id\n<span class=\"hljs-keyword\">JOIN<\/span> sys.filegroups f <span class=\"hljs-keyword\">ON<\/span> dds.data_space_id = f.data_space_id\n<span class=\"hljs-keyword\">WHERE<\/span> OBJECT_NAME(OBJECT_ID) = <span class=\"hljs-string\">'order_reports'<\/span>\n<span class=\"hljs-keyword\">order<\/span> <span class=\"hljs-keyword\">by<\/span> partition_number;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-11\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"343\" height=\"95\" src=\"https:\/\/www.sqlservertutorial.net\/wp-content\/uploads\/SQL-Server-Table-Partitioning-Row-Count.png\" alt=\"\" class=\"wp-image-3237\" srcset=\"https:\/\/www.sqlservertutorial.net\/wp-content\/uploads\/SQL-Server-Table-Partitioning-Row-Count.png 343w, https:\/\/www.sqlservertutorial.net\/wp-content\/uploads\/SQL-Server-Table-Partitioning-Row-Count-300x83.png 300w\" sizes=\"auto, (max-width: 343px) 100vw, 343px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id='summary'>Summary <a href=\"#summary\" class=\"anchor\" id=\"summary\" title=\"Anchor for Summary\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li>Use table partitioning to store data of a table in multiple physical sections or partitions.<\/li><\/ul>\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Was this tutorial helpful?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"3227\"\n\t\t\t\tdata-post-url=\"https:\/\/www.sqlservertutorial.net\/sql-server-administration\/sql-server-table-partitioning\/\"\n\t\t\t\tdata-post-title=\"SQL Server Table Partitioning\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"3227\"\n\t\t\t\tdata-post-url=\"https:\/\/www.sqlservertutorial.net\/sql-server-administration\/sql-server-table-partitioning\/\"\n\t\t\t\tdata-post-title=\"SQL Server Table Partitioning\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\n\t\t\t<button class=\"btn btn-primary wth-btn-submit\">Send<\/button>\n\t\t\t<button class=\"btn wth-btn-cancel\">Cancel<\/button>\n\t\t\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you&#8217;ll learn about SQL Server table partitioning and how to create partitioned tables.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":2903,"menu_order":21,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-3227","page","type-page","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>SQL Server Table Partitioning By Practical Examples<\/title>\n<meta name=\"description\" content=\"In this tutorial, you&#039;ll learn about SQL Server table partitioning and how to create partitioned tables.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.sqlservertutorial.net\/sql-server-administration\/sql-server-table-partitioning\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"SQL Server Table Partitioning By Practical Examples\" \/>\n<meta property=\"og:description\" content=\"In this tutorial, you&#039;ll learn about SQL Server table partitioning and how to create partitioned tables.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.sqlservertutorial.net\/sql-server-administration\/sql-server-table-partitioning\/\" \/>\n<meta property=\"og:site_name\" content=\"SQL Server Tutorial\" \/>\n<meta property=\"article:modified_time\" content=\"2022-04-13T02:08:27+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.sqlservertutorial.net\/wp-content\/uploads\/SQL-Server-Table-Partitioning-order-data.png\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"6 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/sql-server-administration\\\/sql-server-table-partitioning\\\/\",\"url\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/sql-server-administration\\\/sql-server-table-partitioning\\\/\",\"name\":\"SQL Server Table Partitioning By Practical Examples\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/sql-server-administration\\\/sql-server-table-partitioning\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/sql-server-administration\\\/sql-server-table-partitioning\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/wp-content\\\/uploads\\\/SQL-Server-Table-Partitioning-order-data.png\",\"datePublished\":\"2022-04-13T01:55:10+00:00\",\"dateModified\":\"2022-04-13T02:08:27+00:00\",\"description\":\"In this tutorial, you'll learn about SQL Server table partitioning and how to create partitioned tables.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/sql-server-administration\\\/sql-server-table-partitioning\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.sqlservertutorial.net\\\/sql-server-administration\\\/sql-server-table-partitioning\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/sql-server-administration\\\/sql-server-table-partitioning\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/wp-content\\\/uploads\\\/SQL-Server-Table-Partitioning-order-data.png\",\"contentUrl\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/wp-content\\\/uploads\\\/SQL-Server-Table-Partitioning-order-data.png\",\"width\":578,\"height\":353},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/sql-server-administration\\\/sql-server-table-partitioning\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"SQL Server Administration\",\"item\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/sql-server-administration\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"SQL Server Table Partitioning\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/#website\",\"url\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/\",\"name\":\"SQL Server Tutorial\",\"description\":\"The Practical SQL Server Tutorial\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"SQL Server Table Partitioning By Practical Examples","description":"In this tutorial, you'll learn about SQL Server table partitioning and how to create partitioned tables.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.sqlservertutorial.net\/sql-server-administration\/sql-server-table-partitioning\/","og_locale":"en_US","og_type":"article","og_title":"SQL Server Table Partitioning By Practical Examples","og_description":"In this tutorial, you'll learn about SQL Server table partitioning and how to create partitioned tables.","og_url":"https:\/\/www.sqlservertutorial.net\/sql-server-administration\/sql-server-table-partitioning\/","og_site_name":"SQL Server Tutorial","article_modified_time":"2022-04-13T02:08:27+00:00","og_image":[{"url":"https:\/\/www.sqlservertutorial.net\/wp-content\/uploads\/SQL-Server-Table-Partitioning-order-data.png","type":"","width":"","height":""}],"twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"6 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.sqlservertutorial.net\/sql-server-administration\/sql-server-table-partitioning\/","url":"https:\/\/www.sqlservertutorial.net\/sql-server-administration\/sql-server-table-partitioning\/","name":"SQL Server Table Partitioning By Practical Examples","isPartOf":{"@id":"https:\/\/www.sqlservertutorial.net\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.sqlservertutorial.net\/sql-server-administration\/sql-server-table-partitioning\/#primaryimage"},"image":{"@id":"https:\/\/www.sqlservertutorial.net\/sql-server-administration\/sql-server-table-partitioning\/#primaryimage"},"thumbnailUrl":"https:\/\/www.sqlservertutorial.net\/wp-content\/uploads\/SQL-Server-Table-Partitioning-order-data.png","datePublished":"2022-04-13T01:55:10+00:00","dateModified":"2022-04-13T02:08:27+00:00","description":"In this tutorial, you'll learn about SQL Server table partitioning and how to create partitioned tables.","breadcrumb":{"@id":"https:\/\/www.sqlservertutorial.net\/sql-server-administration\/sql-server-table-partitioning\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.sqlservertutorial.net\/sql-server-administration\/sql-server-table-partitioning\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.sqlservertutorial.net\/sql-server-administration\/sql-server-table-partitioning\/#primaryimage","url":"https:\/\/www.sqlservertutorial.net\/wp-content\/uploads\/SQL-Server-Table-Partitioning-order-data.png","contentUrl":"https:\/\/www.sqlservertutorial.net\/wp-content\/uploads\/SQL-Server-Table-Partitioning-order-data.png","width":578,"height":353},{"@type":"BreadcrumbList","@id":"https:\/\/www.sqlservertutorial.net\/sql-server-administration\/sql-server-table-partitioning\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.sqlservertutorial.net\/"},{"@type":"ListItem","position":2,"name":"SQL Server Administration","item":"https:\/\/www.sqlservertutorial.net\/sql-server-administration\/"},{"@type":"ListItem","position":3,"name":"SQL Server Table Partitioning"}]},{"@type":"WebSite","@id":"https:\/\/www.sqlservertutorial.net\/#website","url":"https:\/\/www.sqlservertutorial.net\/","name":"SQL Server Tutorial","description":"The Practical SQL Server Tutorial","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.sqlservertutorial.net\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/www.sqlservertutorial.net\/wp-json\/wp\/v2\/pages\/3227","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.sqlservertutorial.net\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.sqlservertutorial.net\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.sqlservertutorial.net\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sqlservertutorial.net\/wp-json\/wp\/v2\/comments?post=3227"}],"version-history":[{"count":5,"href":"https:\/\/www.sqlservertutorial.net\/wp-json\/wp\/v2\/pages\/3227\/revisions"}],"predecessor-version":[{"id":3239,"href":"https:\/\/www.sqlservertutorial.net\/wp-json\/wp\/v2\/pages\/3227\/revisions\/3239"}],"up":[{"embeddable":true,"href":"https:\/\/www.sqlservertutorial.net\/wp-json\/wp\/v2\/pages\/2903"}],"wp:attachment":[{"href":"https:\/\/www.sqlservertutorial.net\/wp-json\/wp\/v2\/media?parent=3227"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}