ai native database

AI 原生数据库设计:向量检索与结构化查询的融合

1. 从传统数据库到 AI 原生数据库 传统关系数据库为结构化数据设计,以行/列为基本单位,通过 B+ 树索引加速点查询和范围查询。AI 时代新增了一类核心需求:向量检索——在高维空间中寻找与查询向量最相似的 Top-K 条记录。 AI 原生数据库(AI-Native Database)将向量检索与结构化查询深度融合,支持: SELECT * FROM articles WHERE category = 'AI' ORDER BY embedding <-> query_vector LIMIT 10 语义相似性过滤、混合排序、元数据 + 向量联合查询 代表系统:Pinecone、Weaviate、Chroma、Qdrant、Milvus、pgvector(PostgreSQL 扩展)、StarRocks(向量化)、ClickHouse(向量化)。 2. 存储引擎架构 2.1 整体架构 ┌──────────────────────────────────────────────────┐ │ SQL / GraphQL / REST API │ ├──────────────────────────────────────────────────┤ │ Query Optimizer & Planner │ │ ┌─────────────┐ ┌─────────────┐ ┌────────┐ │ │ │Scalar Filter│ │ Vector Index│ │Sort/Max│ │ │ │ Planner │ │ Planner │ │ Planner│ │ │ └─────────────┘ └─────────────┘ └────────┘ │ ├──────────────────────────────────────────────────┤ │ Execution Engine (Vectorized) │ ├──────────────────────────────────────────────────┤ │ ┌──────────┐ ┌──────────┐ ┌──────────────┐ │ │ │ Vector │ │ Row │ │ Column │ │ │ │ Index │ │ Store │ │ Store │ │ │ │(HNSW/IVF)│ │ (Pilosa) │ │(Arrow/Parquet)│ │ │ └──────────┘ └──────────┘ └──────────────┘ │ ├──────────────────────────────────────────────────┤ │ Distributed Storage Layer │ │ ┌────────────────────────────────────────────┐ │ │ │ S3 / HDFS / Local SSD / Remote Memory │ │ │ └────────────────────────────────────────────┘ │ └──────────────────────────────────────────────────┘ 2.2 向量存储格式 from dataclasses import dataclass, field from typing import Optional import numpy as np @dataclass class VectorRecord: id: str vector: np.ndarray # 原始向量(float32/float16) metadata: dict = field(default_factory=dict) version: int = 0 # MVCC 版本号 deleted: bool = False def to_bytes(self) -> bytes: """序列化:4字节维度 + 原始向量 + 元数据JSON + 版本""" dim = len(self.vector) vec_bytes = self.vector.astype(np.float32).tobytes() meta_bytes = json.dumps(self.metadata).encode("utf-8") header = struct.pack("<i", dim) version_bytes = struct.pack("<i", self.version) deleted_byte = b"\x01" if self.deleted else b"\x00" return header + version_bytes + deleted_byte + vec_bytes + meta_bytes @classmethod def from_bytes(cls, data: bytes) -> "VectorRecord": dim, version = struct.unpack("<ii", data[:8]) deleted = data[8] == 1 vec_bytes = data[9:9 + dim * 4] meta_bytes = data[9 + dim * 4:].decode("utf-8") return cls( id="", # id 需要从索引层获取 vector=np.frombuffer(vec_bytes, dtype=np.float32), metadata=json.loads(meta_bytes), version=version, deleted=deleted ) class VectorPage: """向量分页存储单元""" def __init__(self, page_size: int = 4096): self.page_size = page_size self.records: dict[str, bytes] = {} # id -> serialized bytes self.current_size = 0 def add(self, record: VectorRecord) -> bool: record_bytes = record.to_bytes() if self.current_size + len(record_bytes) > self.page_size: return False self.records[record.id] = record_bytes self.current_size += len(record_bytes) return True 3. 向量索引算法 3.1 HNSW(Hierarchical Navigable Small World) HNSW 是当前最流行的向量索引算法,在召回率和延迟之间取得优秀平衡。 ...

2026-06-25 · 8 min · 1597 words · 硅基 AGI 探索者
鲁ICP备2026018361号