Manage partitions

更新时间:
复制 MD 格式

When you create a collection in the Milvus vector database, a default partition named _default is automatically created to store data that is not assigned to a specific partition. Partitioning helps narrow the query scope and improves search performance. This topic describes how to 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.

  • You have created a collection. For more information, see Manage collections.

Limits

A single collection can have a maximum of 4,096 partitions.

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 name of the database to connect to. This example uses the default database.
)

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

Create a partition

Partitions are logical subsets of a collection that help organize and manage data. Run the following code to create a partition.

client.create_partition(
    collection_name="<yourCollectionname>",  # The name of 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 partitions

By checking the load status, you can ensure that you only load partitions notin memory.

  1. Check the load status.

    # 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 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 partitions

Unload a partition from memory using therelease_partitions function.

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

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

Delete a partition

Use the drop_partition function to delete a partition.

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

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