Performance Guide
This guide covers MygramDB's benchmark results, tuning options, and production sizing.
Benchmark Results
The benchmark tables, charts, environment, and measured memory use are rendered from the same result snapshot as the benchmark page. The snapshot uses verify_text: all with the MygramDB query cache disabled.
Benchmark Environment
1,100,000 Wikipedia articles, MySQL 8.4.11 FULLTEXT (ngram parser), MygramDB v1.10.0, verify_text: all, and the query cache disabled. Each query is measured 200 times after 5 discarded warmup runs; the tables below report p50 and p99. Measured on Apple M5 Max (18 cores, 128GB).
On this dataset the speedup is 8.9x at the median, 179x at best and 1.6x at worst.
What the MySQL baseline was given
MygramDB holds its whole index in RAM, so a MySQL left on the default 128MB buffer pool would be measured on disk I/O its opponent never had to do. The pool is set to 8GiB instead — enough to hold the 837MB table, its 115MB of secondary indexes and the 1717MB of FULLTEXT auxiliary tables outright, about 2.6GiB in all.
During this run MySQL served 99.9989% of its page requests from the buffer pool, with 7,809 disk reads. The comparison does not assume a disk read on every query. Index structure, execution plans, sorting, and concurrency can all affect the measured latency.
Applying these results to another deployment
Both latency and speedup ratios depend on hardware, data, queries, and configuration. Run make bench-up && make bench-run to repeat the benchmark on your own hardware.
Search Latency (SORT id LIMIT 100)
Bars are scaled to the largest value in each chart, with a minimum visible width for small values. Use the tables for exact values and speedup ratios.
Search Latency (SORT id LIMIT 100) (p50)
| Query Type | Matches | MySQL p50 | MySQL p99 | MygramDB p50 | MygramDB p99 | Speedup |
|---|---|---|---|---|---|---|
| Multi-word ("quantum physics") | 104 | 639.50ms | 1688.45ms | 3.57ms | 5.61ms | 179x |
| Medium frequency ("quantum") | 1,961 | 509.59ms | 1148.04ms | 64.62ms | 90.30ms | 7.9x |
| Low frequency ("algorithm") | 2,498 | 281.39ms | 307.55ms | 5.19ms | 6.68ms | 54.2x |
| Rare term ("fibonacci") | 84 | 333.33ms | 859.25ms | 37.47ms | 47.40ms | 8.9x |
CJK Search Latency (SORT id LIMIT 100)
CJK Search Latency (SORT id LIMIT 100) (p50)
| Query | Matches | MySQL p50 | MySQL p99 | MygramDB p50 | MygramDB p99 | Speedup |
|---|---|---|---|---|---|---|
| 日本 | 32,282 | 61.05ms | 69.36ms | 18.31ms | 24.41ms | 3.3x |
| 東京 | 6,989 | 12.80ms | 16.38ms | 3.84ms | 5.62ms | 3.3x |
| 科学 | 1,551 | 2.99ms | 4.21ms | 1.87ms | 2.48ms | 1.6x |
COUNT Performance
COUNT Performance (p50)
| Query Type | Count | MySQL p50 | MySQL p99 | MygramDB p50 | MygramDB p99 | Speedup |
|---|---|---|---|---|---|---|
| Medium frequency ("quantum") | 1,961 | 498.67ms | 1417.43ms | 31.18ms | 53.38ms | 16x |
| Low frequency ("algorithm") | 2,498 | 258.74ms | 333.24ms | 4.34ms | 5.42ms | 59.6x |
Result Consistency
| Query | MySQL | MygramDB | Match |
|---|---|---|---|
| quantum | 1,961 | 1,961 | exact |
| algorithm | 2,498 | 2,498 | exact |
| 日本 | 32,282 | 32,282 | exact |
| 科学 | 1,551 | 1,551 | exact |
Concurrent Throughput
Concurrent Throughput — QPS
Query: "algorithm", 10 seconds per connection level.
| Connections | MySQL QPS | MygramDB QPS | MySQL p50 | MySQL p99 | MygramDB p50 | MygramDB p99 |
|---|---|---|---|---|---|---|
| 1 | 3.60 | 213.1 | 275.05ms | 294.51ms | 4.57ms | 6.18ms |
| 2 | 6.10 | 181.1 | 291.71ms | 433.12ms | 10.41ms | 28.12ms |
| 4 | 10.00 | 602.3 | 410.11ms | 450.69ms | 6.88ms | 8.08ms |
| 8 | 15.90 | 1,135.9 | 492.40ms | 541.39ms | 7.14ms | 8.84ms |
| 16 | 14.80 | 729.7 | 906.58ms | 1417.72ms | 19.36ms | 61.89ms |
Memory Usage
| Documents | Index | Documents + Text | Total RSS | Per 1M docs |
|---|---|---|---|---|
| 1,100,000 | 152MB | 1.78GB | 3.41GB | ~3.1GB |
Docker Desktop was allocated 32 GiB; 31.29 GiB was visible to the containers.
Performance Analysis
The published MySQL run had a 99.9989% InnoDB buffer-pool hit ratio, so its measured latency cannot be explained by a physical disk read on every query. MygramDB keeps its search index in memory and stores document IDs in posting lists. The measured difference depends on the query, data, sorting, memory, and concurrency; use the benchmark conditions above when comparing another deployment.
Performance Characteristics
Current Execution Paths
Several implementation choices limit work and lock duration for large queries and busy caches:
- Candidate filtering walks a posting list and the sorted candidate list once, so its work is proportional to the candidates and postings it visits.
- Candidate text is materialized in bounded chunks. The DocumentStore lock is released before BM25 scoring, term-frequency counting, and text post-filters.
- The posting-list and frequency lookup for a term is reused across boolean evaluation, NOT filtering, synonym expansion, and text verification. Expressions without NOT skip the full document-ID scan.
- Filter-column names are stored once per table. Each document keeps its filter values in a compact vector keyed by column ID.
- Cache invalidation has separate reverse indexes for n-gram, filter, and text-sensitive dependencies. LRU maintenance resumes from a bounded slice on each tick instead of sweeping the full cache under one exclusive lock.
- Per-table statistics are aggregated into a bounded snapshot for
INFO,/metrics, and Prometheus, rather than locking every table while formatting each scrape.
Optimization Tips
1. Choose Appropriate ngram_size
tables:
- name: "articles"
ngram_size: 2 # Default size for non-ideographs
kanji_ngram_size: 1 # Size for CJK ideographs; tune for your dataRecommendations:
- Bigram (2) is the default for ASCII text and CJK ideographs.
- Unigram (1) can be useful for CJK ideographs, depending on query patterns and data.
- Trigram (3) changes the indexed vocabulary and candidate selectivity. Its effect on build time, memory, and query latency depends on the workload; measure before changing the defaults.
2. Verify N-gram candidates with verify_text
memory:
verify_text: "all" # Eliminate n-gram false positivesWith verify_text: all, MygramDB checks each eligible candidate for a normalized substring match. This removes N-gram false positives for that matching rule; it does not make every query equivalent to MySQL FULLTEXT. The tested benchmark queries returned the same counts as MySQL FULLTEXT. Normalized text remains stored in every mode, so off does not reduce text-storage memory.
3. Memory Configuration
memory:
hard_limit_mb: 16384 # Reserved / not yet enforced
soft_target_mb: 8192 # Reserved / not yet enforced
roaring_threshold: 0.18 # Density threshold used by OPTIMIZERecommendations:
- Treat
hard_limit_mbandsoft_target_mbas reserved compatibility fields; they do not enforce process memory limits today. - Start with the default
roaring_threshold(0.18). Test changes withOPTIMIZEand your own data; the memory effect depends on posting-list density.
Enforce memory limits at the OS level
hard_limit_mb is not a hard process memory cap today. When running under Docker, systemd, or Kubernetes, set the container or service memory limit as well, and monitor it.
4. Use Filters for Selective Queries
tables:
- name: "articles"
filters:
- name: "status"
type: "int"
- name: "category_id"
type: "int"Filter early to reduce result set:
SEARCH articles tech FILTER status=1 FILTER category_id=5 LIMIT 1005. Optimize Query Patterns
SEARCH table term SORT id LIMIT 100 sorts by an id primary key when that is the configured key. SEARCH table term1 AND term2 intersects candidates from both terms. COUNT table term returns a count without a page of IDs, but still runs the search pipeline, including text verification when enabled. A large LIMIT returns more IDs and can increase response work. Measure these patterns with your own data rather than assuming a fixed speed ranking.
6. Use OPTIMIZE Command
Run OPTIMIZE to rebuild posting-list representations using their current density:
OPTIMIZEThis can convert dense delta-encoded posting lists to Roaring bitmaps. The memory change depends on the data. Allow roughly 2.1 times the index size as free memory before running OPTIMIZE; the command rejects a run when its admission estimate exceeds available memory.
Production Deployment Recommendations
1. Memory Sizing
Sizing reference: The published v1.10.0 benchmark used ~3.1GB of resident memory per million documents with verify_text: all. Current builds also retain normalized text with verify_text: off for highlighting, so that setting is not an index-only memory mode. Measure RSS with your dataset and add headroom.
Planning examples (verify_text: all):
- 1M documents: plan for at least 4GB RAM; 8GB leaves more room for growth
- 10M documents: plan for at least 32GB RAM; 48GB leaves more room for growth
- 100M documents: evaluate application-managed partitions across instances
These are rough estimates based on the benchmark dataset, not guarantees. Document length, filters, and index structure change memory use. Measure representative data and allow headroom.
2. High Availability Setup
Deploy multiple MygramDB instances behind a load balancer:
3. Monitoring
Monitor these metrics with the INFO command:
INFO| Field | Meaning |
|---|---|
total_documents | Number of indexed documents |
used_memory_index | Memory used by the index (human-readable) |
total_requests | Total queries processed |
connected_clients | Current active connections |
uptime_seconds | Server uptime in seconds |
4. Backup Strategy
Use the DUMP SAVE command to write a dump file:
DUMP SAVE snapshot.dmpSchedule regular dumps:
# Daily dump
0 2 * * * dump_date=$(date +\%Y\%m\%d); printf 'AUTH \%s\nDUMP SAVE mygramdb-\%s.dmp\n' "$MYGRAM_API_ADMIN_TOKEN" "$dump_date" | mygram-cliThese filenames resolve under dump.dir; paths outside it are refused. The cron example keeps AUTH and DUMP SAVE on one connection. AUTH is unnecessary only when api.admin_token is empty; configure a token for non-loopback deployments.
Troubleshooting
Query is Slower Than Expected
Check if index is optimized:
mygramOPTIMIZEVerify memory usage:
mygramINFOLook at
used_memory_indexand process RSS;hard_limit_mbis reserved and does not enforce a process memory limit today.Enable debug mode:
mygramDEBUG ON SEARCH table term LIMIT 100Review
query_time,index_time, andoptimizationfields.
High Memory Usage
Run OPTIMIZE:
mygramOPTIMIZECan convert dense posting lists to Roaring bitmaps. Check available memory first:
OPTIMIZEneeds roughly 2.1 times the index size free during rebuilding.Adjust roaring_threshold:
yamlmemory: roaring_threshold: 0.15 # Example value; measure its effect on your dataConsider application-managed sharding: Partition source data, run separate MygramDB instances for the partitions, and route or combine queries in the application. MygramDB does not shard automatically.
Comparison with Alternatives
vs MySQL FULLTEXT
MygramDB advantages:
- Cache-off measurements are maintained in the benchmark snapshot above
- Optional normalized-substring verification with
verify_text; tested benchmark queries returned the same counts as MySQL FULLTEXT, without a general semantic-equivalence guarantee - Search-index access does not depend on the MySQL buffer pool; MygramDB's own query-cache hits can still change latency
- The published concurrent measurements cover 1, 2, 4, 8 and 16 connections; other workloads may behave differently
MySQL FULLTEXT advantages:
- No separate infrastructure
- Works with existing MySQL data and SQL queries
vs Elasticsearch
MygramDB advantages:
- Simpler deployment (single binary)
- Lower operational complexity
- Direct MySQL replication (no ETL)
Elasticsearch advantages:
- Distributed search across nodes
- Advanced analytics and aggregations
- A broader distributed search and analytics ecosystem
- Not limited by single-node RAM
Benchmarking Your Own Data
The benchmark suite is reproducible. To run the same benchmark on your hardware:
# Run the included benchmark (requires Docker and rye)
make bench-up # Start MySQL with Wikipedia dataset
make bench-run # Execute benchmark suiteTo benchmark with your own data, start the server:
./mygramdb -c config.yamlWith auto_initial_snapshot disabled, use the same authenticated CLI connection to start the sync, check its status, enable debugging, and run the test queries:
$ mygram-cli
127.0.0.1:11016> AUTH <token>
OK AUTHENTICATED
127.0.0.1:11016> SYNC table
127.0.0.1:11016> SYNC STATUS
127.0.0.1:11016> DEBUG ON
127.0.0.1:11016> SEARCH table common_term LIMIT 100
127.0.0.1:11016> COUNT table common_termCompare the results with MySQL:
mysql -e "SELECT COUNT(*) FROM table WHERE MATCH(column) AGAINST('common_term')"
mysql -e "SELECT id FROM table WHERE MATCH(column) AGAINST('common_term') ORDER BY id LIMIT 100"Conclusion
The measured difference depends on the query, data, hardware, and cache configuration. The cache-off snapshot above is the canonical source for its latency, memory, and concurrent-throughput values.
With verify_text: all, MygramDB checks candidates using normalized substring matching. The tested benchmark queries returned the same counts as MySQL FULLTEXT; this is not a promise of identical semantics for arbitrary queries. CJK queries with few matches can narrow the latency gap because MySQL may finish quickly.
For read-heavy workloads with millions of documents, evaluate the included benchmark on your own data with make bench-up && make bench-run.