VECTOR_SEARCH(Transact-SQL) (预览版)

SQL Server 2025 (17.x) 预览版

使用近似相邻向量搜索算法搜索类似于给定查询矢量的矢量。 若要详细了解矢量索引和矢量搜索的工作原理,以及精确搜索和近似搜索之间的差异,请参阅 SQL 数据库引擎中的矢量

预览功能

注释

此函数处于预览状态,可能会更改。 请务必阅读 联机服务的服务级别协议(SLA)中的预览使用条款。

此功能处于预览状态。 若要使用此功能,必须启用以下 跟踪标志

DBCC TRACEON(466, 474, 13981, -1)

在使用之前,请确保先查看 当前限制

语法

Transact-SQL 语法约定

VECTOR_SEARCH(
    TABLE = object [AS source_table_alias]
    , COLUMN = vector_column
    , SIMILAR_TO = query_vector
    , METRIC = { 'cosine' | 'dot' | 'euclidean' }
    , TOP_N = k
) [AS result_table_alias]

论据

TABLE = 对象 [AS source_table_alias]

执行搜索的表。 它必须是基表。 不支持视图、临时表(本地表和全局表)。

COLUMN = vector_column

在其中执行搜索的向量列。 该列必须是 矢量 数据类型。

SIMILAR_TO = query_vector

用于搜索的向量。 它必须是 矢量 类型的变量或列。

METRIC = { 'cosine' |'dot' |'euclidean' }

用于计算查询向量与指定列中矢量之间的距离的距离指标。 仅当找到与同一个指标和同一列匹配的 ANN 索引时,才使用 ANN(近似近邻)索引。 如果没有兼容的 ANN 索引,则会引发警告,并使用 KNN (k-Nearest Neighbor) 算法。

TOP_N = <k>

必须返回的最大相似向量数。 它必须是正 整数

result_table_alias

别名用于引用结果集。

返回结果集

返回的结果集包含 TABLE 参数中指定的表中的所有列,以及额外的 distance 列。 该 distance 列包含 COLUMN 参数中给定向量与 SIMILAR_TO 参数中指定的向量之间的距离。

局限性

当前预览版具有以下限制:

仅筛选后

矢量搜索在应用任何谓词之前发生。 仅当返回最相似的向量后,才会应用其他谓词。 下面的示例返回与查询向量 @qv最相似的嵌入的前 10 行,然后应用子句中指定的 WHERE 谓词。 如果与矢量搜索返回的向量关联的 10 行中没有等于 accepted 1 的列,则结果为空。

SELECT
  s.id, 
  s.title,
  r.distance
FROM
  VECTOR_SEARCH(
    TABLE = dbo.sessions AS s, 
    COLUMN = embedding, 
    SIMILAR_TO = @qv, 
    METRIC = 'cosine', 
    TOP_N = 10 
  ) AS r
WHERE
  accepted = 1
ORDER BY
  r.distance

无法在视图中使用VECTOR_SEARCH

VECTOR_SEARCH 不能在视图正文中使用。

例子

示例 1

以下示例查找表中最相似的 10 篇文章Pink Floyd music stylewikipedia_articles_embeddings

DECLARE @qv VECTOR(1536) = AI_GENERATE_EMBEDDING(N'Pink Floyd music style' USE MODEL Ada2Embeddings);
SELECT 
    t.id, s.distance, t.title
FROM
    VECTOR_SEARCH(
        TABLE = [dbo].[wikipedia_articles_embeddings] as t, 
        COLUMN = [content_vector], 
        SIMILAR_TO = @qv, 
        METRIC = 'cosine', 
        TOP_N = 10
    ) AS s
ORDER BY s.distance

示例 2

与示例 1 相同,但这次查询向量是从另一个表而不是变量中获取的。

CREATE TABLE #t (
  id INT, 
  q NVARCHAR(MAX),
  v VECTOR(1536)
);
INSERT INTO 
  #t
SELECT 
    id, q, ai_generate_embeddings(q USE MODEL Ada2Embeddings)
FROM
    (VALUES 
        (1, N'four legged furry animal'),
        (2, N'pink floyd music style')
    ) S(id, q)
;

SELECT 
    t.id, s.distance, t.title
FROM
    #t AS qv
CROSS APPLY
    VECTOR_SEARCH(
        TABLE = [dbo].[wikipedia_articles_embeddings] as t, 
        COLUMN = [content_vector], 
        SIMILAR_TO = qv.v, 
        METRIC = 'cosine', 
        TOP_N = 10
    ) AS s
WHERE
  qv.id = 2
ORDER BY 
  s.distance

示例 3

使用和相关CREATE VECTOR INDEX函数的基本端到端示例VECTOR_SEARCH。 将模拟嵌入内容。 在实际方案中,嵌入使用嵌入模型和 AI_GENERATE_EMBEDDINGSOpenAI SDK 等外部库生成。

以下代码块演示 VECTOR_SEARCH 了具有模拟嵌入的函数:

  1. 启用当前预览版中必需的跟踪标志。
  2. 使用数据类型dbo.Articles的列embedding创建示例表
  3. 使用模拟嵌入数据插入示例数据。
  4. 在 . 上 dbo.Articles.embedding创建向量索引。
  5. 使用函数演示矢量相似性搜索 VECTOR_SEARCH
-- Step 0: Enable Preview Feature
DBCC TRACEON(466, 474, 13981, -1);
GO

-- Step 1: Create a sample table with a VECTOR(5) column
CREATE TABLE dbo.Articles 
(
    id INT PRIMARY KEY,
    title NVARCHAR(100),
    content NVARCHAR(MAX),
    embedding VECTOR(5) -- mocked embeddings
);

-- Step 2: Insert sample data
INSERT INTO Articles (id, title, content, embedding)
VALUES
(1, 'Intro to AI', 'This article introduces AI concepts.', '[0.1, 0.2, 0.3, 0.4, 0.5]'),
(2, 'Deep Learning', 'Deep learning is a subset of ML.', '[0.2, 0.1, 0.4, 0.3, 0.6]'),
(3, 'Neural Networks', 'Neural networks are powerful models.', '[0.3, 0.3, 0.2, 0.5, 0.1]'),
(4, 'Machine Learning Basics', 'ML basics for beginners.', '[0.4, 0.5, 0.1, 0.2, 0.3]'),
(5, 'Advanced AI', 'Exploring advanced AI techniques.', '[0.5, 0.4, 0.6, 0.1, 0.2]');

-- Step 3: Create a vector index on the embedding column
CREATE VECTOR INDEX vec_idx ON Articles(embedding)
WITH (metric = 'cosine', type = 'diskann');

-- Step 4: Perform a vector similarity search
DECLARE @qv VECTOR(5) = '[0.3, 0.3, 0.3, 0.3, 0.3]';
SELECT
    t.id,
    t.title,
    t.content,
    s.distance
FROM
    VECTOR_SEARCH(
        table = Articles AS t,
        column = embedding,
        similar_to = @qv,
        metric = 'cosine',
        top_n = 3
    ) AS s
ORDER BY s.distance, t.title;