Manage partitions

Updated at:
Copy as MD

When you create a collection in Milvus, the system automatically creates a default partition named _default to store inserted data for which no partition is specified. Appropriate partitioning narrows the query scope and improves retrieval performance. This topic describes how to create and manage partitions in Milvus.

Prerequisites

  • The PyMilvus library is installed on your local client and is updated to the latest version.

    To install or update the PyMilvus library, run the following command.

    pip install --upgrade pymilvus
  • A Milvus instance has been created. For more information, see Create a Milvus instance.

  • A collection has been created. For more information, see Manage collections.

Limits

You can create up to 4,096 partitions in a collection.

View partitions

from pymilvus import MilvusClient

client = MilvusClient(
    uri="http://c-xxxx.milvus.aliyuncs.com:19530",  # The public endpoint of the Milvus instance.
    token="<yourUsername>:<yourPassword>",  # The username and password for the Milvus instance.
    db_name="default"  # The database to connect to. This example uses the default database, default.
)

res = client.list_partitions(collection_name="yourCollectionname")
print(res)

Create a partition

Partitions are created within existing collections. A partition is a logical subset of a collection that organizes and manages its data. Create a collection before you create a partition in it.

client.create_partition(
    collection_name="<yourCollectionname>",  # The collection in which to create the partition.
    partition_name="<yourPartitionname>"  # The name of the partition to create.
)

res = client.list_partitions(collection_name="<yourCollectionname>")
print(res)

Load and release partitions

Load Partition

Check the load state to ensure that you load only partitions that are not loaded or have been released.

  1. Check the load state.

    # Release the collection
    client.release_collection(collection_name="yourCollectionname")
    
    # Check the load state
    res = client.get_load_state(collection_name="yourCollectionname")
    print(res)
    
    res = client.get_load_state(
        collection_name="yourCollectionname", 
        partition_name="yourPartitionname"
    )
    
    print(res)
  2. Load the partitions.

    Use the load_partitions function to load partitions into memory.

    client.load_partitions(
        collection_name="test_milvus",
        partition_names=["yourPartitionname", "yourPartitionname1"]
    )
    
    res = client.get_load_state(
        collection_name="yourCollectionname",
        partition_name="yourPartitionname"
    )
    

Release a partition

Use the release_collection function to release partitions.

client.release_partitions(
    collection_name="yourCollectionname",
    partition_names=["_default", "yourPartitionname", "yourPartitionname1"]
)

res = client.get_load_state(
    collection_name="yourCollectionname",
)

Drop a partition

Use the drop_partition function to drop partitions.

client.drop_partition(
    collection_name="yourCollectionname",
    partition_name="yourPartitionname"
)

res = client.list_partitions(collection_name="yourCollectionname")
print(res)