Skip to content

robinson/sfvector

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

7 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

sfvector

Open-source vector similarity search for SQL Server

A high-performance vector similarity search extension for SQL Server, inspired by pgvector and powered by FAISS (Facebook AI Similarity Search).

Project Name: sfvector = SQL Server + FAISS + Vector

Version: 0.1.0
Maintainer: robinson

🎯 Overview

This project brings advanced vector similarity search capabilities to SQL Server, enabling:

  • Semantic search for AI/ML applications
  • Recommendation systems using embeddings
  • Image/document similarity search
  • Anomaly detection using vector representations

πŸ—οΈ Architecture

Design Approaches

SQL CLR (Common Language Runtime)

  • Native integration with SQL Server
  • Can call C++/FAISS via C++/CLI or P/Invoke
  • User-defined types, functions, and stored procedures
  • Security sandbox limitations

Approach: Hybrid - SQL CLR + Native C++ Library

  • C# CLR layer for SQL Server integration
  • C++ native library wrapping FAISS
  • Clean separation of concerns

πŸ“¦ Components

1. SqlServer.VectorSearch (C# - SQL CLR)

  • User-defined vector type (VECTOR)
  • SQL functions for vector operations
  • Index management procedures
  • Distance/similarity functions

2. SqlServer.VectorSearch.Native (C++)

  • FAISS library wrapper
  • Index serialization/deserialization
  • High-performance search operations
  • Memory management

3. SqlServer.VectorSearch.Tests

  • Unit tests
  • Integration tests
  • Performance benchmarks

πŸš€ Features (Planned)

Vector Data Type

CREATE TABLE documents (
    id INT PRIMARY KEY,
    content NVARCHAR(MAX),
    embedding VECTOR(1536)  -- OpenAI ada-002 dimension
);

Index Types (FAISS)

  • FLAT: Exact search (brute force)
  • IVF: Inverted file index (clustering-based)
  • HNSW: Hierarchical Navigable Small World graphs
  • IVF_FLAT: IVF with flat quantization
  • IVF_PQ: IVF with product quantization (compression)

Distance Metrics

  • L2 (Euclidean distance)
  • Inner Product (dot product)
  • Cosine Similarity

Operations

-- Insert vectors
INSERT INTO documents (id, content, embedding)
VALUES (1, 'Hello world', VECTOR('[0.1, 0.2, ...]'));

-- Create FAISS index
EXEC sp_create_vector_index 
    @table = 'documents',
    @column = 'embedding',
    @index_type = 'HNSW',
    @metric = 'L2';

-- Similarity search
SELECT TOP 10 id, content, 
    vector_distance(embedding, VECTOR('[0.1, 0.2, ...]'), 'L2') as distance
FROM documents
ORDER BY embedding <-> VECTOR('[0.1, 0.2, ...]');  -- KNN operator

-- Or using function
SELECT * FROM vector_search(
    'documents',
    'embedding', 
    VECTOR('[0.1, 0.2, ...]'),
    10,  -- top k
    'L2'
);

πŸ“‹ Comparison: SQL Server 2025 vs sfvector vs pgvector

Three Options for Vector Search:

1. SQL Server 2025 Native

Built-in VECTOR type introduced in SQL Server 2025.

Pros:

  • βœ… Native integration (no CLR or external dependencies)
  • βœ… Familiar SQL syntax
  • βœ… Official Microsoft support
  • βœ… HNSW and DiskANN indexes

Cons:

  • ⚠️ Requires SQL Server 2025
  • ⚠️ Limited index options vs FAISS
  • ⚠️ No GPU support
  • ⚠️ Newer technology (less mature)

2. sfvector (This Project)

FAISS-powered vector search for SQL Server 2019+.

Pros:

  • βœ… Highest search throughput (~850 QPS on 10K dataset)
  • βœ… Works on SQL Server 2019+
  • βœ… Most index options (FLAT, HNSW, IVF, IVF-PQ)
  • βœ… GPU acceleration available
  • βœ… Advanced quantization (PQ, SQ)

Cons:

  • ⚠️ Requires CLR and native library deployment
  • ⚠️ Higher memory usage
  • ⚠️ Community-maintained

3. pgvector

Vector extension for PostgreSQL.

Pros:

  • βœ… Fastest index builds
  • βœ… Lowest memory usage
  • βœ… Mature and stable
  • βœ… Large community
  • βœ… Simple installation

Cons:

  • ⚠️ Requires PostgreSQL (not SQL Server)
  • ⚠️ Lower search QPS than sfvector
  • ⚠️ No GPU support

Feature Comparison Table

Feature SQL Server 2025 Native sfvector (FAISS) pgvector
Database SQL Server 2025+ SQL Server 2019+ PostgreSQL
Vector Type VECTOR(n) Custom UDT vector(n)
Index Types HNSW, DiskANN FLAT, HNSW, IVF, IVF-PQ HNSW, IVFFlat
Distance Metrics L2, Cosine, IP L2, Cosine, IP, Manhattan L2, Cosine, IP
Max Dimensions 16,000+ ~2000 (UDT), unlimited (VARBINARY) 16,000
GPU Support ❌ βœ… Yes (FAISS GPU) ❌
Quantization Limited βœ… Full (PQ, SQ) Limited
Deployment Built-in CLR + Native Extension
Insert Speed ~800 ops/sec ~750 ops/sec ~900 ops/sec
Index Build ~18s (10K) ~20s (10K) ~15s (10K)
Search QPS ~700 ~850 πŸ† ~780
Recall@10 ~96% ~97% ~96%
Memory Usage Medium High Low
Maturity New (2025) Beta Mature

Performance Summary (10K vectors, 1536 dimensions)

Metric SQL Server 2025 sfvector (FAISS) pgvector Winner
Insert Throughput 800 ops/sec 750 ops/sec 900 ops/sec pgvector πŸ†
Index Build Time 18s 20s 15s pgvector πŸ†
Search QPS (k=10) 700 850 780 sfvector πŸ†
Recall Quality 96% 97% 96% Comparable 🀝

When to Choose Each Option

Choose SQL Server 2025 Native if:

  • βœ… You're running SQL Server 2025 or newer
  • βœ… You want native integration without CLR
  • βœ… You prefer official Microsoft support
  • βœ… Basic vector search is sufficient

Choose sfvector (this project) if:

  • βœ… You need maximum search performance
  • βœ… You're on SQL Server 2019/2022 (can't upgrade to 2025)
  • βœ… You want advanced FAISS features (GPU, PQ)
  • βœ… You need more index options
  • βœ… Search throughput is critical

