Sunday, January 26, 2025

Vector indexes, MariaDB & pgvector, large server, small dataset: part 2

This post has results for vector index support in MariaDB and Postgres. This work was done by Small Datum LLC and sponsored by the MariaDB Corporation. This is part 2 in a series that compares QPS and recall for the fashion-mnist-784-euclidean dataset using from 1 to 48 concurrent sessions on a large server. This is part 2 and part 1 is here. 

The purpose of this post is to explain the results I shared in part 1 where MariaDB does much better than pgvector at low and high concurrency but the performance gap isn't as large at medium concurrency where low means <= 4 concurrent sessions, medium means 8 to 20 and high means >= 24.

tl;dr

  • QPS is ~1.4X larger for MariaDB than for pgvector at 2 and 48 concurrent sessions
  • pgvector uses more CPU/query than MariaDB
  • MariaDB does more context switches /query than pgvector
  • MariaDB appears to use less CPU to compute euclidean distance

Benchmark

This post has much more detail about my approach in general and part 1 has more detail on this particular setup. I repeated the benchmark for 2 to 48 concurrent sessions because my test server has 48 cores. I use ann-benchmarks via my fork of a fork of a fork at this commit. 

For more on euclidean distance (L2) see here.

For MariaDB:
  • queries use ORDER by vec_distance_euclidean
  • create index uses DISTANCE=euclidean
