Vector type
PlanetScale MySQL provides aVECTOR(X) type that can be used to store vectors.
To add a vector column to a table, set it to type VECTOR(X) where X is the dimension of the vectors to be stored in this column.
Example
Vector index
PlanetScale MySQL provides a newVECTOR INDEX to facilitate fast and scalable approximate nearest neighbor (ANN) search on vector data.
Statements that create a vector index may take optional parameters, which can be specified as JSON key-value pairs, via the SECONDARY_ENGINE_ATTRIBUTE variable.
There are four options that can be specified in the JSON. The first two are the following:
type: specifies the algorithm used to build and query the vector index.- Supported values:
spann(more info on the SPANN algorithm)
- Supported values:
distancespecifies the distance metric that queries will use.- Supported values:
dotfor the dot productcosinefor the cosine of the angle between the two vectors.l2oreuclideanfor the length of a line between the ends of the vectorsl2_squaredoreuclidean_squaredfor the square of the Euclidean distance. This is the default.
- Supported values:
Quantization options
Vector indexes in PlanetScale MySQL can be configured to use quantization to reduce the size of the vectors stored in the index. Quantization is a compression technique that reduces the number of bits used to store each vector. This compression is only applied to the vectors as they’re stored in the index: the vectors that you insert in your MySQL table are always stored losslessly. The default mode of quantization isbfloat16, which reduces the memory and disk space used by an index by half with little or no effect on recall. If you need full, 32-bit floating point numbers in a vector index, you must explicitly request "fixed_quantization":"none" when creating the index.
The quantization options can be specified alongside the other index options in the SECONDARY_ENGINE_ATTRIBUTE variable. The following quantization algorithms are currently supported:
product_quantization(PQ) specifies that product quantization should be used for vector compression.- A value of the form
{"dimensions":X}must be provided, whereXis the number of dimensions to use for the quantization.Xmust be a divisor of the vector’s dimension. As an example, vectors with 1536 dimensions can be quantized to 192 dimensions with minimal losses to recall, but overly small values ofXmay end up affecting the recall of the index. - Product quantization currently only works with the
l2distance metric.
- A value of the form
NoteProduct Quantization is a learned quantization algorithm, which means that an existing vector dataset is required to train the quantization model.
To use Product Quantization on your indexes, begin by inserting a large number of vectors into your table. The more vectors you insert, the better the resulting quantization will be. Then, create the index with the Product Quantization options you want.
Once the index has been created, you can continuously insert, update, and delete vectors from your table, but beware that if the distribution of the new vectors changes significantly (e.g. because they come from a different source), the
recall on the index may be affected.
If you create a Product Quantized index on an empty table, you will need to insert at least 1000 vectors in a single transaction before the index is usable.
"fixed_quantization" : "float16"specifies that fixed quantization into Float16 should be used for vector compression. No extra arguments are required."fixed_quantization" : "bfloat16"specifies that fixed quantization into Brain Float 16 should be used for vector compression. No extra arguments are required. This setting is the default."fixed_quantization" : "none"specifies that no quantization should occur; the index will use full, 32-bit floats. No extra arguments are required."fixed_quantization" : "onebit"specifies that fixed quantization into 1 bit should be used for vector compression. No extra arguments are required.- This is a lossy compression method that can be used to store vectors in a very compact form. It is not suitable for all use cases, as it can significantly affect the recall of the index.
- This quantization method currently only works with the
l2distance metric, which effectively calculates the Hamming Distance between the vectors.
Examples
Vector functions
PlanetScale MySQL includes several new functions for working with vectors.TO_VECTOR(string) or STRING_TO_VECTOR(string)
Converts a text string to a binary vector value. The text string is an array of floating point numbers in JSON format.
Example
FROM_VECTOR(string) or VECTOR_TO_STRING(vector)
Converts a binary vector to a human-readable string.
Example
VECTOR_DIM(string)
Calculates the dimension of a vector.
Example
DISTANCE(vector1, vector2, [metric])
Calculates the distance between vector1 and vector2.
The optional third parameter specifies which distance metric is to be used: dot, cosine, l2 (euclidean), or l2_squared (euclidean_squared).
dotmeans the dot product.cosinemeans the cosine of the angle between the two vectors. This is mathematically defined as the dot product divided by the magnitude of the two vectors, which yields a value between-1and1. Some vector database vendors do additional math on the result to normalize the value to be between0and1or between0and2. Our implementation normalizes the output, and returns a value between0and2. A result of0means the vectors are proportional (point in the same direction) and2means the vectors are opposite.l2(oreuclidean) means the length of a line between the ends of the vectors.l2_squared(oreuclidean_squared) is the square of the Euclidean distance
dot.
Examples
DISTANCE_DOT(vector1, vector2)
Is the same as DISTANCE(vector1, vector2, 'dot')
DISTANCE_COSINE(vector1, vector2)
Is the same as DISTANCE(vector1, vector2, 'cosine')
DISTANCE_L2(vector1, vector2)
Is the same as DISTANCE(vector1, vector2, 'l2')
DISTANCE_EUCLIDEAN(vector1, vector2)
Is the same as DISTANCE(vector1, vector2, 'l2')
DISTANCE_L2_SQUARED(vector1, vector2)
Is the same as DISTANCE(vector1, vector2, 'l2_squared')
DISTANCE_EUCLIDEAN_SQUARED(vector1, vector2)
Is the same as DISTANCE(vector1, vector2, 'l2_squared')

