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
You have installed the PyMilvus library on your local client and updated it to the latest version.
If you have not installed the PyMilvus library or need to update it, run the following command.
pip install --upgrade pymilvusYou have created a Milvus instance. 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.
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)Load partitions.
Use the
load_partitionsfunction 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)