このページでは、mysql.ml_embedding() 関数がテーブルに格納されているデータと mysql vector クエリ機能とでどのように機能するかを示すワークフローの例を示します。この例では、プレーンテキストの入力を使用して、テキストの意味の解析を大規模言語モデル(LLM)に依存するデータベースから結果を取得します。
たとえば、SELECT * FROM item WHERE complaints LIKE
"%wrong color%" などの基本的な SQL クエリでは、complaints フィールドに The picture shows a blue one, but the one I received was red のみが含まれる行は返されません。
It was the wrong color のテキストとの意味的な近さでテーブルの行を並べ替えます。
上位 10 件の苦情を取得します。
このクエリでは、最初に並べ替えられた行の id 値と name 値が表示されます。
SELECTmysql.ML_EMBEDDING('text-embedding-005','It was the wrong color')into@query_vector;selectid,namefromitemsorderbyapprox_distance(complaint_embedding,@query_vector,'distance_measure=cosine')limit10;
[[["わかりやすい","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-08-28 UTC。"],[],[],null,["# Understand an example of an embedding workflow\n\n\u003cbr /\u003e\n\nMySQL \\| [PostgreSQL](/sql/docs/postgres/understand-example-embedding-workflow \"View this page for the PostgreSQL database engine\") \\| SQL Server\n\n\u003cbr /\u003e\n\n|\n| **Preview\n| --- [Cloud SQL for MySQL integration with Vertex AI](/sql/docs/mysql/integrate-cloud-sql-with-vertex-ai)**\n|\n|\n| This feature is subject to the \"Pre-GA Offerings Terms\" in the General Service Terms section\n| of the [Service Specific Terms](/terms/service-terms#1).\n|\n| You can process personal data for this feature as outlined in the\n| [Cloud Data Processing\n| Addendum](/terms/data-processing-addendum), subject to the obligations and restrictions described in the agreement under\n| which you access Google Cloud.\n|\n| Pre-GA features are available \"as is\" and might have limited support.\n|\n| For more information, see the\n| [launch stage descriptions](/products#product-launch-stages).\n\nThis page provides an example of a workflow that demonstrates how the\n[`mysql.ml_embedding()`](/sql/docs/mysql/work-with-vectors#generate-an-embedding) function works\nwith the data that's stored in your tables and the\n[`mysql vector` query functionality](/sql/docs/mysql/work-with-vectors#query). The example\nuses plain-text input to fetch a result from a database that relies on large\nlanguage model (LLM)-driven semantic parsing of the text's meaning.\n\nAn example scenario of an embedding workflow\n--------------------------------------------\n\nImagine a database running on Cloud SQL for MySQL with the following aspects:\n\n- The database contains a table: `items`. Each row in this table describes an\n item that your business sells.\n\n- The `items` table contains a column: `complaints`. This column stores\n buyer complaints that are logged about each item as plain text.\n\n- The database integrates with the [Vertex AI\n Model Garden](/model-garden), giving it access to the `textembedding-gecko` LLM.\n\nEven though this database stores complaints about items, these complaints are\nstored as plain text, making it challenging to query. For example, if you want\nto see which items have the most complaints from customers who received the\nwrong color of merchandise, then you can perform ordinary SQL queries on the\ntable, looking for various keyword matches. However, this approach matches only\nrows that contain those exact keywords.\n\nFor example, a basic SQL query such as `SELECT * FROM item WHERE complaints LIKE\n\"%wrong color%\"` doesn't return a row where the `complaints` field contains only\n`The picture shows a blue one, but the one I received was red`.\n\nSQL queries using LLM-powered embeddings can help bridge this gap. By\napplying embeddings, you can query the table in this example for items where\ncomplaints have semantic similarity to a given text prompt, such as \"It was the\nwrong color\".\n\nThe following steps show how to enable this in the example scenario described\nearlier.\n\nPrepare the table\n-----------------\n\nBefore you run LLM-based queries on the content of the `items` table, you must\nprepare the table to store and index embeddings based on your existing\ndata.\n\n### Create a column to store embeddings\n\nAdd a column to the table to store embeddings.\n`sql\nALTER TABLE items ADD COLUMN complaint_embedding vector(768) using varbinary;`\n\n\u003cbr /\u003e\n\nThis example specifies `768` as an argument because that's how many dimensions the `textembedding-gecko` LLM supports. For more information, see\n[Generate an embedding](/sql/docs/mysql/work-with-vectors#generate-an-embedding).\n\nThe example applies the `vector` data type to the column to simplify using `pgvector` functions and operators with the column's values.\n\n### Populate the new column\n\nUse the `mysql.ml_embedding()`\nfunction to populate this new column with embeddings based\non the value of each row's text that appears in the `complaints` column. In this example,\nCloud SQL generates the embeddings using the LLM with the ID of\n`textembedding-gecko`, version `004`. \n\n UPDATE items SET complaint_embedding = mysql.ml_embedding('text-embedding-005', complaints);\n\nThis example casts the binary return value of `mysql.ml_embedding()` into a `vector` value implicitly to store the value in the `vector` column that you created in [Create a column to store embeddings](#create-column).\n\n\u003cbr /\u003e\n\n### Create an index\n\nTo improve performance, add an index to the `items` table. \n\n CREATE VECTOR INDEX complaint_embed_idx ON items(complaint_embedding)\n USING SCANN DISTANCE_MEASURE=COSINE;\n\nFor more information on creating this type of index, see [Create a nearest-neighbor index](/sql/docs/mysql/work-with-vectors#index). Also, for more information on tuning the index by setting parameters, see\n[Query and index embeddings](/sql/docs/mysql/search-filter-vector-embeddings).\n\nRun LLM-powered queries with provided text\n------------------------------------------\n\nYou can now make semantic nearest-neighbor queries on the `items` table. The\nfollowing query uses the `approx_distance` function\nto complete the following actions:\n\n- Sort the table's rows on semantic proximity to the text of `It was the wrong color`.\n- Return the top ten complaints.\n\nThe query displays the `id` and `name` values of the first sorted row. \n\n SELECT mysql.ML_EMBEDDING('text-embedding-005', 'It was the wrong color') into @query_vector;\n\n select id, name from items order by approx_distance(complaint_embedding , @query_vector,'distance_measure=cosine') limit 10;\n\nWhat's next\n-----------\n\n- [Build generative AI applications using Cloud SQL](/sql/docs/mysql/ai-overview)\n\n- [Integrate Cloud SQL with Vertex AI](/sql/docs/mysql/integrate-cloud-sql-with-vertex-ai)\n\n- [Work with vector embeddings](/sql/docs/mysql/work-with-vectors)\n\n- [Invoke online predictions from Cloud SQL instances](/sql/docs/mysql/invoke-online-predictions)\n\n- [Build LLM-powered applications using LangChain](/sql/docs/mysql/langchain)"]]