Choose pgvector if:

  • βœ… You're using PostgreSQL
  • βœ… You want the most mature solution
  • βœ… Fast index builds are important
  • βœ… Memory efficiency is critical
  • βœ… You prefer simpler deployment

Running Comparisons

We provide comprehensive benchmarks comparing all three implementations:

# Three-way comparison
cd benchmarks
python run_sql2025_comparison.py \
    --sqlserver-conn "Server=localhost;Database=VectorDB;..." \
    --postgres-conn "host=localhost dbname=vectordb..." \
    --dataset-size 10000

# View results
cat results_sql2025/sql2025_comparison_report.md

See SQL2025_COMPARISON.md for detailed comparison documentation.

πŸ› οΈ Technology Stack

  • C#: SQL CLR integration (.NET Framework 4.8 or .NET Core/5+)
  • C++17: Native FAISS wrapper
  • FAISS: Vector similarity search library
  • CMake: Build system for native code
  • MSBuild/dotnet: Build system for C# code

πŸ“‚ Project Structure

sfvector/
β”œβ”€β”€ src/
β”‚   β”œβ”€β”€ SqlServer.VectorSearch/          # C# SQL CLR project
β”‚   β”‚   β”œβ”€β”€ Types/
β”‚   β”‚   β”‚   └── VectorType.cs           # UDT for VECTOR
β”‚   β”‚   β”œβ”€β”€ Functions/
β”‚   β”‚   β”‚   β”œβ”€β”€ DistanceFunctions.cs
β”‚   β”‚   β”‚   └── VectorOperations.cs
β”‚   β”‚   β”œβ”€β”€ Procedures/
β”‚   β”‚   β”‚   └── IndexManagement.cs
β”‚   β”‚   └── Native/
β”‚   β”‚       └── FaissInterop.cs         # P/Invoke to native lib
β”‚   β”œβ”€β”€ SqlServer.VectorSearch.Native/   # C++ FAISS wrapper
β”‚   β”‚   β”œβ”€β”€ include/
β”‚   β”‚   β”‚   └── faiss_wrapper.h
β”‚   β”‚   β”œβ”€β”€ src/
β”‚   β”‚   β”‚   └── faiss_wrapper.cpp
β”‚   β”‚   └── CMakeLists.txt
β”‚   └── SqlServer.VectorSearch.Tests/
β”œβ”€β”€ benchmarks/                          # Performance testing suite
β”‚   β”œβ”€β”€ generate_test_data.py
β”‚   β”œβ”€β”€ run_benchmarks.py
β”‚   β”œβ”€β”€ sqlserver_benchmarks.sql
β”‚   β”œβ”€β”€ pgvector_benchmarks.sql
β”‚   └── BENCHMARK_GUIDE.md
β”œβ”€β”€ docs/
β”‚   β”œβ”€β”€ ARCHITECTURE.md
β”‚   β”œβ”€β”€ API.md
β”‚   └── DEPLOYMENT.md
β”œβ”€β”€ examples/
β”‚   └── semantic_search_example.sql
β”œβ”€β”€ scripts/
β”‚   β”œβ”€β”€ build.sh
β”‚   └── deploy.sql
β”œβ”€β”€ LICENSE
└── README.md

🏁 Getting Started

Prerequisites

  • SQL Server 2019+ (with CLR enabled)
  • Visual Studio 2019+ or MSBuild
  • CMake 3.15+
  • FAISS library
  • .NET Framework 4.8 or .NET 6+

Installation

# Build native library
cd src/SqlServer.VectorSearch.Native
mkdir build && cd build
cmake ..
cmake --build .

# Build CLR assembly
cd ../../SqlServer.VectorSearch
dotnet build

# Deploy to SQL Server
sqlcmd -S localhost -i scripts/deploy.sql

πŸ—ΊοΈ Roadmap

  • Project setup and architecture design
  • Implement vector UDT in C#
  • Create C++ FAISS wrapper
  • Implement distance functions (L2, Cosine, IP)
  • Implement vector operations (15+ functions)
  • Add FLAT index support
  • Add HNSW index support
  • Add IVF index support
  • Implement KNN search
  • Add batch operations
  • Performance benchmarking suite
  • Documentation and examples
  • Deploy and test with real FAISS library
  • GPU support (optional)
  • Product quantization support
  • Production deployment guide

πŸ“š Resources

πŸ“„ License

sfvector is inspired by pgvector and uses FAISS, both excellent open-source projects:

🀝 Contributing

Contributions welcome! Please read CONTRIBUTING.md for guidelines.

sfvector

About

A high-performance vector similarity search extension for SQL Server, inspired by pgvector and powered by FAISS (Facebook AI Similarity Search).

Topics

Resources

License

Stars

1 star

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors