Compact Database Operation


  • Use The CompactDatabaseOperation compaction operation to removes empty gaps on disk that still occupy space after deletes.
    You can choose whether to compact 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 waited for using wait_for_completion.

  • 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.

  • Target database:
    The database to compact is specified in CompactSettings (see examples below).
    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
settings = CompactSettings(
    # Database to compact
    "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
compact_op = CompactDatabaseOperation(settings)

# Execute compaction by passing the operation to maintenance.server.send
operation = store.maintenance.server.send_async(compact_op)

# Wait for operation to complete, during compaction the database is offline
operation.wait_for_completion()

Compact specific indexes:

The following example will compact only specific indexes.

# Define the compact settings
settings = CompactSettings(
    # Database to compact
    database_name="Northwind",
    # Setting 'documents' to False will compact only the specified indexes
    documents=False,
    # Specify which indexes to compact
    indexes=["Orders/Totals", "Orders/ByCompany"],
    # Optimize indexes is Lucene's feature to gain disk space and efficiency
    # Set whether to skip this optimization when compacting the indexes
    skip_optimize_indexes=False,
)
# Define the compact operation, pass the settings
compact_op = CompactDatabaseOperation(settings)

# Execute compaction by passing the operation to maintenance.server.send
operation = store.maintenance.server.send_async(compact_op)
# Wait for operation to complete
operation.wait_for_completion()

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 from the default database defined on the store
all_indexes_names = store.maintenance.for_database("Northwind").send(GetIndexNamesOperation(0, int_max))

# Define the compact settings
settings = CompactSettings(
    database_name="Northwind",  # Database to compact
    documents=True,  # Compact all documents
    indexes=all_indexes_names,  # All indexes will be compacted
    skip_optimize_indexes=True,  # Skip Lucene's indexes optimization
)

# Define the compact operation, pass the settings
compact_op = CompactDatabaseOperation(settings)

# Execute compaction by passing the operation to maintenance.server.send
operation = store.maintenance.server.send(compact_op)
# Wait for operation to complete
operation.wait_for_completion()

Compaction triggers compression

  • When document compression is turned on, compression is applied to the documents when:

    • New documents that are created and saved.
    • Existing documents that are modified and saved.
  • You can use the compaction operation to compress existing documents without having to modify and save them.
    Executing compaction triggers compression on ALL existing documents for the collections that are configured for compression.

  • Learn more about Compression -vs- Compaction here.

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.