TLTan LeAI, cloud & automation for legacy systems · CalgaryLet’s talk
← All notes

RAG on MatrixOne: SQL and vector search in one database

MatrixOne stores the chunks, metadata and embeddings of a RAG system and answers vector, keyword and SQL filters in one query engine. Setup, schema, queries, and how it compares with pgvector and Azure AI Search.

Tried itRan it myself on a demo or prototype.

A RAG system needs somewhere to keep document chunks, their metadata and their embeddings, and a way to pull back the closest chunks for a question. Most stacks split that across a SQL database and a separate vector store. MatrixOne is an open-source database that does relational SQL, analytics, full-text and vector search in one engine, and speaks the MySQL protocol. I ran it locally in Docker to see whether one database is enough for a small RAG system.

To be clear about what it is: MatrixOne is not an LLM or an agent. In a RAG pipeline it is the storage and retrieval layer. You still need an embedding model, a chat model and code that runs the pipeline.

Where it sits in a RAG pipeline

flowchart TD
  S[PDF · DOCX · web · database] --> C[Extract + chunk]
  C --> E[Embedding model]
  E --> M[(MatrixOne<br/>chunks + metadata + vectors)]
  Q[User question] --> QE[Question embedding]
  QE -->|vector search + SQL filters| M
  M --> X[Retrieved context]
  X --> L[Chat LLM]
  L --> A[Answer with citations]

Run it locally

docker run -d --name matrixone -p 127.0.0.1:6001:6001 matrixorigin/matrixone:latest
mysql -h 127.0.0.1 -P 6001 -u root -p

The quick start uses a default root password. That is fine on localhost; change it before the database is reachable from anywhere else.

Schema: chunks with a vector column

The vector size must match the embedding model. text-embedding-3-small returns 1,536 dimensions by default:

CREATE DATABASE rag_demo;
USE rag_demo;

CREATE TABLE document_chunks (
    id            BIGINT PRIMARY KEY,
    document_name VARCHAR(255),
    content       TEXT,
    embedding     VECF32(1536)
);

Embed, then search with the same model

from openai import OpenAI

client = OpenAI()
resp = client.embeddings.create(
    model="text-embedding-3-small",
    input="How to troubleshoot a slow SQL query. Check execution plans, indexes and statistics.",
)
embedding = resp.data[0].embedding  # 1536 floats

Insert the text and the full vector through any MySQL-compatible driver. At question time, embed the question with the same model, then rank by distance:

SELECT id, document_name, content,
       l2_distance(embedding, ?) AS distance
FROM document_chunks
ORDER BY distance ASC
LIMIT 5;

Pass the vector as a parameter. Never build the SQL string from user input.

Indexing: exact first, then IVF

For a small proof of concept, exact search gives you a correct baseline. For larger tables MatrixOne has IVF-FLAT (and HNSW) indexes. In the version I used, IVF sits behind an experimental flag, and the syntax can change between versions:

SET experimental_ivf_index = 1;

CREATE INDEX idx_embedding
USING IVFFLAT ON document_chunks(embedding)
LISTS = 100 OP_TYPE 'vector_l2_ops';

Lesson. Measure recall of the index against the exact-search baseline before trusting it. A faster index that misses the right chunk makes the answer worse, and the LLM will not tell you.

Generate the answer

context = "\n\n".join(retrieved_chunks)
resp = client.chat.completions.create(
    model="gpt-4.1-mini",
    messages=[
        {"role": "system", "content": "Answer using only the provided context. "
                                      "If the answer is not in the context, say you don't know."},
        {"role": "user", "content": f"Context:\n{context}\n\nQuestion:\n{question}"},
    ],
)

In a real system, pass the document name, page or section, and chunk ID with each chunk, so the answer can cite its sources.

How it would fit a .NET / Azure stack

This is the shape I would use for an internal knowledge assistant. It is a design, not something I have shipped.

flowchart TD
  UI[React / Angular<br/>chat + upload] --> API[ASP.NET Core Web API]
  API --> AO[Azure OpenAI<br/>embeddings + chat]
  API --> MO[(MatrixOne<br/>SQL + vectors)]
  W[Worker / Hangfire<br/>extract · chunk · index] --> MO
  W --> AO
  BL[(Azure Blob Storage)] --> W
  • DB client: MySqlConnector. MySQL protocol compatibility does not mean every MySQL or ORM feature works; test the queries you need.
  • Orchestration: Semantic Kernel or plain C#.
  • Ingestion: a background worker with idempotent jobs and retries, so re-uploading a document replaces its chunks instead of duplicating them.

MatrixOne or something else?

OptionPick it when
MatrixOneYou want SQL, analytics and vectors in one engine and can live with a younger ecosystem
PostgreSQL + pgvectorYou already run Postgres and want to add RAG to it
QdrantYou want a dedicated vector search service
MilvusLarge vector workloads at scale
Azure AI SearchYou are on Azure and want managed hybrid search

My take: MatrixOne is a good way to learn RAG with plain SQL, and keeping filters, keywords and vectors in one query is genuinely convenient. For a production .NET/Azure system I would benchmark it against pgvector and Azure AI Search on recall, latency, cost and operations before choosing.

The LangGraph data analyst uses the same retrieval idea on a smaller scale: SQL for the numbers, documents for the reasons, with cited answers.

Next steps

  1. Load a real document set and compare exact search with the IVF index on recall@5.
  2. Add hybrid retrieval: full-text plus vector, filtered by team for access control.
  3. Build a small evaluation set of questions with known answers, and track it as the pipeline changes.