How to Compact a Database
-
The compaction operation removes empty gaps on disk that still occupy space after deletes.
You can choose what should be compacted: documents and/or selected indexes. -
During compaction the database will be offline.
The operation is a executed asynchronously as a background operation and can be awaited. -
The operation will compact the database on one node.
To compact all database-group nodes, the command must be sent to each node separately. -
Target node:
By default, the operation will be executed on the server node that is defined by the client configuration.
The operation can be executed on a specific node by using the forNode method. -
Target database:
The database to compact is specified inCompactSettings
(see examples below). // todo.. this is interface ??
An exception is thrown if the specified database doesn't exist on the server node. -
In this page:
Examples
Compact documents
- The following example will compact only documents for the specified database.
// Define the compact settings
const compactSettings = {
// Database to compact
databaseName: "Northwind",
// Set 'documents' to true to compact all documents in database
// Indexes are not set and will not be compacted
documents: true
};
// Define the compact operation, pass the settings
const compactOp = new CompactDatabaseOperation(compactSettings);
// Execute compaction by passing the operation to maintenance.server.send
const asyncOperation = await documentStore.maintenance.server.send(compactOp);
// Wait for operation to complete, during compaction the database is offline
await asyncOperation.waitForCompletion();
Compact specific indexes
- The following example will compact only specific indexes.
// Define the compact settings
const compactSettings = {
// Database to compact
databaseName: "Northwind",
// Setting 'documents' to false will compact only the specified indexes
documents: false,
// Specify which indexes to compact
indexes: ["Orders/Totals", "Orders/ByCompany"]
};
// Define the compact operation, pass the settings
const compactOp = new CompactDatabaseOperation(compactSettings);
// Execute compaction by passing the operation to maintenance.server.send
const asyncOperation = await documentStore.maintenance.server.send(compactOp);
// Wait for operation to complete
await asyncOperation.waitForCompletion();
Compact all indexes
- The following example will compact all indexes and documents.
// Get all indexes names in the database using the 'GetIndexNamesOperation' operation
// Use 'forDatabase' if the target database is different than the default database defined on the store
const allIndexNames = await documentStore.maintenance.forDatabase("Northwind")
.send(new GetIndexNamesOperation(0, 50));
// Define the compact settings
const compactSettings = {
databaseName: "Northwind", // Database to compact
documents: true, // Compact all documents
indexes: allIndexNames, // All indexes will be compacted
};
// Define the compact operation, pass the settings
const compactOp = new CompactDatabaseOperation(settings);
// Execute compaction by passing the operation to maintenance.server.send
const asyncOperation = await documentStore.maintenance.server.send(compactOp);
// Wait for operation to complete
await asyncOperation.waitForCompletion();
Compact on other nodes
- By default, an operation executes on the server node that is defined by the client configuration.
- The following example will compact the database on all member nodes from its database-group topology.
forNode
is used to execute the operation on a specific node.
// Get all member nodes in the database-group using the 'GetDatabaseRecordOperation' operation
const databaseRecord =
await documentStore.maintenance.server.send(new GetDatabaseRecordOperation("Northwind"));
const allMemberNodes = databaseRecord.topology.members;
// Define the compact settings as needed
const compactSettings = {
// Database to compact
databaseName: "Northwind",
//Compact all documents in database
documents: true
};
// Execute the compact operation on each member node
for (let i = 0; i < allMemberNodes.length; i++) {
// Define the compact operation, pass the settings
const compactOp = new CompactDatabaseOperation(compactSettings);
// Execute the operation on a specific node
// Use `forNode` to specify the node to operate on
const serverOpExecutor = await documentStore.maintenance.server.forNode(allMemberNodes[i]);
const asyncOperation = await serverOpExecutor.send(compactOp);
// Wait for operation to complete
await asyncOperation.waitForCompletion();
}
Compaction triggers compression
- The compaction operation triggers documents compression on all existing documents in collections that are configured for compression.
- Differences between the two features are summarized below:
Compaction | |
---|---|
Action: | Remove empty gaps on disk that still occupy space after deletes |
Items that can be compacted: | Documents and/or indexes on the specified database |
Triggered by: | Client API code |
Triggered when: | Explicitly calling CompactDatabaseOperation |
Compression | |
---|---|
Action: | Reduce storage space using the Zstd compression algorithm |
Items that can be compressed: | - Documents in collections that are configured for compression - Revisions for all collections |
Triggered by: | The server |
Triggered when: | Compression feature is configured, and when either of the following occurs for the configured collections: - Storing new documents - Modifying & saving existing documents - Compact operation is triggered, existing documents will be compressed |
Compact from Studio
-
Compaction can be triggered from the Storage Report view in the Studio.
The operation will compact the database only on the node being viewed (node info is in the Studio footer). -
To compact the database on another node,
simply trigger compaction from the Storage Report view in a browser tab opened for that other node.
Syntax
const compactOperation = new CompactDatabaseOperation(compactSettings);
Parameters | Type | Description |
---|---|---|
compactSettings | object | Settings for the compact operation. See object fields below. |
compactSettings fields | ||
---|---|---|
databaseName | string | Name of database to compact. Mandatory param. |
documents | boolean | Indicates if documents should be compacted. Optional param. |
indexes | string[] | List of index names to compact. Optional param. |
Note: Either Documents or Indexes (or both) must be specified |