{"id":17028,"date":"2026-10-05T09:57:05","date_gmt":"2026-10-05T16:57:05","guid":{"rendered":"https:\/\/www.pingcap.com\/?post_type=article&#038;p=17028"},"modified":"2026-10-05T10:01:19","modified_gmt":"2026-10-05T17:01:19","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 class=\"wp-block-paragraph\">Adding vector search to an application usually means adopting a new client library and a new query language on top of the SQL your team already knows. <a href=\"https:\/\/www.pingcap.com\/what-is-tidb\/\">TiDB<\/a> takes a different approach. Vector storage and search use familiar MySQL syntax, extended with a native VECTOR data type and a small set of distance functions. If you can write a MySQL query, you only need to learn those additions to store and search vectors in TiDB.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"How_TiDB_Solves_the_Second-Query-Language_Problem\"><\/span><strong>How TiDB Solves the Second-Query-Language Problem<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">A dedicated vector database usually ships its own client library and query syntax, separate from the SQL interface your application already speaks. TiDB avoids that split by extending its SQL parser and type system with a native VECTOR data type and distance functions such as VEC_COSINE_DISTANCE. Vectors travel over the standard MySQL protocol as ordinary string literals, so existing MySQL drivers connect without changes. ORMs get vector support through lightweight integration packages rather than a separate vector database SDK.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The vector type and functions are TiDB extensions, not part of MySQL itself. Everything around them is SQL your team already writes: CREATE TABLE, INSERT, SELECT, ORDER BY, and LIMIT.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/www.pingcap.com\/case-study\/dify-consolidates-massive-database-containers-into-one-unified-system-with-tidb\/\">Dify<\/a>, the open-source LLM app development platform, consolidated nearly half a million database containers into a single TiDB Cloud deployment. It now runs vector search, knowledge graphs, and operational data in one system.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Setting_Up_TiDB_for_Vector_Search\"><\/span><strong>Setting Up TiDB for Vector Search<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Vector search is available on TiDB Cloud Starter, TiDB Cloud Essential, TiDB Cloud Dedicated, and TiDB Self-Managed. On Dedicated and Self-Managed, the cluster must run TiDB v8.4.0 or later, and v8.5.0 or later is recommended.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The quickest path is TiDB Cloud Starter. Create a cluster, connect with any MySQL client or driver, and start creating vector columns right away. No separate client library is required.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Creating_and_Querying_Vector_Data_in_TiDB\"><\/span><strong>Creating and Querying Vector Data in TiDB<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Create a table with a vector column the same way you would create any other table, specifying a dimension that matches your embedding model:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>CREATE TABLE vector_table (<br>&nbsp; &nbsp; id INT PRIMARY KEY,<br>&nbsp; &nbsp; doc TEXT,<br>&nbsp; &nbsp; embedding VECTOR(1536)<br>);<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Insert rows the same way you would insert any other row, passing the embedding as a vector literal:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>-- Vectors are truncated for readability.<br>-- Supply all 1536 values, or the insert fails on a dimension mismatch.<br>INSERT INTO vector_table (id, doc, embedding) VALUES<br>&nbsp; &nbsp; (1, 'TiDB is a distributed SQL database.', '&#91;0.01, 0.02, 0.03, ...]'),<br>&nbsp; &nbsp; (2, 'TiFlash is the columnar storage engine.', '&#91;0.04, 0.02, 0.01, ...]'),<br>&nbsp; &nbsp; (3, 'TiKV is the row-based storage engine.', '&#91;0.02, 0.05, 0.01, ...]');<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Query by similarity by ordering on VEC_COSINE_DISTANCE and limiting the result count:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT id, doc, VEC_COSINE_DISTANCE(embedding, '&#91;0.01, 0.02, 0.03, ...]') AS distance<br>FROM vector_table<br>ORDER BY VEC_COSINE_DISTANCE(embedding, '&#91;0.01, 0.02, 0.03, ...]')<br>LIMIT 3;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Without an index, this query compares the query vector against every row. Results are exact, but the query slows down as the table grows. For larger tables, add an HNSW index on the vector column:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>CREATE TABLE vector_table_indexed (<br>&nbsp; &nbsp; id INT PRIMARY KEY,<br>&nbsp; &nbsp; doc TEXT,<br>&nbsp; &nbsp; embedding VECTOR(1536),<br>&nbsp; &nbsp; VECTOR INDEX idx_embedding ((VEC_COSINE_DISTANCE(embedding)))<br>);<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The vector index runs on TiFlash, TiDB&#8217;s columnar engine. Defining the index in CREATE TABLE, as above, creates the TiFlash replica automatically. To add an index to an existing table that has no TiFlash replica, create the replica first with ALTER TABLE vector_table SET TIFLASH REPLICA 1; and then run CREATE VECTOR INDEX.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The optimizer uses the index only when the query orders by the same distance function the index was built with, in ascending order, with a LIMIT clause. Vector indexes currently support cosine and L2 distance. A WHERE filter applied before the distance sort prevents index use, so run the vector search in a subquery and filter its results instead.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Index results are approximate. TiDB&#8217;s documentation reports recall generally above 90%, which suits most retrieval workloads but can differ from the exact results of an unindexed query.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Apart from the VECTOR type, the VEC_ functions, and the VECTOR INDEX clause, every statement here is SQL a MySQL developer already writes.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"FAQ\"><\/span><strong>FAQ<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Do I need a separate vector database to use TiDB&#8217;s VECTOR type?<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>No: vector columns live in the same tables as your relational data<\/li>\n\n\n\n<li>Vectors and relational rows share one SQL interface and one transactional boundary<\/li>\n\n\n\n<li>No sync pipeline between a vector store and your primary database<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>What SQL functions does TiDB provide for vector search?<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>VEC_COSINE_DISTANCE and VEC_L2_DISTANCE, both supported by vector indexes<\/li>\n\n\n\n<li>VEC_NEGATIVE_INNER_PRODUCT and VEC_L1_DISTANCE for other similarity metrics<\/li>\n\n\n\n<li>Utility functions such as VEC_DIMS and VEC_L2_NORM<\/li>\n\n\n\n<li>Standard ORDER BY and LIMIT for nearest-neighbor queries<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Can I use my existing MySQL ORM or driver with TiDB&#8217;s vector columns?<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Drivers: yes, vectors travel as string literals over the standard MySQL protocol<\/li>\n\n\n\n<li>Python ORMs need a small integration package: tidb-vector for SQLAlchemy and peewee, django-tidb[vector] for Django<\/li>\n\n\n\n<li>LangChain and LlamaIndex have official TiDB vector integrations<\/li>\n\n\n\n<li>No separate vector-database SDK is required<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>What embedding dimension should I use?<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Match the dimension your embedding model produces, for example 1536 for OpenAI&#8217;s text-embedding-3-small<\/li>\n\n\n\n<li>TiDB vector columns support up to 16,383 dimensions<\/li>\n\n\n\n<li>Declare the dimension explicitly, as in VECTOR(1536), because vector indexes require a fixed dimension<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Query_Vectors_Without_Learning_a_New_System\"><\/span><strong>Query Vectors Without Learning a New System<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">TiDB stores vectors as a native SQL data type. You create, insert, and query vector data with the CREATE TABLE, INSERT, and SELECT syntax your team already knows, plus a VECTOR type and a few distance functions. <a href=\"https:\/\/tidbcloud.com\/free-trial\/\">Sign up for TiDB Cloud Starter<\/a> and try the examples in this guide against your own data.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Adding vector search to an application usually means adopting a new client library and a new query language on top of the SQL your team already knows. TiDB takes a different approach. Vector storage and search use familiar MySQL syntax, extended with a native VECTOR data type and a small set of distance functions. If [&hellip;]<\/p>\n","protected":false},"author":305,"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 v28.2 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>In Age of LLM: How to Store Vectors with MySQL SQL Grammar | TiDB Store Vectors with MySQL SQL Grammar in TiDB<\/title>\n<meta name=\"description\" content=\"Store and query vectors using plain MySQL SQL syntax in TiDB. Create a vector column, insert embeddings, and search by distance.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.pingcap.com\/ko\/article\/in-age-of-llm-how-to-store-vectors-with-mysql-sql-grammar\/\" \/>\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 Store Vectors with MySQL SQL Grammar in TiDB\" \/>\n<meta property=\"og:description\" content=\"Store and query vectors using plain MySQL SQL syntax in TiDB. Create a vector column, insert embeddings, and search by distance.\" \/>\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=\"2026-10-05T17:01:19+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=\"\uc608\uc0c1 \ub418\ub294 \ud310\ub3c5 \uc2dc\uac04\" \/>\n\t<meta name=\"twitter:data1\" content=\"5\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 Store Vectors with MySQL SQL Grammar in TiDB\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.pingcap.com\\\/#website\"},\"datePublished\":\"2026-10-05T16:57:05+00:00\",\"dateModified\":\"2026-10-05T17:01:19+00:00\",\"description\":\"Store and query vectors using plain MySQL SQL syntax in TiDB. Create a vector column, insert embeddings, and search by distance.\",\"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 Store Vectors with MySQL SQL Grammar in TiDB","description":"Store and query vectors using plain MySQL SQL syntax in TiDB. Create a vector column, insert embeddings, and search by distance.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.pingcap.com\/ko\/article\/in-age-of-llm-how-to-store-vectors-with-mysql-sql-grammar\/","og_locale":"ko_KR","og_type":"article","og_title":"In Age of LLM: How to Store Vectors with MySQL SQL Grammar | TiDB Store Vectors with MySQL SQL Grammar in TiDB","og_description":"Store and query vectors using plain MySQL SQL syntax in TiDB. Create a vector column, insert embeddings, and search by distance.","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":"2026-10-05T17:01:19+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":{"\uc608\uc0c1 \ub418\ub294 \ud310\ub3c5 \uc2dc\uac04":"5\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 Store Vectors with MySQL SQL Grammar in TiDB","isPartOf":{"@id":"https:\/\/www.pingcap.com\/#website"},"datePublished":"2026-10-05T16:57:05+00:00","dateModified":"2026-10-05T17:01:19+00:00","description":"Store and query vectors using plain MySQL SQL syntax in TiDB. Create a vector column, insert embeddings, and search by distance.","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>Adding vector search to an application usually means adopting a new client library and a new query language on top of the SQL your team already knows. TiDB takes a different approach. Vector storage and search use familiar MySQL syntax, extended with a native VECTOR data type and a small set of distance functions. If [&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\/305"}],"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}]}}