[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["難以理解","hardToUnderstand","thumb-down"],["資訊或程式碼範例有誤","incorrectInformationOrSampleCode","thumb-down"],["缺少我需要的資訊/範例","missingTheInformationSamplesINeed","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-09-09 (世界標準時間)。"],[],[],null,["# Search and filter with vector embeddings\n\n\u003cbr /\u003e\n\nMySQL \\| PostgreSQL \\| SQL Server\n\n\u003cbr /\u003e\n\n| To use the GA version of vector search, the instance maintenance version should be version `MYSQL_8_0_version.R20241208.01_00` or newer, where version is the minor version number. For information about how to upgrade your instance to a newer version that supports GA vector embeddings, see [Self-service maintenance](/sql/docs/mysql/self-service-maintenance). \n|\n| For information about the syntax and behavior of vector embeddings for the Public Preview version of this feature, see [Work with vector embeddings (Preview)](/sql/docs/mysql/work-with-vectors-preview).\n\nThis page describes the different ways that you can query vector embeddings. For\nan overview of ANN and KNN similarity searches, see\n[vector searches](/sql/docs/mysql/vector-search#vector_similarity_search_2).\n\nSearch approximate nearest neighbors (ANN)\n------------------------------------------\n\nTo perform an ANN search, use the\n[`approx_distance` function](/sql/docs/mysql/functions#ann_function)\nin a `SELECT` and `ORDER BY` clause. You must use a `LIMIT` clause in an ANN\nsearch. You can also get the distance value by putting `approx_distance` in a\n`SELECT` list.\n\nUse the following syntax for ANN queries: \n\n # Ordering by distance\n SELECT title\n FROM books\n ORDER BY approx_distance(embedding, string_to_vector('[1,2,3]'), 'distance_measure=l2_squared')\n LIMIT 4;\n\n # Selecting the distance value\n SELECT\n approx_distance(\n embedding_name,\n string_to_vector('[1,2,3]'),\n 'distance_measure=cosine,num_leaves_to_search=3')\n dist\n FROM table\n ORDER BY dist\n LIMIT limit_value;\n\nThe `approx_distance` function uses the following options:\n\n- **`embedding`**: uses the vector embedding column name from the base table.\n- **`string_to_vector`** or **`vector_to_string`**: converts a vector to a string and a string to a vector to make the vector human readable.\n- **`distance_measure`** : specify the distance measure to use for a vector similarity search. This value must match the value you set in the `distance_measure` parameter when you created the index. This parameter is required. Possible values for this parameter are:\n - `COSINE`\n - `L2_SQUARED`\n - `DOT_PRODUCT`\n- **`num_leaves_to_search`** : optional. specifies the number of leaves to probe for an ANN vector similarity search. If you don't specify the number of leaves, then Cloud SQL uses a value generated based on the size of the table, number of leaves in the vector index, and other factors. You can view this value in `information_schema.innodb_vector_indexes`. We recommend that you fine-tune `num_leaves_to_search` to achieve the best balance between search quality and performance for your specific workload. If increased, it impacts performance but improves recall.\n\nThe following example shows how to use `approx_distance` to find the top K\nclosest rows using the `l2_squared` distance measure and order the results by\ndistance. \n\n # Ordering by distance\n SELECT title\n FROM books\n ORDER BY approx_distance(embedding, string_to_vector('[1,2,3]'),\n 'distance_measure=l2_squared')\n LIMIT 4;\n\n # Selecting the distance value\n SELECT\n approx_distance\n (embedding, string_to_vector('[1,2,3]'),\n 'distance_measure=l2_squared') dist\n FROM table\n ORDER BY dist\n LIMIT 4;\n\n### Filter results from approx_distance queries\n\nYou can use the `approx_distance` function with `WHERE` conditions that filter\nquery results with a non-vector predicate to perform post filtering. The\n`approx_distance` function is evaluated before applying the filter which means\nthat the number of results returned are nondeterministic.\n\nFor example, for the following query: \n\n SELECT id FROM products WHERE price \u003c 100\n ORDER BY approx(embedding, @query_vector,'distance_measure=cosine')\n LIMIT 11;\n\nThe `approx_distance` function returns the 11 nearest neighbors to the query\nvector regardless of price. In post filtering, the products with a price\n\\\u003c 100 are selected. It's possible that all of the nearest neighbors have a\nprice \\\u003c 100, so there are 11 results to the query. Alternatively, if none\nof the nearest neighbors have a price \\\u003c 100, there are 0 rows returned.\n\nIf you anticipate that your filter in the `WHERE` condition is very selective, an\nexact search (KNN) might be a better option to ensure that a sufficient number\nof rows are returned.\n\n### Check the fallback status on ANN searches\n\nThere are certain cases where an ANN search falls back to a KNN search. These\ninclude the following:\n\n- There's no vector index on the base table.\n- There's a vector index on the base table, but it uses a different distance measure from the `distance_measure` parameter in the `approx_distance` search options.\n- The vector index is corrupt or invisible to the current transaction.\n- The `LIMIT` specified is greater than 10000.\n- There is no `LIMIT` specified.\n- The current query involves more than one `approx_distance` call on the same base table.\n- The optimizer calculates that it's more efficient to use KNN.\n\nAll of these cases push a warning to the client indicating that exact search was\nperformed and the reason why.\n\nUse the following command in the mysql client to view the fallback status: \n\n SHOW global status LIKE '%cloudsql_vector_knn_fallback%';\n\nIf you want to use ANN and it's falling back to KNN, the query might run slower.\nYou should find the reason it's falling back\nand assess whether to make changes so that ANN is used instead.\n\n### Example: Create a vector index and run an ANN query\n\nThe following example walkthrough provides steps to create a vector index and\nrun an ANN query in Cloud SQL.\n\n1. Generate vector embeddings. You can create vector embeddings manually or use a text embedding API of your choice. For an example that uses Vertex AI, see [Generate vector embeddings based on row data](/sql/docs/mysql/generate-manage-vector-embeddings#generate_vector_embeddings_based_on_row_data).\n2. Create a table in Cloud SQL that contains a vector embedding\n column with three dimensions.\n\n CREATE TABLE books(\n id INTEGER PRIMARY KEY AUTO_INCREMENT, title VARCHAR(60), embedding VECTOR(3) USING VARBINARY);\n\n3. Insert a vector embedding into the column.\n\n INSERT INTO books VALUES ((1, 'book title', string_to_vector('[1,2,3]')));\n\n4. Commit the changes.\n\n commit;\n\n5. Create the vector index using the `L2_squared` function to measure distance.\n\n CREATE\n VECTOR INDEX vectorIndex\n ON dbname.books(embeddings)\n USING SCANN QUANTIZER = SQ8 DISTANCE_MEASURE = l2_squared;\n\n6. Use the following syntax to perform an ANN search with a `LIMIT` of 4 search\n results:\n\n SELECT title\n FROM books\n ORDER BY approx_distance(embedding, string_to_vector('[1,2,3]'), 'distance_measure=l2_squared')\n LIMIT 4;\n\n SELECT approx_distance(embedding, string_to_vector('[1,2,3]'), 'distance_measure=cosine') dist\n FROM books\n ORDER BY dist\n LIMIT 4;\n\nSearch K-nearest neighbors (KNN)\n--------------------------------\n\nTo perform a K-nearest neighbor search, use the\n[`vector_distance` function](/sql/docs/mysql/functions#knn_functions)\nwith a distance measure option and a vector conversion function\n(`string_to_vector` or `vector_to_string`) in a `SELECT` statement. Use the\nfollowing syntax: \n\n SELECT vector_distance(string_to_vector('[1,2,3]'),\n string_to_vector('[1,2,3]'),\n 'Distance_Measure=dot_product');\n\nReplace the values \\[1,2,3\\] with the embedding values of your data.\n\nThe following example shows how to use this query with the `cosine_distance`\nfunction and the `string_to_vector` vector conversion function. \n\n SELECT id,cosine_distance(embedding, string_to_vector('[1,2,3]')) dist\n FROM books\n ORDER BY distance\n LIMIT 10;\n\n### Get the Cosine distance in a KNN query\n\nUse the Cloud SQL\n[`cosine_distance` function](/sql/docs/mysql/functions#knn_functions)\nto calculate the distance using cosine. \n\n SELECT cosine_distance(embedding, string_to_vector('[3,1,2]')) AS distance FROM books WHERE id = 10;\n\n### Get the Dot Product distance in a KNN query\n\nUse the Cloud SQL\n[`dot_product` function](/sql/docs/mysql/functions#dot_product)\nto calculate the distance using the dot product. \n\n SELECT dot_product(embedding, string_to_vector('[3,1,2]')) AS distance FROM books WHERE id = 10;\n\n### Get the L2-squared distance in a KNN query\n\nUse the Cloud SQL\n[`l2_squared_distance` function](/sql/docs/mysql/functions#knn_functions)\nto calculate the distance using L2 squared. \n\n SELECT\n l2_squared_distance(embedding, string_to_vector('[3,1,2]'))\n AS distance\n FROM books\n WHERE id = 10;\n\nWhat's next\n-----------\n\n- Read the [overview about vector search on Cloud SQL](/sql/docs/mysql/vector-search).\n- Learn how to [enable and disable vector embeddings on your instance](/sql/docs/mysql/enable-vector-search).\n- Learn how to [generate vector embeddings](/sql/docs/mysql/generate-manage-vector-embeddings#generate_vector_embeddings_based_on_row_data).\n- Learn how to [create vector indexes](/sql/docs/mysql/create-manage-vector-indexes).\n- Learn how to [perform searches on vector embeddings](/sql/docs/mysql/search-filter-vector-embeddings)."]]