SQL Recipe Library
Introduction
This recipe library provides copy-paste ready SQL queries for common NeuronDB operations. Each recipe file contains multiple queries with explanations, use cases, and complexity ratings.
What you'll find:
- ✅ Ready-to-run SQL queries for vector search
- ✅ Hybrid search patterns combining vector and full-text search
- ✅ Filtered search examples with SQL WHERE clauses
- ✅ Index creation and tuning patterns
- ✅ Embedding generation examples
Quick Start
Get started with recipes in three steps:
1. Load Quickstart Data
If you haven't already, load the quickstart data pack:
Load quickstart data
./examples/quickstart/load_quickstart.sh2. Try a Recipe
Run a recipe file directly:
Run recipe file
psql "postgresql://neurondb:neurondb@localhost:5433/neurondb" -f 01_vector_search.sql3. Or Copy Individual Queries
Open the recipe file, find the query you need, and copy it into your SQL client.
Recipe Files
The recipe library includes 5 main recipe files:
| File | Description | Complexity | Use Cases |
|---|---|---|---|
01_vector_search.sql | Vector similarity search patterns | ⭐ - ⭐⭐⭐ | KNN search, distance metrics, similarity ranking |
02_hybrid_search.sql | Vector + full-text search combinations | ⭐⭐ - ⭐⭐⭐ | Combined semantic and keyword search, RRF fusion |
03_filtered_search.sql | Vector search with SQL filters | ⭐ - ⭐⭐⭐ | Category filters, date ranges, conditional search |
04_indexing.sql | Index creation patterns | ⭐ - ⭐⭐⭐ | HNSW indexes, IVF indexes, parameter tuning |
05_embedding_generation.sql | Embedding generation patterns | ⭐ - ⭐⭐⭐ | Text embeddings, batch generation, model selection |
Recipe Categories
1. Vector Search (01_vector_search.sql)
Basic to advanced vector similarity search queries.
Key Recipes:
- Basic cosine similarity search
- L2/Euclidean distance search
- Inner product search
- Distance threshold filtering
- Multi-metric comparison
When to Use:
- Finding similar documents
- Recommendation systems
- Semantic search
- Similarity-based ranking
2. Hybrid Search (02_hybrid_search.sql)
Combine vector similarity with full-text search.
Key Recipes:
- Weighted combination (vector + FTS)
- Reciprocal Rank Fusion (RRF)
- Query text search
- Boosted fields
- Performance comparison
When to Use:
- Combining semantic and keyword search
- Improving search recall
- Handling both synonym and exact match queries
- Balancing relevance and precision
3. Filtered Search (03_filtered_search.sql)
Vector search with SQL WHERE clause filters.
Key Recipes:
- Category filtering
- Date range filtering
- Multiple filter conditions
- Top-K per category
- Complex filters
When to Use:
- Category-based recommendations
- Time-based search (recent items)
- User-specific filtering
- Excluding unwanted results
- Multi-criteria search
4. Indexing (04_indexing.sql)
Create and manage vector indexes.
Key Recipes:
- HNSW index creation (L2, cosine, inner product)
- IVF index creation
- Index parameter tuning
- Multiple indexes
- Performance optimization
When to Use:
- Setting up new tables
- Optimizing query performance
- Tuning for your workload
- Supporting multiple distance metrics
5. Embedding Generation (05_embedding_generation.sql)
Generate embeddings from text.
Key Recipes:
- Single embedding generation
- Batch generation
- Updating existing documents
- Query embedding generation
- Error handling
When to Use:
- Ingesting new documents
- Updating existing data
- Generating query vectors
- Batch processing
Usage Patterns
Pattern 1: Run Entire Recipe File
Run all recipes in a file
# Run all recipes in a file
psql "postgresql://neurondb:neurondb@localhost:5433/neurondb" -f 01_vector_search.sql
# Or with Docker Compose
docker compose exec neurondb psql -U neurondb -d neurondb -f /path/to/recipes/01_vector_search.sqlPattern 2: Copy Individual Queries
- Open the recipe file
- Find the recipe you need
- Copy the SQL query
- Paste into your SQL client
- Modify as needed for your use case
Pattern 3: Interactive Learning
- Run a simple recipe first
- Understand the output
- Try modifying the query
- Experiment with parameters
- Move to more complex recipes
Complexity Guide
- ⭐ Beginner: Simple queries, basic operations, easy to understand
- ⭐⭐ Intermediate: Moderate complexity, requires some SQL knowledge
- ⭐⭐⭐ Advanced: Complex queries, performance optimization, advanced patterns
Common Use Cases
Use Case: Build a Recommendation System
- Setup: Load quickstart data or your own data
- Indexing: Use
04_indexing.sqlto create HNSW index - Search: Use
01_vector_search.sqlfor similarity search - Filtering: Use
03_filtered_search.sqlfor user/category filters
Use Case: Semantic Search with Keywords
- Setup: Load data with embeddings
- Hybrid: Use
02_hybrid_search.sqlfor combined search - Filtering: Use
03_filtered_search.sqlfor additional filters
Use Case: Ingest New Documents
- Generation: Use
05_embedding_generation.sqlto generate embeddings - Indexing: Use
04_indexing.sqlto create indexes - Search: Use
01_vector_search.sqlto query
Tips for Using Recipes
1. Start Simple
Begin with ⭐ complexity recipes to understand the patterns.
2. Read Comments
Each recipe includes comments explaining what it does and when to use it.
3. Customize for Your Schema
Recipes use quickstart_documents table - adjust table/column names for your schema.
4. Understand Parameters
HNSW parameters (m, ef_construction), search parameters (ef_search), etc.
5. Test Performance
Use EXPLAIN ANALYZE to verify index usage and query performance.
6. Combine Recipes
Mix recipes from different files to build complex workflows.
Adapting Recipes
Change Table Name
Recipes use quickstart_documents - replace with your table:
Adapt table name
-- Recipe uses:
FROM quickstart_documents
-- Change to:
FROM your_table_nameChange Column Names
Adjust embedding column name if different:
Adapt column name
-- Recipe uses:
embedding <=> ...
-- Change to:
your_embedding_column <=> ...Change Dimensions
Adjust vector dimensions to match your model:
Adapt dimensions
-- Recipe uses:
embedding vector(384)
-- Change to:
embedding vector(1536) -- For OpenAI ada-002Performance Tips
1. Use Indexes
All vector search queries benefit from HNSW indexes. See 04_indexing.sql.
2. Tune Search Parameters
Adjust hnsw.ef_search (HNSW) or ivfflat.probes (IVF) for speed vs. recall.
3. Use Filters
Apply WHERE clauses before vector search to reduce search space.
4. Batch Operations
When generating embeddings, use batch processing (see 05_embedding_generation.sql).
5. Monitor Performance
Use EXPLAIN ANALYZE to verify index usage and query plans.
Troubleshooting
"Table quickstart_documents does not exist"
Solution: Load the quickstart data pack first:
Load quickstart data
./examples/quickstart/load_quickstart.shOr adapt the recipe to use your own table.
"Index not used in query"
Solution:
- Verify index exists:
\d table_name - Check query uses correct operator (
<=>for cosine,<->for L2) - Increase
ef_searchif using HNSW - Use
EXPLAIN ANALYZEto see query plan
"Embedding generation fails"
Solution:
- Check embedding model is configured
- Verify API keys (if using external models)
- Check network connectivity
- See
05_embedding_generation.sqlfor error handling examples
"Query is slow"
Solution:
- Create/verify indexes (see
04_indexing.sql) - Adjust search parameters (
ef_search,probes) - Add filters to reduce search space
- Use
EXPLAIN ANALYZEto identify bottlenecks
Related Resources
- Quickstart Data Pack - Sample data for trying recipes
- Quickstart Guide - Complete quickstart guide
- CLI Helpers - Command-line tools for common tasks
- NeuronDB Documentation - Comprehensive documentation