For Postgres
  • queries use ORDER BY embedding::halfvec(...) <-> $name::halfvec(...)
  • create index uses USING hnsw ((embedding::halfvec(...)) halfvec_l2_ops

Results: QPS by concurrency

The charts in this section show QPS by concurrency level as the benchmark was repeated for 1 to 48 concurrent sessions (X concurrent sessions means X concurrent queries).

The charts come from this spreadsheet. All of the data from the benchmark is here and the data I scraped to make these charts is here. I used configurations that provide a recall of ~0.96.

  • MariaDB - ef_search=10, M=6
  • Postgres - ef_search=10, M=16, ef_construction=32

This chart has absolute QPS for each of the systems tested.

This chart has relative QPS which is: (QPS for Postgres / QPS for MariaDB).

  • The MariaDB advantage is larger at low and high concurrency.
  • The MariaDB advantage isn't as large between 8 and 20 concurrent sessions

And this table also has relative QPS.

  • The MariaDB advantage is larger at low and high concurrency.
  • The MariaDB advantage isn't as large between 8 and 20 concurrent sessions
  • pgvector.halfvecpgvector
    10.600.57
    20.710.69
    40.780.72
    80.890.83
    120.820.79
    160.980.86
    200.960.85
    240.820.81
    280.850.79
    320.900.86
    360.710.76
    400.840.75
    440.700.65
    480.730.64

    This table has (QPS / concurrency). For all systems tested the QPS per session decreases as the concurrency increases. I suspect the benchmark client is part of the problem but I am just speculating

    • multiprocessing.pool is used by both MariaDB and pgvector, which is good, less GIL. See here for MariaDB and for pgvector.
    • the benchmark client includes all of the time to process queries, including
      • creating & start multiprocessing.pool - perhaps the pool can be cached & reused across runs
      • creating a database connection
      • gathering results from the concurrent sessions - some of this is done in the main thread
    • AFAIK, the total number of queries per run is fixed, so the number of queries per session is less when there are more concurrent sessions and setup overhead (create database connection, create multiprocessing.pool, process results) becomes more significant as the concurrency level increases.

    MariaDBpgvector.halfvecpgvector
    14639.62803.52661.3
    23376.42405.62327.3
    42869.22226.82076.0
    82149.61922.21784.0
    121858.11522.21471.8
    161527.51498.31310.5
    201353.11303.11156.3
    241261.81039.81023.5
    281062.7906.6839.5
    32909.0814.8784.9
    36918.5649.0702.5
    40783.2659.8589.9
    44745.8518.9482.5
    48680.1495.9435.3

    Performance debugging

    The benchmark client does a lot of work (like checking results for recall) which means there is a brief burst of CPU overhead when queries run followed by longer periods where the benchmark client is processing the results. So I modified the benchmark client to only run queries in a loop and avoid other overheads like checking the results for recall. This makes it easier to collect performance data like CPU profiles (perf), PMP stacks and vmstat samples.

    Performance debugging: MariaDB

    From a test with 2 concurrent sessions the perf profile shows that much CPU is used to compute the dot product which is used to determine the distance between vectors:

    # Overhead  Command          Shared Object        Symbol
        16.89%  one_connection   mariadbd.orig        [.] FVector::dot_product
         4.71%  one_connection   mariadbd.orig        [.] escape_string_for_mysql
         3.42%  one_connection   mariadbd.orig        [.] search_layer
         2.99%  one_connection   mariadbd.orig        [.] buf_page_get_gen
         2.31%  one_connection   mariadbd.orig        [.] my_charlen_utf8mb4
         2.16%  one_connection   mariadbd.orig        [.] MYSQLparse
         2.03%  one_connection   libc.so.6            [.] __memmove_avx512_unaligned_erms
         1.74%  one_connection   mariadbd.orig        [.] PatternedSimdBloomFilter<FVectorNode>::Query
         1.58%  one_connection   libm.so.6            [.] __roundf
         1.49%  one_connection   mariadbd.orig        [.] mtr_memo_slot_t::release
         1.40%  one_connection   mariadbd.orig        [.] mhnsw_read_first
         1.32%  one_connection   mariadbd.orig        [.] page_cur_search_with_match
         1.09%  one_connection   libc.so.6            [.] __memcmp_evex_movbe
         1.06%  one_connection   mariadbd.orig        [.] FVectorNode::distance_to
         1.03%  one_connection   mariadbd.orig        [.] row_search_mvcc
         0.98%  one_connection   mariadbd.orig        [.] rec_get_offsets_func
         0.93%  one_connection   mariadbd.orig        [.] cmp_data
         0.93%  one_connection   mariadbd.orig        [.] alloc_root
         0.75%  one_connection   mariadbd.orig        [.] Visited::cmp

    And then the result with 48 concurrent sessions

    • the percentage of time in dot_product was ~17% above, but only ~11.5% here
    • more time is spent in InnoDB functions like buf_page_get_gen, mtr_memo_slot_t::release, page_cur_search_with_match, btr_cur_t_::search_leaf, ssux_lock::psd_read_lock, rec_get_offsets_func and buf_page_make_young_if_needed. Some of that might be expected but that can also be a sign of too much mutex contention in InnoDB.
    • I don't see signs of mutex contention in PMP output

    # Overhead  Command          Shared Object        Symbol
        11.49%  one_connection   mariadbd.orig        [.] FVector::dot_product
         7.17%  one_connection   mariadbd.orig        [.] buf_page_get_gen
         4.44%  one_connection   mariadbd.orig        [.] search_layer
         4.00%  one_connection   mariadbd.orig        [.] escape_string_for_mysql
         2.49%  one_connection   mariadbd.orig        [.] mtr_memo_slot_t::release
         2.23%  one_connection   mariadbd.orig        [.] page_cur_search_with_match
         1.86%  one_connection   mariadbd.orig        [.] MYSQLparse
         1.85%  one_connection   mariadbd.orig        [.] my_charlen_utf8mb4
         1.75%  one_connection   libc.so.6            [.] __memmove_avx512_unaligned_erms
         1.60%  one_connection   mariadbd.orig        [.] btr_cur_t::search_leaf
         1.38%  one_connection   mariadbd.orig        [.] ssux_lock::psi_rd_lock
         1.37%  one_connection   mariadbd.orig        [.] mhnsw_read_first
         1.32%  one_connection   mariadbd.orig        [.] FVectorNode::distance_to
         1.23%  one_connection   mariadbd.orig        [.] rec_get_offsets_func
         1.19%  one_connection   mariadbd.orig        [.] PatternedSimdBloomFilter<FVectorNode>::Query
         1.02%  one_connection   mariadbd.orig        [.] FVectorNode::load
         0.97%  one_connection   libm.so.6            [.] __roundf
         0.88%  one_connection   mariadbd.orig        [.] buf_page_make_young_if_needed
         0.83%  one_connection   mariadbd.orig        [.] cmp_dtuple_rec_with_match_low

    From vmstat with 2 concurrent sessions

    procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
     r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
     2  0  96784 89703408 1982284 34588692    0    0     0    70 1401 56743  3  1 95  0  0
     2  0  96784 89703408 1982284 34588696    0    0     0     9 1356 56588  4  1 96  0  0
     2  0  96784 89703408 1982284 34588696    0    0     0     0 1434 56755  3  1 95  0  0
     2  0  96784 89703408 1982284 34588696    0    0     0     0 1340 56629  4  1 96  0  0
     2  0  96784 89702672 1982288 34588700    0    0     0    86 1810 56874  4  1 95  0  0

    From vmstat with 48 concurrent sessions

    procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
     r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
    58  0  96784 89428544 1988284 34733232    0    0     0   290 27883 637320 80 20  0  0  0
    56  0  96784 89430560 1988284 34733236    0    0     0     0 28393 639018 81 19  0  0  0
    52  0  96784 89430560 1988284 34733236    0    0     0     0 28240 638914 80 20  0  0  0
    59  0  96784 89430560 1988284 34733236    0    0     0     0 33141 642055 80 20  1  0  0
    56  0  96784 89430560 1988284 34733236    0    0     0     0 29520 639747 81 19  0  0  0

    Comparing the vmstat results for 2 vs 48 sessions

    • CPU/query is (vmstat.us + vmstat.sy) / QPS
      • For 2 sessions it is ((4+5+4+5+5) / 5) / 6752.7 = .000681
      • For 48 sessions it is ((100+100+100+100+100)/5) / 32645.5 = .003063
      • CPU/query is ~4.5X larger at 48 sessions
    • Context switches /query is vmstat.cs / QPS
      • For 2 sessions it is 56743 / 6752.7 = 8.40
      • For 48 sessions it is 637320 / 32645.5 = 19.52
      • Context switches /query is ~2.3X larger at 48 sessions

    Performance debugging: pgvector with halfvec

    From a test with 2 concurrent sessions the perf profile shows

    • computing L2 distance accounts for the most time, here it is 25.98% while above for MariaDB at 2 concurrent sessions it was 16.89%. Perhaps MariaDB is faster at computing L2 distance, perhaps MariaDB has more overhead elsewhere to reduce the fraction of time in computing L2 distance. But I suspect that the former is true.
    • Postgres here appears to have more CPU overhead than MariaDB in accessing the data (PinBuffer, LWLock, etc)

    # Overhead  Command   Shared Object      Symbol
        25.98%  postgres  vector.so          [.] HalfvecL2SquaredDistanceF16c
         9.68%  postgres  postgres           [.] PinBuffer
         6.46%  postgres  postgres           [.] hash_search_with_hash_value
         5.39%  postgres  postgres           [.] LWLockRelease
         4.25%  postgres  postgres           [.] pg_detoast_datum
         3.95%  postgres  vector.so          [.] vector_to_halfvec
         3.06%  postgres  postgres           [.] LWLockAttemptLock
         2.97%  postgres  postgres           [.] LWLockAcquire
         2.89%  postgres  vector.so          [.] HnswLoadUnvisitedFromDisk
         2.44%  postgres  postgres           [.] StartReadBuffer
         1.82%  postgres  postgres           [.] GetPrivateRefCountEntry
         1.70%  postgres  vector.so          [.] tidhash_insert_hash_internal
         1.65%  postgres  postgres           [.] LockBuffer
         1.59%  postgres  vector.so          [.] HnswLoadElementImpl
         0.88%  postgres  libc.so.6          [.] __memcmp_evex_movbe
         0.80%  postgres  postgres           [.] ItemPointerEquals
         0.79%  postgres  postgres           [.] ResourceOwnerForget
         0.71%  postgres  vector.so          [.] HnswSearchLayer
         0.64%  postgres  postgres           [.] pq_getmsgfloat4

    And then from a test with 48 concurrent sessions

    • The fraction of time computing L2 distance here is less than it is for 2 sessions above. This is similar to the results for MariaDB.
    • From PMP I don't see signs of mutex contention, but I only took 3 samples

    # Overhead  Command   Shared Object      Symbol
        19.86%  postgres  vector.so          [.] HalfvecL2SquaredDistanceF16c
        10.89%  postgres  postgres           [.] PinBuffer
         6.78%  postgres  postgres           [.] hash_search_with_hash_value
         5.44%  postgres  postgres           [.] LWLockRelease
         5.25%  postgres  postgres           [.] LWLockAttemptLock
         4.97%  postgres  postgres           [.] pg_detoast_datum
         3.30%  postgres  vector.so          [.] vector_to_halfvec
         2.62%  postgres  vector.so          [.] HnswLoadUnvisitedFromDisk
         2.61%  postgres  postgres           [.] StartReadBuffer
         2.03%  postgres  postgres           [.] GetPrivateRefCountEntry
         1.70%  postgres  postgres           [.] LockBuffer
         1.69%  postgres  vector.so          [.] HnswLoadElementImpl
         1.49%  postgres  postgres           [.] LWLockAcquire
         1.28%  postgres  vector.so          [.] tidhash_insert_hash_internal
         0.79%  postgres  postgres           [.] ReadBufferExtended
         0.78%  postgres  postgres           [.] AllocSetAlloc
         0.76%  postgres  libc.so.6          [.] __memcmp_evex_movbe
         0.65%  postgres  postgres           [.] ResourceOwnerForget
         0.63%  postgres  vector.so          [.] HnswSearchLayer

    From vmstat with 2 concurrent sessions

    procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
     r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
     2  0  96784 90724048 1997176 36391152    0    0     0    80 4188 24830  4  0 96  0  0
     2  0  96784 90724304 1997176 36391152    0    0     0     9 4079 24931  4  0 96  0  0
     2  0  96784 90724304 1997176 36391152    0    0     0   210 4117 24857  4  0 96  0  0
     2  0  96784 90724048 1997176 36391152    0    0     0    42 4160 24946  4  0 96  0  0
     2  0  96784 90723576 1997176 36391152    0    0     0    51 4511 24971  4  1 96  0  0

    From vmstat with 48 concurrent sessions

    procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
     r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
    88  0  96784 90323440 2001256 36414792    0    0     0     0 13129 224133 92  8  0  0  0
    86  0  96784 90323440 2001256 36414796    0    0     0   197 13011 223926 92  8  0  0  0
    85  0  96784 90322936 2001256 36414796    0    0     0    48 13118 224239 92  8  0  0  0
    83  0  96784 90322688 2001264 36414800    0    0     0    56 13084 223987 92  8  0  0  0
    86  0  96784 90324952 2001264 36414800    0    0     0    17 13361 224189 92  8  0  0  0

    Comparing the vmstat results for 2 vs 48 sessions

    • QPS is ~1.4X larger for MariaDB at 2 and 48 sessions
    • CPU/query is (vmstat.us + vmstat.sy) / QPS
      • For 2 sessions it is ((4+4+4+4+5)/5) / 4811.2 = .000872
      • For 48 sessions it is ((100+100+100+100+100)/5) / 23802.6 = .004201
      • CPU/query is ~4.8X larger at 48 sessions than at 2
      • CPU/query for Postgres is ~1.3X larger than MariaDB at 2 sessions and ~1.4X larger at 48
    • Context switches is vmstat.cs / QPS
      • For 2 sessions it is 24830 / 4811.2 = 5.16
      • For 48 sessions it is 224133 / 23802.6 = 9.41
      • Context switches /query is ~1.8X larger at 48 sessions
      • Context switches /query for MariaDB is ~1.6X larger than Postgres at 2 sessions and ~2.1X larger at 48


    Saturday, January 25, 2025

    Vector indexes, MariaDB & pgvector, large server, small dataset: part 1

    This post has results for vector index support in MariaDB and Postgres. This work was done by Small Datum LLC and sponsored by the MariaDB Corporation. 

    My previous posts (here and here) used a server with 8 cores and 32G of RAM. While that was OK for one of the smaller datasets from ann-benchmarks it wasn't enough for larger datasets and the problem was the amount of memory used by the benchmark client. I have some changes to the benchmark client to reduce the transient spikes in memory usage I wasn't able to fully solve the problem, so I moved to a larger server with 48 cores and 128G of RAM. 

    This post has results for the fashion-mnist-784-euclidean dataset using from 1 to 48 concurrent sessions. This is part 1. There will be parts 2, 3 and 4 to explain the results and then I move on to a larger dataset.

    I compare MariaDB with pgvector because I respect the work that the Postgres community has done to support vector search workloads. And I am happy to report that MariaDB has also done a great job on this. While I don't know the full story of the development effort, this feature came from the MariaDB Foundation and the community and it is wonderful to see that collaboration.

    tl;dr

    • At low and high concurrency levels MariaDB gets much more QPS for a given recall target. Here low means <= 4 concurrent sessions and high means >= 24. 
    • At middle concurrency levels (8 through 20 concurrent sessions) MariaDB still does better but the gap isn't as large. I try to explain this in future posts.
    Benchmark

    This post has much more detail. However I switched to a larger server (Hetzner ax162-s) with 48 cores, 128G of RAM, Ubuntu 22.04 and HW RAID 10 using 2 NVMe devices.

    I use ann-benchmarks via my fork of a fork of a fork at this commit. Note that parallel index create was disabled for Postgres by my configuration and isn't (yet) supported by MariaDB.

    I ran tests for fashion-mnist-784-euclidean at 1, 2, 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44 and 44 concurrent sessions. The command lines were the following using pgvector, pgvector_halfvec and mariadb as the value of $alg. When --batch is used the concurrency level (between 2 and 48 concurrent sessions) is set by an environment variable (POSTGRES_BATCH_CONCURRENCY or MARIADB_BATCH_CONCURRENCY)
        python3 run.py --algorithm $alg  --dataset fashion-mnist-784-euclidean --timeout -1 --local --force \
            --respect_config_order --runs 3
        python3 run.py --algorithm $alg  --dataset fashion-mnist-784-euclidean --timeout -1 --local --force \
            --respect_config_order --runs 3 --batch

    I filed MDEV-35897 for MariaDB because it allocates and then deallocates too much memory when ef_search is large, and large for me was >= 300. The overhead from this hurts query response times. Fortunately the fix should be easy. For now I changed config.yml for MariaDB to not use ef_search values larger than 200 (see query_args here).

    Files:
    Results: QPS vs recall graphs

    The recall vs QPS graph is created by running plot.py from ann-benchmarks. The line colors are red for MariaDB, dark blue for pgvector with halfvec (float16) and light blue for pgvector.

    1 session (no concurrency)
    2 sessions
    4 sessions
    8 sessions
    12 sessions
    16 sessions
    20 sessions
    24 sessions
    28 sessions
    32 sessions
    36 sessions
    40 sessions
    44 sessions
    48 sessions



















    Monday, January 13, 2025

    Evaluating vector indexes in MariaDB and pgvector: part 2

    This post has results from the ann-benchmarks with the fashion-mnist-784-euclidean dataset for MariaDB and Postgres (pgvector) with concurrent queries (--batch). My previous post has results when not using concurrent queries. This work was done by Small Datum LLC and sponsored by the MariaDB Corporation.

    I compare MariaDB with pgvector because I respect the work that the Postgres community has done to support vector search. And I am happy to report that MariaDB has also done a great job on this. While I don't know the full story of the development effort, this feature came from the MariaDB Foundation and the community and it is wonderful to see that collaboration.

    Performance for MariaDB is excellent
    • peak QPS for a given recall target is much better than pgvector
    • time to create the index to achieve a given recall target is almost always much better than pgvector

    Benchmark

    Part 1 has all of the details.

    I am using the ann-benchmarks via my fork of a fork of a fork at this commit. I added --batch to the command lines listed in part 1 to repeat the benchmark using concurrent queries. Note that parallel index create was disabled for Postgres and isn't (yet) supported by MariaDB.

    With --batch there is one concurrent query per CPU core and my server has 8 cores.

    Files related to these tests are archived here.

    Results: QPS vs recall graphs

    The recall vs QPS graph is created by: python3 plot.py --dataset fashion-mnist-784-euclidean --batch. This chart is very similar to the chart in part 1. One difference is that the peak QPS for MariaDB and Postgres are ~6000/s and ~4000/s there vs ~22000/s and ~15000/s here.

    The results below for MariaDB are excellent. It gets more QPS than pgvector at a given recall target.


    Results: create index

    I am still trying to figure out how to present this data. All of the numbers are here for the time to create an index and the size of the index. The database configs for Postgres and MariaDB are shared above, and parallel index create is disabled by the config for Postgres (and not supported yet by MariaDB). The summary is:
    • Indexes have a similar size with MariaDB and Postgres with halfvec. The Postgres index without halfvec is about 2X larger.
    • Time to create the index for Postgres is similar with and without halfvec
    • Time to create the index for MariaDB is less than for pgvector. Perhaps the best way to compare this is the time to create the index for a similar point on the QPS/recall graph (see the last section of this blog post)
    Results: best QPS for a given recall

    Many benchmark results are marketed via peak performance (max throughput or min response time) but these are usually constrained optimization problems -- determine peak performance that satisfies some SLA. And the SLA might be response time or efficiency (cost).

    With ann-benchmarks the constraint might be recall where you seek the best QPS that satisfies a recall target. Below I share the best QPS for a given recall target along with the configuration parameters (M, ef_construction, ef_search) at which that occurs for each of the algorithms (MariaDB, pgvector with float32, pgvector with float16).

    For all cases below except the first (recall = 1.000) the time to create the index is less with MariaDB.

    For all cases below the best QPS at a given recall target is much better, sometimes 3X better, with MariaDB than with pgvector.

    Legend
    * recall & QPS - results from the benchmark
    * isecs - number of seconds to create the index for M and ef_cons (ef_cons)

    Best QPS with recall = 1.000
    recall  QPS     isecs   algorithm
    1.000    4727   115.9   PGVector(m=16, ef_cons=256, ef_search=120)
    1.000    5479    98.6   PGVector_halfvec(m=16, ef_cons=192, ef_search=120)
    1.000   13828   108.5   MariaDB(m=32, ef_search=10)

    Best QPS with recall >= 0.99
    recall  QPS     isecs   algorithm
    0.990   10704    71.2   PGVector(m=16, ef_cons=96, ef_search=20)
    0.991   11377    90.1   PGVector_halfvec(m=16, ef_cons=256, ef_search=20)
    0.995   17642    24.5   MariaDB(m=12, ef_search=10)

    Best QPS with recall >= 0.98
    recall  QPS     isecs   algorithm
    0.985   10843    51.0   PGVector(m=16, ef_cons=32, ef_search=20)
    0.984   11749    44.5   PGVector_halfvec(m=16, ef_cons=32, ef_search=20)
    0.995   17642    24.5   MariaDB(m=12, ef_search=10)

    Best QPS with recall >= 0.97
    recall  QPS     isecs   algorithm
    0.970   12836    35.1   PGVector(m=8, ef_cons=64, ef_search=20)
    0.973   13742    33.8   PGVector_halfvec(m=8, ef_cons=96, ef_search=20)
    0.995   17642    24.5   MariaDB(m=12, ef_search=10)

    Best QPS with recall >= 0.96
    recall  QPS     isecs   algorithm
    0.970   12836    35.1   PGVector(m=8, ef_cons=64, ef_search=20)
    0.973   13742    33.8   PGVector_halfvec(m=8, ef_cons=96, ef_search=20)
    0.995   17642    24.5   MariaDB(m=12, ef_search=10)

    Best QPS with recall >= 0.95
    recall  QPS     isecs   algorithm
    0.970   12836    35.1   PGVector(m=8, ef_cons=64, ef_search=20)
    0.973   13742    33.8   PGVector_halfvec(m=8, ef_cons=96, ef_search=20)
    0.995   17642    24.5   MariaDB(m=12, ef_search=10)

    Sunday, January 12, 2025

    Evaluating vector indexes in MariaDB and pgvector: part 1

    This post has results for vector index support in MariaDB and Postgres. I am new to vector indexes so I will start small and over time add more results.  This work was done by Small Datum LLC and sponsored by the MariaDB Corporation.

    I compare MariaDB with pgvector because I respect the work that the Postgres community has done to support vector search workloads. And I am happy to report that MariaDB has also done a great job on this. While I don't know the full story of the development effort, this feature came from the MariaDB Foundation and the community and it is wonderful to see that collaboration.

    tl;dr

    • Performance for MariaDB is excellent
      • peak QPS for a given recall target is much better than pgvector
      • time to create the index to achieve a given recall target is almost always much better than pgvector
    • MariaDB is easier to tune than pgvector
    • MariaDB needs more documentation
    The good and the less good

    The good for MariaDB starts with performance. The results I get are great and match the results in this blog post from upstream. Another good thing about MariaDB is that it is easier to tune. With pgvector I need to set M and ef_construction while creating an index and then ef_search while running a query, With MariaDB there is no option to set ef_construction. And evaluations are easier when there are fewer options to tune. Note that the M with pgvector isn't the same thing as the M with MariaDB, but they are similar (at least in spirit).

    The less good for MariaDB is the documentation. We need more, but that is easy to fix. I have been using:

    Things that need more documentation
    • the algorithm is described as a variation of HNSW (MHNSW) and the community will benefit from more details on what has been changed. For example, I can't set ef_construction and it always uses float16 in the index. But with pgvector I can use either float32 (default) or float16 (via halfvec).
    • What transaction isolation level is supported? I get that workloads will be read-heavy but I assume that some won't be read-only so I won't to know whether repeatable read and read committed are provided.
    • What concurrent operations are supported? Obviously, reads can be concurrent with other reads. But can writes be concurrent with reads or with other writes on the same table?
    Hardware

    The hardware is a Beelink SER7 7840HS with a Ryzen 7 7840HS CPU, 32G of RAM and Ubuntu 22.04.

    While I already installed some dependencies on this server long ago, to run this benchmark I did:

    sudo apt install libmariadb3 libmariadb-dev

    pip3 install mariadb

    pip3 install pgvector psycopg


    Database software

    For Postgres I compiled version 17.2 and pgvector 0.8.0 from source. Files that I used include:
    For MariaDB I compiled version 11.7.1 from source. Files that I used include:
    Benchmark

    I am using the ann-benchmarks via my fork of a fork of a fork at this commit. These forks have changes to run the benchmark for MariaDB and pgvector without using Docker containers. A request I have for anyone writing a benchmark client is to limit dependencies, or at least make them optional. I just want to point the benchmark client at an existing installation.

    The ann-benchmarks configuration files are here for MariaDB and for pgvector. I am open to feedback that I should try different parameters. I added support to use float16 (halfvec) for the pgvector index (but only the index, not for the vector stored in the table).

    In this post I use the fashion-mnist-784-euclidean dataset in non-batch mode where non-batch mode doesn't run concurrent queries and batch mode does. Note that I don't set --count when running the benchmark which means that all of the queries use LIMIT 10.

    Files related to these tests are archived here.

    The command lines for non-batch mode are:
    POSTGRES_CONN_ARGS=root:pw:127.0.0.1:5432 POSTGRES_DB_NAME=ib \
        python3 -u run.py  --algorithm pgvector --dataset fashion-mnist-784-euclidean --local

    POSTGRES_CONN_ARGS=root:pw:127.0.0.1:5432 POSTGRES_DB_NAME=ib \
        python3 -u run.py  --algorithm pgvector_halfvec --dataset fashion-mnist-784-euclidean --local

    MARIADB_CONN_ARGS=root:pw:127.0.0.1:3306 MARIADB_DB_NAME=test \
        python3 -u run.py  --algorithm mariadb --dataset fashion-mnist-784-euclidean --local

    Results: QPS vs recall graphs

    The recall vs QPS graph is created by: python3 plot.py --dataset fashion-mnist-784-euclidean

    The results below for MariaDB are excellent. It gets more QPS than pgvector at a given recall target.


    Results: create index

    I am still trying to figure out how to present this data. All of the numbers are here for the time to create an index and the size of the index. The database configs for Postgres and MariaDB are shared above, and parallel index create is disabled by the config for Postgres (and not supported yet by MariaDB). The summary is:
    • Indexes have a similar size with MariaDB and Postgres with halfvec. The Postgres index without halfvec is about 2X larger.
    • Time to create the index for Postgres is similar with and without halfvec
    • Time to create the index for MariaDB is less than for pgvector. Perhaps the best way to compare this is the time to create the index for a similar point on the QPS/recall graph (see the last section of this blog post)
    Results: best QPS for a given recall

    Many benchmark results are marketed via peak performance (max throughput or min response time) but these are usually constrained optimization problems -- determine peak performance that satisfies some SLA. And the SLA might be response time or efficiency (cost).

    With ann-benchmarks the constraint might be recall where you seek the best QPS that satisfies a recall target. Below I share the best QPS for a given recall target along with the configuration parameters (M, ef_construction, ef_search) at which that occurs for each of the algorithms (MariaDB, pgvector with float32, pgvector with float16).

    For all cases below except the first (recall = 1.000) the time to create the index is about 2X or more longer with pgvector than with MariaDB.

    For all cases below the best QPS at a given recall target is much better, sometimes 3X better, with MariaDB than with pgvector.

    Legend
    * recall & QPS - results from the benchmark
    * isecs - number of seconds to create the index for M and ef_construction (ef_cons)

    Best QPS with recall >= 1.000
    recall  QPS  isecs  algorithm
    1.000   920  97.4   PGVector(m=16, ef_cons=192, ef_search=120)
    1.000   991  89.8   PGVector_halfvec(m=16, ef_cons=256, ef_search=120)
    1.000  3226 111.8   MariaDB(m=32, ef_search=10)

    Best QPS with recall >= 0.99
    recall  QPS  isecs  algorithm
    0.990  2337  70.7   PGVector(m=16, ef_cons=96, ef_search=20)
    0.991  2558  78.3   PGVector_halfvec(m=16, ef_cons=192, ef_search=20)
    0.995  4745  27.4   MariaDB(m=12, ef_search=10)

    Best QPS with recall >= 0.98
    recall  QPS  isecs  algorithm
    0.985  2383  50.4   PGVector(m=16, ef_cons=32, ef_search=20)
    0.988  2608  52.8   PGVector_halfvec(m=16, ef_cons=64, ef_search=20)
    0.984  5351  18.1   MariaDB(m=8, ef_search=10)

    Best QPS with recall >= 0.97
    recall  QPS  isecs  algorithm
    0.972  3033  42.3   PGVector(m=8, ef_cons=96, ef_search=20)
    0.973  3185  89.8   PGVector_halfvec(m=16, ef_cons=256, ef_search=10)
    0.984  5351  18.1   MariaDB(m=8, ef_search=10)

    Best QPS with recall >= 0.96
    recall  QPS  isecs  algorithm
    0.961  3044   50.4  PGVector(m=16, ef_cons=32, ef_search=10)
    0.967  3250   52.8  PGVector_halfvec(m=16, ef_cons=64, ef_search=10)
    0.961  5691   14.8  MariaDB(m=6, ef_search=10)

    Best QPS with recall >= 0.95
    recall  QPS  isecs  algorithm
    0.961  3044  50.4   PGVector(m=16, ef_cons=32, ef_search=10)
    0.967  3250  52.8   PGVector_halfvec(m=16, ef_cons=64, ef_search=10)
    0.961  5691  14.8   MariaDB(m=6, ef_search=10)

    Thursday, January 9, 2025

    Sysbench performance over time for InnoDB and MyRocks: part 4

    This is part 4 in my (possibly) final series on performance regressions in MySQL using cached sysbench as the workload. For previous posts, see part 1, part 2 and part 3. This post covers performance differences between InnoDB in upstream MySQL 8.0.32, InnoDB in FB MySQL 8.0.32 and MyRocks in FB MySQL 8.0.32 using a server with 32 cores and 128G of RAM.

    I don't claim that the MyRocks CPU overhead isn't relevant, but this workload (CPU-bound, database is cached) is a worst-case for it.

    tl;dr 

    • InnoDB from FB MySQL is no worse than ~10% slower than InnoDB from upstream
    • Fixing bug 1506 is important for InnoDB in FB MySQL
    • MyRocks is ~30% slower than upstream InnoDB at low concurrency and ~45% slower at high, as it uses ~1.5X more CPU/query
    • For writes, MyRocks does worse at high concurrency than at low
    Updates: For writes, MyRocks does worse at high concurrency than at low

    I looked at vmstat metrics for the update-nonindex benchmark and the number of context switches per update is about 1.2X larger for MyRocks vs InnoDB at high concurrency. 

    Then I looked at PMP stacks and MyRocks has more samples for commit processing. The top stacks are here. This should not be a big surprise because MyRocks does more work at commit time (pushes changes from a per-session buffer into the memtable). But I need to look at this more closely.

    I browsed the code in Commit_stage_manager::enroll_for, which is on the call stack for the mutext contention, and it is kind of complicated. I am trying to figure out how many mutexes are locked in there and figuring that out will take some time. 

    Benchmark, Hardware

    Much more detail on the benchmark and hardware is here. I am trying to avoid repeating that information in the posts that follow. 

    Results here are from the c32r128 server with 32 CPU cores and 128G of RAM. The benchmarks were repeated for 1 and 24 threads. On the charts below that is indicated by NT=1 and NT=24.

    Builds

    The previous post has more detail on the builds, my.cnf files and bug fixes.

    The encoded names for these builds is:
    • my8032_rel_o2nofp
      • InnoDB from upstream MySQL 8.0.32
    • fbmy8032_rel_o2nofp_end_241023_ba9709c9_971
      • FB MySQL 8.0.32 at git hash ba9709c9 (as of 2024/10/23) using RocksDB 9.7.1. This supports InnoDB and MyRocks.
    • fbmy8032_rel_o2nofp_241023_ba9709c9_971_bug1473_1481_1482_1506
      • FB MySQL 8.0.32 at git hash ba9709c9 (as of 2024/10/23) using RocksDB 9.7.1 with patches applied for bugs 1473, 1481, 1482 and 1506, This supports InnoDB and MyRocks.
    The my.cnf files are:
    Relative QPS

    The charts and summary statistics that follow use a number that I call the relative QPS (rQPS) where:
    • rQPS is: (QPS for my version) / (QPS for base version)
    • base version is InnoDB from upstream MySQL 8.0.32 (my8032_rel_o2nofp)
    • my version is one of the other versions
    Results

    The microbenchmarks are split into three groups: point queries, range queries, writes. The tables below have summary statistics for InnoDB and MyRocks using the relative QPS (the same data as the charts).

    Results are provided in two formats: charts and summary statistics. The summary statistics table have the min, max, average and median relative QPS per group (group = point, range and writes).

    The spreadsheets and charts are also here. I don't know how to prevent the microbenchmark names on the x-axis from getting truncated in the png files I use here but they are easier to read on the spreadsheet.

    The charts use NT=1, NT=16 and NT=24 to indicate whether sysbench was run with 1, 16 or 24 threads. The charts and table use the following abbreviations for the DBMS versions:
    • fbinno-nofix
      • InnoDB from fbmy8032_rel_o2nofp_end_241023_ba9709c9_971
    • fbinno-somefix
      • InnoDB from fbmy8032_rel_o2nofp_241023_ba9709c9_971_bug1473_1481_1482_1506
    • myrocks-nofix
      • MyRocks from fbmy8032_rel_o2nofp_end_241023_ba9709c9_971
    • myrocks-somefix
      • MyRocks from fbmy8032_rel_o2nofp_241023_ba9709c9_971_bug1473_1481_1482_1506

    Summary statistics: InnoDB

    Summary:

    • InnoDB from FB MySQL is no worse than ~10% slower than InnoDB from upstream
    • Fixing bug 1506 is important for InnoDB in FB MySQL
    1 thread

    fbinno-nofixminmaxaveragemedian
    point0.890.960.920.91
    range0.630.930.820.82
    writes0.860.980.890.88
    fbinno-somefixminmaxaveragemedian
    point0.921.000.960.95
    range0.890.960.910.91
    writes0.890.990.920.92

    24 threads

    fbinno-nofixminmaxaveragemedian
    point0.920.960.940.94
    range0.620.960.810.82
    writes0.840.940.880.87
    fbinno-somefixminmaxaveragemedian
    point0.940.990.970.98
    range0.780.990.890.91
    writes0.860.950.900.88

    Summary statistics: MyRocks

    Summary:

    • MyRocks does better at low concurrency than at high. The fix might be as simple as enabling the hyper clock block cache
    • MyRocks is ~30% slower than upstream InnoDB at low concurrency and ~45% slower at high
    • For writes, MyRocks does worse at high concurrency than at low
    1 thread

    myrocks-nofixminmaxaveragemedian
    point0.520.750.660.68
    range0.370.720.600.60
    writes0.651.210.790.73
    myrocks-somefixminmaxaveragemedian
    point0.510.790.680.70
    range0.430.760.620.61
    writes0.661.230.800.74

    24 threads

    myrocks-nofixminmaxaveragemedian
    point0.400.760.490.43
    range0.400.710.580.60
    writes0.441.370.650.55
    myrocks-somefixminmaxaveragemedian
    point0.480.770.550.51
    range0.430.710.600.60
    writes0.451.390.660.55

    Results: c32r128 with InnoDB and point queries

    Summary
    • InnoDB from FB MySQL is no worse than 10% slower than upstream

    Results: c32r128 with MyRocks and point queries

    Summary
    • at low concurrency the worst case for MyRocks are the tests that do point lookup on secondary indexes because that uses a range scan rather than a point lookup on the LSM tree, which means that bloom filters cannot be used
    • at high concurrency the difference between primary and secondary index queries is less significant, perhaps this is dominated by mutex contention from the LRU block cache and solved by using hyper clock

    Results: c32r128 with InnoDB and range queries

    Summary

    • the worst case for InnoDB from FB MySQL are the long range scans and fixing bug 1506 will be a big deal

    Results: c32r128 with MyRocks and range queries

    Summary

    • while long range scans are the worst case here, bug 1506 is not an issue as that is InnoDB-only

    Results: c32r128 with InnoDB and writes

    Summary

    • results are stable here, InnoDB from FB MySQL is no worse than ~10% slower than upstream but results at high concurrency are a bit worse than at low

    Results: c32r128 with MyRocks and writes

    Summary

    • while MyRocks does much better than InnoDB for update-index because it does blind writes rather than RMW for non-unique secondary index maintenance
    • MyRocks does worse at high concurrency than at low




    CPU-bound sysbench on a large server: Postgres 12 to 19 beta1

    This has results from sysbench on a small server with Postgres versions 12 through 19 beta1. Sysbench is run with high concurrency (40 conne...