GCP – How to integrate your Cloud SQL for MySQL database with Vertex AI & vector search
Search is a critical component of many modern applications – whether searching for products in an online storefront, finding solutions to your customers’ support cases, or building the perfect playlist. But traditional keyword searches often miss the deeper meaning of data. Vector embeddings, however, capture the complexities of your data, enabling highly accurate and powerful searches. Vector embeddings are numerical representations of your data, generated by models, that help computers understand nuances and meaningfully compare data points.
Cloud SQL for MySQL’s generally available vector support allows you to store vector embeddings, build persistent indexes on them, and perform ANN search between them. Now, we have a new Vertex AI integration that makes this process easy by helping you generate vector embeddings using a simple SQL function, eliminating the need for external embedding generation. You can also leverage any Vertex AI model directly from MySQL, bringing advanced AI capabilities closer to your data.
Ready to see how AI can improve searches in your application and help you understand and predict customer trends? Let’s dive in.
- aside_block
- <ListValue: [StructValue([(‘title’, ‘$300 in free credit to try Google Cloud AI and ML’), (‘body’, <wagtail.rich_text.RichText object at 0x3ea410747b50>), (‘btn_text’, ‘Start building for free’), (‘href’, ‘http://console.cloud.google.com/freetrial?redirectPath=/vertex-ai/’), (‘image’, None)])]>
Generate, store, & search vector embeddings
Let’s say you have an ecommerce application and are looking to see how Vertex AI and vector search with Cloud SQL for MySQL can power product searches and help you analyze customer reviews.
After setting up Vertex AI permissions and enabling the feature, you can start embedding your existing product data with the `mysql.ml_embedding` function. You just need to specify the model name and then pass the text you would like to embed–all formatting and parsing of Vertex AI requests is handled for you.
For example, in your ecommerce application you can use a query like the following to directly store the output of `mysql.ml_embedding` in a VECTOR type column:
- code_block
- <ListValue: [StructValue([(‘code’, “ALTER TABLE my_products ADD COLUMN embedding vector(768) using varbinary;rnrn– In this example, product_description can be a text column describing the product in a given rowrnUPDATE my_products SET embedding = mysql.ml_embedding(‘text-embedding-005’, product_description);”), (‘language’, ‘lang-sql’), (‘caption’, <wagtail.rich_text.RichText object at 0x3ea4107474c0>)])]>
To quickly find similar products, create a vector index for ANN search. You can select the best distance measure for your use case and customize the number of leaves to fine-tune speed and recall.
- code_block
- <ListValue: [StructValue([(‘code’, ‘CREATE VECTOR INDEX my_products_embedding_idx ON my_products(embedding) USING SCANN DISTANCE_MEASURE=COSINE NUM_LEAVES=500;’), (‘language’, ‘lang-sql’), (‘caption’, <wagtail.rich_text.RichText object at 0x3ea410747f70>)])]>
When a customer searches in your application, you can embed their query using `mysql.ml_embedding` and pass it to ANN search. For example, to find clothes for a specific occasion in your database:
- code_block
- <ListValue: [StructValue([(‘code’, “SELECT mysql.ml_embedding(‘text-embedding-005′,’Dress for a springtime black-tie wedding’) into @query_vector;rnrnSELECTrn approx_distance(embedding ,@query_vector, ‘distance_measure=cosine’) as distancernFROMrn my_productsrnORDER BYrn distance ASCrnLIMIT 5;”), (‘language’, ‘lang-sql’), (‘caption’, <wagtail.rich_text.RichText object at 0x3ea410747e20>)])]>
Get responses from Gemini & other Vertex AI models
The Cloud SQL Vertex AI integration lets you make requests to any Vertex AI endpoint, including custom models fine-tuned to your datasets or pre-trained models like Gemini. This means that in addition to being able to generate vector embeddings for product descriptions, you can use Vertex AI to make predictions about things like what your users will purchase or whether a purchase is fraud.
For example, say you store customer reviews of your products in a MySQL database and want to perform analysis on them to get an idea of customer sentiment. You can invoke a Vertex AI LLM to determine customer satisfaction directly from the MySQL client and store its assessment.
- code_block
- <ListValue: [StructValue([(‘code’, ‘SELECTrn mysql.ml_predict_row(rn ‘publishers/google/models/gemini-2.0-flash:generateContent’,rn CONCAT(rn'{ “contents”: [{ “role”: “user”, “parts”:[rn {rn “text”:rn “Please rate customer sentiment from 1-10 based on the following review. Only output the number, nothing else. Text: \n’, review_content, ‘ “rn }]}]}rn ‘)) from customer_reviews;’), (‘language’, ”), (‘caption’, <wagtail.rich_text.RichText object at 0x3ea410747c40>)])]>
Start building
Want to get started with the MySQL Vertex AI integration after seeing how it enhances product search and analysis? Explore our detailed codelab for an embeddings workflow tutorial or dive into our documentation for a comprehensive overview of all capabilities.
Read More for the details.