Pushdown and the executor
How the front-end turns WHERE conditions into Raft DB filters, how its general SELECT executor runs, and the changes that made it level with MySQL in one process, with their records.
The general SELECT executor (D55)#
One executor, query.rs, runs every SELECT. It handles INNER, LEFT, RIGHT and CROSS joins and the comma, ON and USING, derived tables, WHERE, GROUP BY (expressions, aliases, positions), the aggregates COUNT, SUM, AVG, MIN, MAX and GROUP_CONCAT anywhere in an expression, HAVING, DISTINCT, ORDER BY, LIMIT, UNION [ALL], and scalar, IN and EXISTS subqueries (correlated or not; uncorrelated ones run once). Every table the statement names is read first, the outermost FROM's tables with the part of the WHERE that concerns only that table; then the executor works on rows in memory. An equality of two columns of the same kind joins with a hash; anything else is a nested loop; groups come out in the order their first row was met. ONLY_FULL_GROUP_BY is checked with MySQL's errors 1055 and 1140.
Pushdown: WHERE to Raft DB filters (D58, pushdown.rs)#
The simple parts of a WHERE (column op constant, IN, BETWEEN, joined by AND) become a filter that Raft DB runs next to the data with its indexes (hash for = and IN, ordered for ranges), so the connector receives the rows that can match instead of the table.
- The pushed filter is always a superset. Raft DB compares text by bytes and the front-end ignores case, so the connector checks the whole
WHEREagain.pushdown::exactmarks the comparisons Raft DB makes exactly as MySQL does, so that the second check can be skipped for them. - Whole-number columns are pushed against whole-number constants. Short text columns (
CHARandVARCHARup to 255) are pushed for=andINthrough a shadow field every row of the table carries (__l_<column>, the value in lower case); the index of such a column is made on the shadow. - Bounds on one field are one index range, and an integer primary key is indexed (D63).
- A join: the rows of the earlier tables and an equality of one of their columns with a column of the joined table give a list of key values (up to 2000 distinct keys); only the rows of the joined table with those values are read. In two processes, when the first table's own conditions already bound the joined key (a range of at most 256 whole numbers), the joined table is asked for the range before the first table has been read (
ahead, D68). - The atoms (
column, comparison, constants) do not depend on the SQL dialect, so the MariaDB dialect uses the same code.
The cost model of one process (D66)#
With embed, sessions reach rows through raftdb_server::Local, the in-process client (link.rs: Link::Tcp or Link::Local, the same operations):
- A read costs no round trip: a key read or an index-backed filter runs on the session's thread under the table's read lock and the row is decoded where it is stored (
Data::get_bin_with,scan_plan_with); the filter is handed over compiled (pushdown::raftdb_plan), not as text. - The writes of a transaction are answered when applied and wait for the disk once, at
COMMIT(Session::defer_writes,Data::sync). INSERT ... VALUES (...), (...)into a table with no unique indexes and no foreign keys to check is sent together (Data::insert_many): each row is still its own insert-if-absent with one wait for the disk per shard.COUNT(*),COUNT(c),SUM(c),MIN(c),MAX(c)of whole-number columns over one table, with aWHEREof exact comparisons, are computed in Raft DB (Session::aggregate_in_rdb); the others use the general executor.- The general executor reads only the columns the statement names, takes the rows of the outermost
FROMinstead of copying them, keeps table definitions shared, hashes join and group keys as values and computes plain-column aggregates from the rows.
Result recorded in D66: measured through the stock JDBC driver, the one-process mode is level with or ahead of MySQL 9.7 on every workload of the JDBC benchmark. In two processes every row operation pays a loopback round trip (about 60 us on the test PC) and reads stay behind MySQL. D69 states that two processes at 16 busy connections are not reliably ahead of MySQL on reads, and gives the reason: each hop to another thread is a wait for a processor when all processors are busy, and two processes have two more hops per row operation than one.
Closing the gaps in two processes (D68, D69)#
alloc_cache = on(default): a per-thread cache of freed blocks (raftdb_server::pool). A statement makes hundreds of small blocks, and on Windows the system allocator was most of the executor's time.spin_us = 150(default,0= never): a bounded look for the next packet before a session's thread sleeps, also on the connection to Raft DB while it waits for a read (raftdb-client). In two processes this takes about 30 us off each round trip.- Definition caches are read under a read-write lock without allocating a key.
- Protocol version 2: a statement is sent only the columns it reads (
Request::Project; a range read that sums one column of 60 rows dropped from about 12 KiB to about 0.5 KiB); a transaction waits for the disk once; a scan that no index narrows runs on a helper thread of the server; a dropped table fails the sessions on it with an error instead of closing their connections. - Shared connections:
raftdb_connections = 1andraftdb_share = reads(the defaults) put the sessions' reads by key on long-lived shared connections as streams (raftdb_client::Mux); scans, writes andSyncstay on the session's own connections.raftdb_share = allputs every row request on the shared connections, which suits a front-end on another machine or hundreds of sessions; measured with 16 busy sessions on 12 processors, reads by key were faster, range reads level, and joins and writes a tenth to a fifth slower.
Views, temporary tables and CREATE TABLE AS#
- Views (D70): definitions live in the hidden table
__rdb_viewsof each database.expand_viewsturns a view in aFROMinto a derived table made from its query (nested up to 16 levels);INSERT,UPDATEandDELETEon a one-table view are rewritten to the table. - Temporary tables (D72): a session's temporary table is a table named
__tmp<id>_<name>, dropped when the session ends. CREATE TABLE ... LIKEand... AS SELECT(D71):AS SELECTruns the query once, derives the column types with the expression typing of Expressions and types, and inserts the rows as oneINSERT; a failed insert drops the table.
Tools#
The project measures the front-end with its own benchmark programs, which are not part of the download: the executor alone (execbench), the server's own time for the reads the front-end asks of it (filterbench), the stream transport (muxbench) and a comparison with real MySQL over JDBC. RAFTDB_MYSQL_PROF=1 shows the parts of every statement. Figures are in the decisions D62 to D69 (see the decision log and the performance chapter).