You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: articles/sql-database/sql-database-file-space-management.md
+41-34Lines changed: 41 additions & 34 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -13,45 +13,45 @@ ms.author: carlrab
13
13
---
14
14
# Manage file space in Azure SQL Database
15
15
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.
17
17
18
18
## Overview
19
19
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:
21
21
- Azure Resource Manager based metrics APIs including PowerShell [get-metrics](https://docs.microsoft.com/powershell/module/azurerm.insights/get-azurermmetric)
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 andprevent data growth or prevent performance tier changes, and require shrinking data files to mitigate.
27
27
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).
29
29
30
30
> [!NOTE]
31
31
> Unlike data files, the SQL Database service automatically shrinks log files since that operation does not impact database performance.
32
32
33
-
## Understanding the types of storage space for a database
33
+
## Understanding types of storage space for a database
34
34
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.
36
36
37
-
|Storage space term|Definition|Comments|
37
+
|Database quantity|Definition|Comments|
38
38
|---|---|---|
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.|
43
43
||||
44
44
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.
46
46
47
47

48
48
49
49
## Query a database for storage space information
50
50
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.
52
52
53
53
### 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.
55
55
56
56
```sql
57
57
-- Connect to master
@@ -62,8 +62,8 @@ WHERE database_name = 'db1'
62
62
ORDER BY end_time DESC
63
63
```
64
64
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.
67
67
68
68
```sql
69
69
-- Connect to database
@@ -75,21 +75,33 @@ GROUP BY type_desc
75
75
HAVING type_desc ='ROWS'
76
76
```
77
77
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.
80
80
81
81
```sql
82
82
-- Connect to database
83
83
-- Database data max size in bytes
84
84
SELECT DATABASEPROPERTYEX('db1', 'MaxSizeInBytes') AS DatabaseDataMaxSizeInBytes
85
85
```
86
86
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
+
87
99
## Query an elastic pool for storage space information
88
100
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.
90
102
91
103
### 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.
93
105
94
106
```sql
95
107
-- Connect to master
@@ -100,9 +112,9 @@ WHERE elastic_pool_name = 'ep1'
100
112
ORDER BY end_time DESC
101
113
```
102
114
103
-
### Elastic pool data allocated and allocated space unused
115
+
### Elastic pool data space allocated and unused allocated space
104
116
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.
106
118
107
119
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.
108
120
@@ -155,9 +167,9 @@ The following screenshot is an example of the output of the script:
155
167
156
168

157
169
158
-
### Elastic pool max size
170
+
### Elastic pool data max size
159
171
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.
161
173
162
174
```sql
163
175
-- Connect to master
@@ -170,30 +182,25 @@ ORDER BY end_time DESC
170
182
171
183
## Reclaim unused allocated space
172
184
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.
179
186
180
187
```sql
181
188
-- Shrink database data space allocated.
182
-
DBCC SHRINKDATABASE (N'<database_name>')
189
+
DBCC SHRINKDATABASE (N'db1')
183
190
```
184
191
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).
186
193
187
194
> [!IMPORTANT]
188
195
> Consider rebuilding database indexes
189
196
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).
190
197
191
198
## Next steps
192
199
193
-
- For information about max database sizes, see:
200
+
- For information about database max sizes, see:
194
201
-[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)
195
202
-[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)
196
203
-[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)
197
204
-[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)
198
205
- 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