{"id":17028,"date":"2024-05-21T22:48:13","date_gmt":"2024-05-22T05:48:13","guid":{"rendered":"https:\/\/www.pingcap.com\/?post_type=article&#038;p=17028"},"modified":"2024-05-22T17:23:09","modified_gmt":"2024-05-23T00:23:09","slug":"in-age-of-llm-how-to-store-vectors-with-mysql-sql-grammar","status":"publish","type":"article","link":"https:\/\/www.pingcap.com\/ko\/article\/in-age-of-llm-how-to-store-vectors-with-mysql-sql-grammar\/","title":{"rendered":"In Age of LLM: How to Store Vectors with MySQL SQL Grammar"},"content":{"rendered":"\n<p>As the landscape of artificial intelligence evolves, leveraging large language models (LLMs) for semantic search and AI-driven applications has become increasingly prevalent. One critical component of this new wave is vector search, which allows for the retrieval of data based on its semantic meaning rather than simple keyword matching. This post explores how to store and query vectors in a MySQL-compatible SQL database, <a href=\"https:\/\/tidb.cloud\/\">TiDB Serverless<\/a>, which offers robust <a href=\"https:\/\/www.pingcap.com\/blog\/integrating-vector-search-into-tidb-for-ai-applications\/\">vector search capabilities<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Introduction_to_Vector_Search\"><\/span>Introduction to Vector Search<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Vector search is a technique used to find similar items in a dataset based on their vector representations. These vectors, often referred to as embeddings, capture the semantic essence of the data. Whether you&#8217;re working with text, images, or other data types, embeddings enable the comparison and retrieval of similar items through distance metrics like cosine similarity or Euclidean distance.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Setting_Up_TiDB_for_Vector_Search\"><\/span>Setting Up TiDB for Vector Search<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>TiDB Serverless is a distributed SQL database that integrates vector search functionalities, enabling the storage and querying of vector embeddings directly within the database. Here&#8217;s a step-by-step guide to getting started:<\/p>\n\n\n\n<ol class=\"wp-block-list\" start=\"1\">\n<li><strong>Sign Up for TiDB Serverless:<\/strong>\n<ol class=\"wp-block-list\">\n<li><a href=\"https:\/\/tidb.cloud\/ai\/\">Join the waitlist<\/a> for the private beta of built-in vector search\u00a0in TiDB Serverless.<\/li>\n\n\n\n<li>Visit <a href=\"https:\/\/tidbcloud.com\">tidbcloud.com<\/a> and sign up.<\/li>\n\n\n\n<li>Select the <code>eu-central-1<\/code> region as vector search is currently available there.<\/li>\n\n\n\n<li>Follow the instructions to create a TiDB Serverless cluster with vector support enabled.<\/li>\n<\/ol>\n<\/li>\n\n\n\n<li><strong>Connect to Your TiDB Cluster:<\/strong>\n<ol class=\"wp-block-list\">\n<li>Navigate to the Clusters page on the TiDB Cloud console.<\/li>\n\n\n\n<li>Click on your cluster name to access its overview page.<\/li>\n\n\n\n<li>Use the &#8220;Connect&#8221; option to get the connection details.<\/li>\n<\/ol>\n<\/li>\n<\/ol>\n\n\n<div class=\"ub_call_to_action hide wp-block-ub-call-to-action-block\"  id=\"ub_call_to_action_cf0aa624-720d-4ca1-ab12-eb158115b9ce\">\n                <div class=\"ub_call_to_action_headline\">\n                    <p class=\"ub_call_to_action_headline_text\">Try TiDB Serverless with Vector Search<\/p><\/div>\n                <div class=\"ub_call_to_action_content\">\n                    <p class=\"ub_cta_content_text\">Join the waitlist for the private beta of built-in vector search.<\/p><\/div>\n                <div class=\"ub_call_to_action_button\">\n                    <a href=\"https:\/\/tidb.cloud\/ai\/\" target=\"_self\" rel=\"noopener noreferrer\"\n                        class=\"ub_cta_button\">\n                        <p class=\"ub_cta_button_text\">Join Now<\/p><\/a><\/div><\/div>\n\n\n<div class=\"ub_call_to_action\" id=\"ub_call_to_action_ad880767-759a-411f-a445-b69adc785e49\">\n                <div class=\"ub_call_to_action_headline\">\n                    <p class=\"ub_call_to_action_headline_text\">Try TiDB Serverless with Vector Search<\/p><\/div>\n                <div class=\"ub_call_to_action_content\">\n                    <p class=\"ub_cta_content_text\">Join the waitlist for the private beta of built-in vector search.<\/p><\/div>\n                <div class=\"ub_call_to_action_button\">\n                    <a href=\"https:\/\/tidb.cloud\/ai\/\" target=\"_blank\" rel=\"noopener noreferrer\" class=\"ub_cta_button external-link\" data-gtag=\"event:go_to_lead_form_page,product_type:serverless,button_name:Join the Waitlist,position:article_middle_cta\">\n                        <p class=\"ub_cta_button_text\" data-gtag=\"event:go_to_lead_form_page,product_type:serverless,button_name:Join the Waitlist,position:article_middle_cta\">Join Now<\/p><\/a><\/div><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">Creating and Managing Vector Data in TiDB<\/h3>\n\n\n\n<p>To illustrate how to store and query vectors, let&#8217;s walk through creating a table, inserting data, and performing a vector search.<\/p>\n\n\n\n<p><strong>Create a Table with Vector Field:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>CREATE TABLE vector_table (\n    id INT PRIMARY KEY,\n    doc TEXT,\n    embedding VECTOR(3)\n);<\/code><\/pre>\n\n\n\n<p><strong>Insert Vector Data:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>INSERT INTO vector_table (id, doc, embedding) VALUES\n(1, 'apple', '&#91;1,1,1]'),\n(2, 'banana', '&#91;1,1,2]'),\n(3, 'dog', '&#91;2,2,2]');<\/code><\/pre>\n\n\n\n<p><strong>Querying for Nearest Neighbors:<\/strong><\/p>\n\n\n\n<p>To find vectors closest to a given vector <code>[1,1,3]<\/code>, you can use cosine similarity:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT * FROM vector_table ORDER BY vec_cosine_distance(embedding, '&#91;1,1,3]') LIMIT 3;<\/code><\/pre>\n\n\n\n<p><strong>Creating an HNSW Index:<\/strong><\/p>\n\n\n\n<p>To optimize vector searches, you can create an HNSW (Hierarchical Navigable Small World) index:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>CREATE TABLE vector_table_with_index (\n    id INT PRIMARY KEY,\n    doc TEXT,\n    embedding VECTOR(3) COMMENT 'hnsw(distance=cosine)'\n);<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Using TiDB Vector Functions<\/h3>\n\n\n\n<p>TiDB supports various vector functions for calculating distances and transforming vectors:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Vector Distance Functions:<\/strong><\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT vec_cosine_distance('&#91;1,1,1]', '&#91;1,2,3]');\nSELECT vec_l1_distance('&#91;1,1,1]', '&#91;1,2,3]');\nSELECT vec_l2_distance('&#91;1,1,1]', '&#91;1,2,3]');<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Vector Transformation Functions:<\/strong><\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT vec_from_text('&#91;1,1,1]');\nSELECT vec_as_text('&#91;1,1,1]');<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Norm Calculation:<\/strong><\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT vec_l2_norm('&#91;1,2,3]');<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Integrating with AI Frameworks<\/h3>\n\n\n\n<p>TiDB seamlessly integrates with AI frameworks like LangChain and LlamaIndex, making it easier to implement advanced AI applications. These integrations allow for efficient vector storage and retrieval, essential for AI-driven workflows.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Conclusion\"><\/span>Conclusion<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Storing and querying vectors in a MySQL-compatible database like TiDB offers a powerful way to leverage semantic search capabilities in your applications. With robust support for vector functions and integration with popular AI frameworks, TiDB provides a comprehensive solution for handling vector data in the age of LLMs.<\/p>\n\n\n\n<p class=\"has-medium-font-size\"><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\"><strong>Try to store vectors with MySQL SQL grammar in TiDB Serverless<em>.<\/em><\/strong><\/mark><\/p>\n\n\n\n<p><a href=\"https:\/\/tidb.cloud\/ai\/\" class=\"button\" target=\"_blank\" data-gtag=\"event:go_to_lead_form_page,product_type:serverless,button_name:Join the Waitlist,position:article_bottom_cta\" rel=\"noopener\">Join the Waitlist<\/a><br><\/p>&nbsp;\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p class=\"has-medium-font-size\"><strong>Additional Resources<\/strong>:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/www.pingcap.com\/article\/mysql-vector-search-powering-the-future-of-ai-applications\/\">MySQL Vector Search: Powering the Future of AI Applications<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/www.pingcap.com\/article\/unlocking-semantic-search-with-tidb-serverless\/\">Unlocking Semantic Search with TiDB Serverless<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/www.pingcap.com\/article\/revolutionizing-mysql-with-vector-similarity-search\/\">Revolutionizing MySQL with Vector Similarity Search<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>As the landscape of artificial intelligence evolves, leveraging large language models (LLMs) for semantic search and AI-driven applications has become increasingly prevalent. One critical component of this new wave is vector search, which allows for the retrieval of data based on its semantic meaning rather than simple keyword matching. This post explores how to store [&hellip;]<\/p>\n","protected":false},"author":8,"featured_media":0,"template":"","class_list":["post-17028","article","type-article","status-publish","hentry"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.9 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>In Age of LLM: How to Store Vectors with MySQL SQL Grammar | TiDB<\/title>\n<meta name=\"description\" content=\"This post explores how to store and query vectors in a MySQL-compatible database, TiDB, which offers robust vector search capabilities.\" \/>\n<meta name=\"robots\" content=\"noindex, follow\" \/>\n<meta property=\"og:locale\" content=\"ko_KR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"In Age of LLM: How to Store Vectors with MySQL SQL Grammar | TiDB\" \/>\n<meta property=\"og:description\" content=\"This post explores how to store and query vectors in a MySQL-compatible database, TiDB, which offers robust vector search capabilities.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.pingcap.com\/ko\/article\/in-age-of-llm-how-to-store-vectors-with-mysql-sql-grammar\/\" \/>\n<meta property=\"og:site_name\" content=\"TiDB\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/facebook.com\/pingcap2015\" \/>\n<meta property=\"article:modified_time\" content=\"2024-05-23T00:23:09+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/static.pingcap.com\/files\/2024\/09\/11005522\/Homepage-Ad.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1440\" \/>\n\t<meta property=\"og:image:height\" content=\"714\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:site\" content=\"@PingCAP\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"3\ubd84\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.pingcap.com\/article\/in-age-of-llm-how-to-store-vectors-with-mysql-sql-grammar\/\",\"url\":\"https:\/\/www.pingcap.com\/article\/in-age-of-llm-how-to-store-vectors-with-mysql-sql-grammar\/\",\"name\":\"In Age of LLM: How to Store Vectors with MySQL SQL Grammar | TiDB\",\"isPartOf\":{\"@id\":\"https:\/\/www.pingcap.com\/#website\"},\"datePublished\":\"2024-05-22T05:48:13+00:00\",\"dateModified\":\"2024-05-23T00:23:09+00:00\",\"description\":\"This post explores how to store and query vectors in a MySQL-compatible database, TiDB, which offers robust vector search capabilities.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.pingcap.com\/article\/in-age-of-llm-how-to-store-vectors-with-mysql-sql-grammar\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.pingcap.com\/article\/in-age-of-llm-how-to-store-vectors-with-mysql-sql-grammar\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.pingcap.com\/article\/in-age-of-llm-how-to-store-vectors-with-mysql-sql-grammar\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.pingcap.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Articles\",\"item\":\"https:\/\/www.pingcap.com\/article\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"In Age of LLM: How to Store Vectors with MySQL SQL Grammar\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.pingcap.com\/#website\",\"url\":\"https:\/\/www.pingcap.com\/\",\"name\":\"TiDB\",\"description\":\"TiDB | SQL at Scale\",\"publisher\":{\"@id\":\"https:\/\/www.pingcap.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.pingcap.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"ko-KR\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.pingcap.com\/#organization\",\"name\":\"PingCAP\",\"url\":\"https:\/\/www.pingcap.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"ko-KR\",\"@id\":\"https:\/\/www.pingcap.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/static.pingcap.com\/files\/2021\/11\/pingcap-logo.png\",\"contentUrl\":\"https:\/\/static.pingcap.com\/files\/2021\/11\/pingcap-logo.png\",\"width\":811,\"height\":232,\"caption\":\"PingCAP\"},\"image\":{\"@id\":\"https:\/\/www.pingcap.com\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/facebook.com\/pingcap2015\",\"https:\/\/x.com\/PingCAP\",\"https:\/\/linkedin.com\/company\/pingcap\",\"https:\/\/youtube.com\/channel\/UCuq4puT32DzHKT5rU1IZpIA\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"In Age of LLM: How to Store Vectors with MySQL SQL Grammar | TiDB","description":"This post explores how to store and query vectors in a MySQL-compatible database, TiDB, which offers robust vector search capabilities.","robots":{"index":"noindex","follow":"follow"},"og_locale":"ko_KR","og_type":"article","og_title":"In Age of LLM: How to Store Vectors with MySQL SQL Grammar | TiDB","og_description":"This post explores how to store and query vectors in a MySQL-compatible database, TiDB, which offers robust vector search capabilities.","og_url":"https:\/\/www.pingcap.com\/ko\/article\/in-age-of-llm-how-to-store-vectors-with-mysql-sql-grammar\/","og_site_name":"TiDB","article_publisher":"https:\/\/facebook.com\/pingcap2015","article_modified_time":"2024-05-23T00:23:09+00:00","og_image":[{"width":1440,"height":714,"url":"https:\/\/static.pingcap.com\/files\/2024\/09\/11005522\/Homepage-Ad.png","type":"image\/png"}],"twitter_card":"summary_large_image","twitter_site":"@PingCAP","twitter_misc":{"Est. reading time":"3\ubd84"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.pingcap.com\/article\/in-age-of-llm-how-to-store-vectors-with-mysql-sql-grammar\/","url":"https:\/\/www.pingcap.com\/article\/in-age-of-llm-how-to-store-vectors-with-mysql-sql-grammar\/","name":"In Age of LLM: How to Store Vectors with MySQL SQL Grammar | TiDB","isPartOf":{"@id":"https:\/\/www.pingcap.com\/#website"},"datePublished":"2024-05-22T05:48:13+00:00","dateModified":"2024-05-23T00:23:09+00:00","description":"This post explores how to store and query vectors in a MySQL-compatible database, TiDB, which offers robust vector search capabilities.","breadcrumb":{"@id":"https:\/\/www.pingcap.com\/article\/in-age-of-llm-how-to-store-vectors-with-mysql-sql-grammar\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.pingcap.com\/article\/in-age-of-llm-how-to-store-vectors-with-mysql-sql-grammar\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.pingcap.com\/article\/in-age-of-llm-how-to-store-vectors-with-mysql-sql-grammar\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.pingcap.com\/"},{"@type":"ListItem","position":2,"name":"Articles","item":"https:\/\/www.pingcap.com\/article\/"},{"@type":"ListItem","position":3,"name":"In Age of LLM: How to Store Vectors with MySQL SQL Grammar"}]},{"@type":"WebSite","@id":"https:\/\/www.pingcap.com\/#website","url":"https:\/\/www.pingcap.com\/","name":"\ud2f0DB","description":"TiDB | SQL at Scale","publisher":{"@id":"https:\/\/www.pingcap.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.pingcap.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"ko-KR"},{"@type":"Organization","@id":"https:\/\/www.pingcap.com\/#organization","name":"PingCAP","url":"https:\/\/www.pingcap.com\/","logo":{"@type":"ImageObject","inLanguage":"ko-KR","@id":"https:\/\/www.pingcap.com\/#\/schema\/logo\/image\/","url":"https:\/\/static.pingcap.com\/files\/2021\/11\/pingcap-logo.png","contentUrl":"https:\/\/static.pingcap.com\/files\/2021\/11\/pingcap-logo.png","width":811,"height":232,"caption":"PingCAP"},"image":{"@id":"https:\/\/www.pingcap.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/facebook.com\/pingcap2015","https:\/\/x.com\/PingCAP","https:\/\/linkedin.com\/company\/pingcap","https:\/\/youtube.com\/channel\/UCuq4puT32DzHKT5rU1IZpIA"]}]}},"card_markup":"        <a class=\"card-article\" href=\"https:\/\/www.pingcap.com\/ko\/article\/in-age-of-llm-how-to-store-vectors-with-mysql-sql-grammar\/\">            <h3>In Age of LLM: How to Store Vectors with MySQL SQL Grammar<\/h3>            <p>As the landscape of artificial intelligence evolves, leveraging large language models (LLMs) for semantic search and AI-driven applications has become increasingly prevalent. One critical component of this new wave is vector search, which allows for the retrieval of data based on its semantic meaning rather than simple keyword matching. This post explores how to store [&hellip;]<\/p>        <\/a>","_links":{"self":[{"href":"https:\/\/www.pingcap.com\/ko\/wp-json\/wp\/v2\/article\/17028","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.pingcap.com\/ko\/wp-json\/wp\/v2\/article"}],"about":[{"href":"https:\/\/www.pingcap.com\/ko\/wp-json\/wp\/v2\/types\/article"}],"author":[{"embeddable":true,"href":"https:\/\/www.pingcap.com\/ko\/wp-json\/wp\/v2\/users\/8"}],"wp:attachment":[{"href":"https:\/\/www.pingcap.com\/ko\/wp-json\/wp\/v2\/media?parent=17028"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}