Skip to content

Commit 9d7f530

Browse files
authored
Merge pull request #48354 from oslake/patch-5
Update sql-database-file-space-management.md
2 parents a373d3a + ab3a6f1 commit 9d7f530

File tree

1 file changed

+41
-34
lines changed

1 file changed

+41
-34
lines changed

articles/sql-database/sql-database-file-space-management.md

Lines changed: 41 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -13,45 +13,45 @@ ms.author: carlrab
1313
---
1414
# Manage file space in Azure SQL Database
1515

16-
This article describes different types of storage space in Azure SQL Database, and steps that can be taken when the file space allocated for databases and elastic pools needs to be managed by the customer.
16+
This article describes different types of storage space in Azure SQL Database, and steps that can be taken when the file space allocated for databases and elastic pools needs to be explicitly managed.
1717

1818
## Overview
1919

20-
In Azure SQL Database, storage size metrics displayed in the Azure portal and the following APIs measure the number of used data pages for databases and elastic pools:
20+
In Azure SQL Database, most storage space metrics displayed in the Azure portal and the following APIs measure the number of used data pages for databases and elastic pools:
2121
- Azure Resource Manager based metrics APIs including PowerShell [get-metrics](https://docs.microsoft.com/powershell/module/azurerm.insights/get-azurermmetric)
2222
- T-SQL: [sys.dm_db_resource_stats](https://docs.microsoft.com/sql/relational-databases/system-dynamic-management-views/sys-dm-db-resource-stats-azure-sql-database)
2323
- T-SQL: [sys.resource_stats](https://docs.microsoft.com/sql/relational-databases/system-catalog-views/sys-resource-stats-azure-sql-database)
2424
- T-SQL: [sys.elastic_pool_resource_stats](https://docs.microsoft.com/sql/relational-databases/system-catalog-views/sys-elastic-pool-resource-stats-azure-sql-database)
2525

26-
There are workload patterns in which the allocation of space in the underlying data files for databases becomes larger than the number of used data pages in the data files. This scenario can occur when space used increases and then data is subsequently deleted. When the data is deleted, the file space allocated is not automatically reclaimed when the data is deleted. In such scenarios, the allocated space for a database or pool may exceed supported max limits set (or supported) for the database and, as a result, prevent data growth or prevent performance tier changes, even though the database space actually used is less than the max space limit. To mitigate, you may need to shrink the database to reduce allocated but unused space in the database.
26+
There are workload patterns where the allocation of underlying data files for databases can become larger than the amount of used data pages. This can occur when space used increases and data is subsequently deleted. This is because file space allocated is not automatically reclaimed when data is deleted. In such scenarios, the allocated space for a database or pool may exceed supported limits and prevent data growth or prevent performance tier changes, and require shrinking data files to mitigate.
2727

28-
The SQL Database service does not automatically shrink database files to reclaim unused allocated space due to the potential impact on database performance. However, you can shrink the file in a database at a time of your choosing by following the steps described in [Reclaim unused allocated space](#reclaim-unused-allocated-space).
28+
The SQL DB service does not automatically shrink data files to reclaim unused allocated space due to the potential impact to database performance. However, customers may shrink data files via self-service at a time of their choosing by following the steps described in [Reclaim unused allocated space](#reclaim-unused-allocated-space).
2929

3030
> [!NOTE]
3131
> Unlike data files, the SQL Database service automatically shrinks log files since that operation does not impact database performance.
3232
33-
## Understanding the types of storage space for a database
33+
## Understanding types of storage space for a database
3434

35-
To manage file space, you need to understand the following terms related to database storage for both a single database and for an elastic pool.
35+
Understanding the following storage space quantities are important for managing the file space of a database.
3636

37-
|Storage space term|Definition|Comments|
37+
|Database quantity|Definition|Comments|
3838
|---|---|---|
39-
|**Data space used**|The amount of space used to store database data in 8 KB pages.|Generally, this space used increases (decreases) on inserts (deletes). In some cases, the space used does not change on inserts or deletes depending on the amount and pattern of data involved in the operation and any fragmentation. For example, deleting one row from every data page does not necessarily decrease the space used.|
40-
|**Space allocated**|The amount of formatted file space made available for storing database data|The space allocated grows automatically, but never decreases after deletes. This behavior ensures that future inserts are faster since space does not need to be reformatted.|
41-
|**Space allocated but unused**|The amount of unused data file space allocated for the database.|This quantity is the difference between the amount of space allocated and space used, and represents the maximum amount of space that can be reclaimed by shrinking database files.|
42-
|**Max size**|The maximum amount of data space that can be used by the database.|The data space allocated cannot grow beyond the data max size.|
39+
|**Data space used**|Amount of space used to store database data in 8 KB pages.|Generally, space used increases (decreases) on inserts (deletes). In some cases, the space used does not change on inserts or deletes depending on the amount and pattern of data involved in the operation and any fragmentation. For example, deleting one row from every data page does not necessarily decrease the space used.|
40+
|**Data space allocated**|Amount of formatted file space made available for storing database data.|The allocated space allocated grows automatically, but never decreases after deletes. This behavior ensures that future inserts are faster since space does not need to be reformatted.|
41+
|**Data space allocated but unused**|Amount of unused data file space allocated for the database.|This quantity is the difference between the amount of space allocated and space used, and represents the maximum amount of free space that can be reclaimed by shrinking database files.|
42+
|**Data max size**|Maximum amount of space that can be used by the database for storing data.|The amount of data space allocated cannot grow beyond the data max size.|
4343
||||
4444

45-
The following diagram illustrates the relationship between the types of storage space.
45+
The following diagram illustrates the relationship between the different types of storage space.
4646

4747
![storage space types and relationships](./media/sql-database-file-space-management/storage-types.png)
4848

4949
## Query a database for storage space information
5050

51-
To determine if you have allocated but unused data space for an individual database that you may wish to reclaim, use the following queries:
51+
The following queries can be used to determine storage space quantities for a database.
5252

5353
### Database data space used
54-
Modify the following query to return the amount of database data space used in MB.
54+
Modify the following query to return the amount of database data space used. Units of the query result are in MB.
5555

5656
```sql
5757
-- Connect to master
@@ -62,8 +62,8 @@ WHERE database_name = 'db1'
6262
ORDER BY end_time DESC
6363
```
6464

65-
### Database data allocated and allocated space unused
66-
Modify the following query to return the amount of database data allocated and allocated space unused.
65+
### Database data space allocated and unused allocated space
66+
Use the following query to return the amount of database data space allocated and the amount of unused space allocated. Units of the query result are in MB.
6767

6868
```sql
6969
-- Connect to database
@@ -75,21 +75,33 @@ GROUP BY type_desc
7575
HAVING type_desc = 'ROWS'
7676
```
7777

78-
### Database max size
79-
Modify the following query to return the database max size in bytes.
78+
### Database data max size
79+
Modify the following query to return the database data max size. Units of the query result are in bytes.
8080

8181
```sql
8282
-- Connect to database
8383
-- Database data max size in bytes
8484
SELECT DATABASEPROPERTYEX('db1', 'MaxSizeInBytes') AS DatabaseDataMaxSizeInBytes
8585
```
8686

87+
## Understanding the types of storage space for an elastic pool
88+
89+
Understanding the following storage space quantities are important for managing the file space of an elastic pool.
90+
91+
|Elastic pool quantity|Definition|Comments|
92+
|---|---|---|
93+
|**Data space used**|The summation of data space used by all databases in the elastic pool.||
94+
|**Data space allocated**|The summation of data space allocated by all databases in the pool.||
95+
|**Data space allocated but unused**|The difference between the amount of space allocated and space used by the elastic pool.|This quantity represents the maximum amount of space allocated for the pool that can be reclaimed by shrinking database files.|
96+
|**Data max size**|The maximum amount of data space that can be used by the elastic pool for all of its databases.|The space allocated for the pool should not exceed the elastic pool max size. If this occurs, then space allocated that is unused can be reclaimed by shrinking database files.|
97+
||||
98+
8799
## Query an elastic pool for storage space information
88100

89-
To determine if you have allocated but unused data space in an elastic pool and for each pooled database that you may wish to reclaim, use the following queries:
101+
The following queries can be used to determine storage space quantities for an elastic pool.
90102

91103
### Elastic pool data space used
92-
Modify the following query to return the amount of elastic pool data space used in MB.
104+
Modify the following query to return the amount of elastic pool data space used. Units of the query result are in MB.
93105

94106
```sql
95107
-- Connect to master
@@ -100,9 +112,9 @@ WHERE elastic_pool_name = 'ep1'
100112
ORDER BY end_time DESC
101113
```
102114

103-
### Elastic pool data allocated and allocated space unused
115+
### Elastic pool data space allocated and unused allocated space
104116

105-
Modify the following PowerShell script to return a table listing the total space allocated and unused space allocated for each database in an elastic pool. The table orders databases from those with the greatest amount of space allocated unused to the least amount of space allocated unused.
117+
Modify the following PowerShell script to return a table listing the total space allocated and unused space allocated for each database in an elastic pool. The table orders databases from those with the greatest amount of space allocated unused to the least amount of space allocated unused. Units of the query result are in MB.
106118

107119
The query results for determining the space allocated for each database in the pool can be added together to the determine the elastic pool space allocated. The elastic pool space allocated should not exceed the elastic pool max size.
108120

@@ -155,9 +167,9 @@ The following screenshot is an example of the output of the script:
155167

156168
![elastic pool allocated space and unused allocated space example](./media/sql-database-file-space-management/elastic-pool-allocated-unused.png)
157169

158-
### Elastic pool max size
170+
### Elastic pool data max size
159171

160-
Use the following T-SQL query to return the elastic database max size in MB.
172+
Modify the following T-SQL query to return the elastic pool data max size. Units of the query result are in MB.
161173

162174
```sql
163175
-- Connect to master
@@ -170,30 +182,25 @@ ORDER BY end_time DESC
170182

171183
## Reclaim unused allocated space
172184

173-
Once you have determined that you have unused allocated space that you wish to reclaim, use the following command to shrink the database space allocated.
174-
175-
> [!IMPORTANT]
176-
> For databases in an elastic pool, databases with the most space allocated unused should be shrunk first to reclaim file space most quickly.
177-
178-
Use the following command to shrink all of the data files in the specified database:
185+
Once databases have been identified for reclaiming unused allocated space, modify the following command to shrink the data files for each database.
179186

180187
```sql
181188
-- Shrink database data space allocated.
182-
DBCC SHRINKDATABASE (N'<database_name>')
189+
DBCC SHRINKDATABASE (N'db1')
183190
```
184191

185-
For more information about this command, see [SHRINKDATABASE](https://docs.microsoft.com/sql/t-sql/database-console-commands/dbcc-shrinkdatabase-transact-sql).
192+
For more information about this command, see [SHRINKDATABASE](https://docs.microsoft.com/sql/t-sql/database-console-commands/dbcc-shrinkdatabase-transact-sql).
186193

187194
> [!IMPORTANT]
188195
> Consider rebuilding database indexes
189196
After database data files are shrunk, indexes may become fragmented and lose their performance optimization effectiveness. If this occurs, then the indexes should be rebuilt. For more information on fragmentation and rebuilding indexes, see [Reorganize and Rebuild Indexes](https://docs.microsoft.com/sql/relational-databases/indexes/reorganize-and-rebuild-indexes).
190197

191198
## Next steps
192199

193-
- For information about max database sizes, see:
200+
- For information about database max sizes, see:
194201
- [Azure SQL Database vCore-based purchasing model limits for a single database](https://docs.microsoft.com/azure/sql-database/sql-database-vcore-resource-limits-single-databases)
195202
- [Resource limits for single databases using the DTU-based purchasing model](https://docs.microsoft.com/azure/sql-database/sql-database-dtu-resource-limits-single-databases)
196203
- [Azure SQL Database vCore-based purchasing model limits for elastic pools](https://docs.microsoft.com/azure/sql-database/sql-database-vcore-resource-limits-elastic-pools)
197204
- [Resources limits for elastic pools using the DTU-based purchasing model](https://docs.microsoft.com/azure/sql-database/sql-database-dtu-resource-limits-elastic-pools)
198205
- For more information about the `SHRINKDATABASE` command, see [SHRINKDATABASE](https://docs.microsoft.com/sql/t-sql/database-console-commands/dbcc-shrinkdatabase-transact-sql).
199-
- For more information on fragmentation and rebuilding indexes, see [Reorganize and Rebuild Indexes](https://docs.microsoft.com/sql/relational-databases/indexes/reorganize-and-rebuild-indexes).
206+
- For more information on fragmentation and rebuilding indexes, see [Reorganize and Rebuild Indexes](https://docs.microsoft.com/sql/relational-databases/indexes/reorganize-and-rebuild-indexes).

0 commit comments

Comments
 (0)