Skip to content

Commit 53f1708

Browse files
committed
Merge branch 'main' into release-dallas
2 parents 187de49 + 3af1765 commit 53f1708

11 files changed

Lines changed: 873 additions & 706 deletions

docs/relational-databases/partitions/create-partitioned-tables-and-indexes.md

Lines changed: 336 additions & 290 deletions
Large diffs are not rendered by default.
Lines changed: 86 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
---
2-
description: "Modify a Partition Function"
3-
title: "Modify a Partition Function | Microsoft Docs"
2+
description: "Modify a partition function"
3+
title: "Modify a partition function"
44
ms.custom: ""
5-
ms.date: "03/14/2017"
5+
ms.date: "4/22/2022"
66
ms.prod: sql
77
ms.reviewer: ""
88
ms.technology:
@@ -12,126 +12,139 @@ author: LitKnd
1212
ms.author: kendralittle
1313
monikerRange: "=azuresqldb-current||>=sql-server-2016||>=sql-server-linux-2017||=azuresqldb-mi-current"
1414
---
15-
# Modify a Partition Function
16-
[!INCLUDE [SQL Server Azure SQL Database](../../includes/applies-to-version/sql-asdb.md)]
17-
You can change the way a table or index is partitioned in [!INCLUDE[ssnoversion](../../includes/ssnoversion-md.md)] by adding or subtracting the number of partitions specified, in increments of 1, in the partition function of the partitioned table or index by using [!INCLUDE[tsql](../../includes/tsql-md.md)]. When you add a partition, you do so by "splitting" an existing partition into two partitions and redefining the boundaries of the new partitions. When you drop a partition, you do so by "merging" the boundaries of two partitions into one. This last action repopulates one partition and leaves the other partition unassigned.
15+
# Modify a partition function
16+
[!INCLUDE [SQL Server Azure SQL Database Azure SQL Managed Instance](../../includes/applies-to-version/sql-asdb-asdbmi.md)]
17+
18+
You can change the way a table or index is partitioned in SQL Server, Azure SQL Database, and Azure SQL Managed Instance by adding or subtracting the number of partitions specified, in increments of 1, in the partition function of the partitioned table or index by using [!INCLUDE[tsql](../../includes/tsql-md.md)]. When you add a partition, you do so by "splitting" an existing partition into two partitions and redefining the boundaries of the new partitions. When you drop a partition, you do so by "merging" the boundaries of two partitions into one. This last action repopulates one partition and leaves the other partition unassigned. Review [best practices](../../t-sql/statements/alter-partition-function-transact-sql.md#best-practices) before modifying a partition function.
1819

1920
> [!CAUTION]
20-
> More than one table or index can use the same partition function. When you modify a partition function, you affect all of them in a single transaction. Check the partition function's dependencies before modifying it.
21-
22-
**In This Topic**
23-
24-
- **Before you begin:**
25-
26-
[Limitations and Restrictions](#Restrictions)
27-
28-
[Security](#Security)
29-
30-
- **To modify a partition function, using:**
31-
32-
[SQL Server Management Studio](#SSMSProcedure)
33-
34-
[Transact-SQL](#TsqlProcedure)
35-
36-
## <a name="BeforeYouBegin"></a> Before You Begin
21+
> More than one table or index can use the same partition function. When you modify a partition function, you affect all of them in a single transaction. Check the [partition function's dependencies](#query-partitioned-objects-in-a-database) before modifying it.
22+
23+
Table partitioning is also available in dedicated SQL pools in Azure Synapse Analytics, with some syntax differences. Learn more in [Partitioning tables in dedicated SQL pool](/azure/synapse-analytics/sql-data-warehouse/sql-data-warehouse-tables-partition).
3724

38-
### <a name="Restrictions"></a> Limitations and Restrictions
25+
## <a name="Restrictions"></a> Limitations
3926

4027
- ALTER PARTITION FUNCTION can only be used for splitting one partition into two, or for merging two partitions into one. To change the way a table or index is partitioned (from 10 partitions to 5, for example), you can use any one of the following options:
4128

42-
- Create a new partitioned table with the desired partition function, and then insert the data from the old table into the new table by using either an INSERT INTO ... SELECT FROM [!INCLUDE[tsql](../../includes/tsql-md.md)] statement or the **Manage Partition Wizard** in [!INCLUDE[ssManStudioFull](../../includes/ssmanstudiofull-md.md)].
29+
- Create a new partitioned table with the desired partition function, and then insert the data from the old table into the new table by using either an INSERT INTO ... SELECT FROM [!INCLUDE[tsql](../../includes/tsql-md.md)] statement or the **Manage Partition Wizard** in [SQL Server Management Studio (SSMS)](../../ssms/sql-server-management-studio-ssms.md).
4330

44-
- Create a partitioned clustered index on a heap.
31+
- Create a partitioned [clustered index](../../t-sql/statements/create-index-transact-sql.md) on a heap.
4532

4633
> [!NOTE]
4734
> Dropping a partitioned clustered index results in a partitioned heap.
4835
49-
- Drop and rebuild an existing partitioned index by using the [!INCLUDE[tsql](../../includes/tsql-md.md)] CREATE INDEX statement with the DROP EXISTING = ON clause.
36+
- Drop and rebuild an existing partitioned index by using the [!INCLUDE[tsql](../../includes/tsql-md.md)] [CREATE INDEX statement with the DROP EXISTING = ON](../../t-sql/statements/create-index-transact-sql.md#drop_existing---on--off-) clause.
5037

5138
- Perform a sequence of ALTER PARTITION FUNCTION statements.
5239

53-
- [!INCLUDE[ssNoVersion](../../includes/ssnoversion-md.md)] does not provide replication support for modifying a partition function. If you want to make changes to a partition function in the publication database, you must do this manually in the subscription database.
40+
- The database engine does not provide replication support for modifying a partition function. If you want to make changes to a partition function in the publication database, you must do this manually in the subscription database.
5441

5542
- All filegroups that are affected by ALTER PARTITION FUNCTION must be online.
5643

57-
### <a name="Security"></a> Security
58-
59-
#### <a name="Permissions"></a> Permissions
44+
## <a name="Permissions"></a> Permissions
6045
Any one of the following permissions can be used to execute ALTER PARTITION FUNCTION:
6146

6247
- ALTER ANY DATASPACE permission. This permission defaults to members of the **sysadmin** fixed server role and the **db_owner** and **db_ddladmin** fixed database roles.
6348

6449
- CONTROL or ALTER permission on the database in which the partition function was created.
6550

6651
- CONTROL SERVER or ALTER ANY DATABASE permission on the server of the database in which the partition function was created.
67-
68-
## <a name="SSMSProcedure"></a> Using SQL Server Management Studio
69-
**To modify a partition function:**
70-
71-
This specific action cannot be performed using [!INCLUDE[ssManStudioFull](../../includes/ssmanstudiofull-md.md)]. In order to modify a partition function, you must first delete the function and then create a new one with the desired properties using the Create Partition Wizard. For more information, see
72-
73-
#### To delete a partition function
74-
75-
1. Expand the database where you want to delete the partition function and then expand the **Storage** folder.
76-
77-
2. Expand the **Partition Functions** folder.
78-
79-
3. Right-click the partition function you want to delete and select **Delete**.
80-
81-
4. In the **Delete Object** dialog box, ensure that the correct partition function is selected, and then click **OK**.
8252

83-
## <a name="TsqlProcedure"></a> Using Transact-SQL
84-
85-
#### To split a single partition into two partitions
86-
87-
1. In **Object Explorer**, connect to an instance of [!INCLUDE[ssDE](../../includes/ssde-md.md)].
53+
## Query partitioned objects in a database
54+
55+
The following query lists all partitioned objects in a database. This can be used to check the dependencies for a partition function before modifying it.
56+
57+
```sql
58+
SELECT
59+
PF.name AS PartitionFunction,
60+
ds.name AS PartitionScheme,
61+
OBJECT_SCHEMA_NAME(si.object_id) as SchemaName,
62+
OBJECT_NAME(si.object_id) AS PartitionedTable,
63+
si.name as IndexName
64+
FROM sys.indexes AS si
65+
JOIN sys.data_spaces AS ds
66+
ON ds.data_space_id = si.data_space_id
67+
JOIN sys.partition_schemes AS PS
68+
ON PS.data_space_id = si.data_space_id
69+
JOIN sys.partition_functions AS PF
70+
ON PF.function_id = PS.function_id
71+
WHERE ds.type = 'PS'
72+
AND OBJECTPROPERTYEX(si.object_id, 'BaseType') = 'U'
73+
ORDER BY PartitionFunction, PartitionScheme, SchemaName, PartitionedTable;
74+
```
75+
76+
## Split a partition with Transact-SQL
8877

89-
2. On the Standard bar, click **New Query**.
78+
1. In **Object Explorer**, connect to your target database.
9079

91-
3. Copy and paste the following example into the query window and click **Execute**.
80+
2. On the Standard bar, select **New Query**.
9281

93-
```
94-
-- Look for a previous version of the partition function "myRangePF1" and deletes it if it is found.
82+
3. Copy and paste the following example into the query window and select **Execute**.
83+
84+
This example:
85+
- Checks for a previous version of the partition function `myRangePF1` and deletes it if it is found.
86+
- Creates a partition function called `myRangePF1` that partitions a table into four partitions.
87+
- Splits the partition between boundary_values 100 and 1000 to create a partition between boundary_values 100 and 500 and a partition between boundary_values 500 and 1000.
88+
89+
```sql
9590
IF EXISTS (SELECT * FROM sys.partition_functions
9691
WHERE name = 'myRangePF1')
9792
DROP PARTITION FUNCTION myRangePF1;
98-
GO
99-
-- Create a new partition function called "myRangePF1" that partitions a table into four partitions.
93+
GO
94+
10095
CREATE PARTITION FUNCTION myRangePF1 (int)
10196
AS RANGE LEFT FOR VALUES ( 1, 100, 1000 );
10297
GO
103-
--Split the partition between boundary_values 100 and 1000
104-
--to create two partitions between boundary_values 100 and 500
105-
--and between boundary_values 500 and 1000.
98+
10699
ALTER PARTITION FUNCTION myRangePF1 ()
107100
SPLIT RANGE (500);
108101
```
109102

110-
#### To merge two partitions into one partition
103+
## Merge two partitions with Transact-SQL
111104

112-
1. In **Object Explorer**, connect to an instance of [!INCLUDE[ssDE](../../includes/ssde-md.md)].
105+
1. In **Object Explorer**, connect to your target database.
113106

114-
2. On the Standard bar, click **New Query**.
107+
2. On the Standard bar, select **New Query**.
115108

116-
3. Copy and paste the following example into the query window and click **Execute**.
117-
118-
```
119-
-- Look for a previous version of the partition function "myRangePF1" and deletes it if it is found.
109+
3. Copy and paste the following example into the query window and select **Execute**.
110+
111+
This example:
112+
- Checks if a previous version of the partition function `myRangePF1` exists, and deletes it if it is found.
113+
- Creates a partition function called `myRangePF1` with three boundary values, which will result in four partitions.
114+
- Merges the partition between boundary_values 1 and 100 with the partition between boundary_values 100 and 1,000.
115+
- This results in the partition function `myRangePF1` having two boundary points, 1 and 1,000.
116+
117+
```sql
120118
IF EXISTS (SELECT * FROM sys.partition_functions
121119
WHERE name = 'myRangePF1')
122120
DROP PARTITION FUNCTION myRangePF1;
123-
GO
124-
-- Create a new partition function called "myRangePF1" that partitions a table into four partitions.
121+
GO
122+
125123
CREATE PARTITION FUNCTION myRangePF1 (int)
126124
AS RANGE LEFT FOR VALUES ( 1, 100, 1000 );
127125
GO
128-
--Merge the partitions between boundary_values 1 and 100
129-
--and between boundary_values 100 and 1000 to create one partition
130-
--between boundary_values 1 and 1000.
126+
131127
ALTER PARTITION FUNCTION myRangePF1 ()
132128
MERGE RANGE (100);
133129
```
130+
131+
## Delete a partition function with SSMS
132+
133+
1. In **Object Explorer**, connect to your target database.
134+
135+
1. Expand the database where you want to delete the partition function and then expand the **Storage** folder.
134136

135-
For more information, see [ALTER PARTITION FUNCTION &#40;Transact-SQL&#41;](../../t-sql/statements/alter-partition-function-transact-sql.md).
137+
1. Expand the **Partition Functions** folder.
136138

139+
1. Right-click the partition function you want to delete and select **Delete**.
137140

141+
1. In the **Delete Object** dialog box, ensure that the correct partition function is selected, and then select **OK**.
142+
143+
## Next steps
144+
145+
Learn more about related concepts in the following articles:
146+
147+
- [Partitioned tables and indexes](partitioned-tables-and-indexes.md)
148+
- [Create partitioned tables and indexes](create-partitioned-tables-and-indexes.md)
149+
- [ALTER PARTITION FUNCTION &#40;Transact-SQL&#41;](../../t-sql/statements/alter-partition-function-transact-sql.md)
150+
- [Partitioning tables in dedicated SQL pool](/azure/synapse-analytics/sql-data-warehouse/sql-data-warehouse-tables-partition)

0 commit comments

Comments
 (0)