/* * sqlite_query.c -- steady-state query execution driver. * * Opens an already-populated database READ-ONLY and then does nothing but * repeatedly execute prepared queries against it. There is no CREATE, no * INSERT, no schema construction and no dataset loading anywhere in this * program; the only non-query work is sqlite3_open_v2() + sqlite3_prepare_v2() * at startup, a few million instructions before the steady-state loop begins. * * The database is mapped with PRAGMA mmap_size so page reads are served * straight out of the mapping: there is no multi-hundred-million-instruction * page-cache warm-up phase, the loop is in steady state almost immediately. * * The loop rotates through four query shapes, each of which exercises the * fixed-layout internal structures of interest: * 1. rowid point lookup -> table B-tree descent, record lookup * 2. secondary index lookup -> index B-tree descent + rowid table seek * 3. rowid range scan -> cursor movement (sqlite3BtreeNext) over pages * 4. index range scan -> index cursor movement + key comparison * Every iteration resets the statement, so VDBE program state management and * cursor teardown/re-seek are on the hot path as well. */ #include #include #include #include #include "sqlite3.h" static uint64_t rng; static uint64_t next_rand(void) { rng = rng * 6364136223846793005ULL + 1442695040888963407ULL; return rng >> 17; } static void must(sqlite3 *db, int rc, const char *what) { if (rc != SQLITE_OK && rc != SQLITE_DONE && rc != SQLITE_ROW) { fprintf(stderr, "%s failed: %d %s\n", what, rc, sqlite3_errmsg(db)); exit(1); } } int main(int argc, char **argv) { if (argc != 8) { fprintf(stderr, "usage: %s " " \n", argv[0]); return 2; } const char *path = argv[1]; long rounds = atol(argv[2]); long n_point = atol(argv[3]); long n_index = atol(argv[4]); long n_range = atol(argv[5]); long n_irange = atol(argv[6]); long nrows = atol(argv[7]); sqlite3 *db = NULL; must(db, sqlite3_open_v2(path, &db, SQLITE_OPEN_READONLY, NULL), "open"); /* locking_mode=EXCLUSIVE takes the shared file lock once instead of * fcntl()-ing on every implicit read transaction; without it roughly two * syscalls per query dominate the profile and bury the B-tree work. */ sqlite3_exec(db, "PRAGMA locking_mode=EXCLUSIVE;", NULL, NULL, NULL); sqlite3_exec(db, "PRAGMA mmap_size=2147483648;", NULL, NULL, NULL); sqlite3_exec(db, "PRAGMA cache_size=-524288;", NULL, NULL, NULL); sqlite3_exec(db, "PRAGMA query_only=ON;", NULL, NULL, NULL); sqlite3_stmt *q_point = NULL, *q_index = NULL, *q_range = NULL, *q_irange = NULL; must(db, sqlite3_prepare_v2(db, "SELECT v0, v1, payload FROM records WHERE id = ?", -1, &q_point, NULL), "prepare point"); must(db, sqlite3_prepare_v2(db, "SELECT id, v0 FROM records INDEXED BY idx_k WHERE k = ?", -1, &q_index, NULL), "prepare index"); must(db, sqlite3_prepare_v2(db, "SELECT id, v0, v1 FROM records WHERE id BETWEEN ? AND ?", -1, &q_range, NULL), "prepare range"); must(db, sqlite3_prepare_v2(db, "SELECT id, k FROM records INDEXED BY idx_k " "WHERE k BETWEEN ? AND ?", -1, &q_irange, NULL), "prepare irange"); uint64_t checksum = 0; long rows_seen = 0; for (long r = 0; r < rounds; r++) { /* ---- phase 1: rowid point lookups (B-tree descent + record read) */ rng = 0x1234567ULL + (uint64_t)r; for (long i = 0; i < n_point; i++) { long id = (long)(next_rand() % (uint64_t)nrows) + 1; sqlite3_bind_int64(q_point, 1, id); while (sqlite3_step(q_point) == SQLITE_ROW) { checksum += (uint64_t)sqlite3_column_int64(q_point, 0); checksum ^= (uint64_t)sqlite3_column_int64(q_point, 1); const unsigned char *p = sqlite3_column_text(q_point, 2); checksum += p ? (uint64_t)p[0] : 0; rows_seen++; } sqlite3_reset(q_point); } /* ---- phase 2: secondary index lookups (index descent + rowid seek) */ rng = 0x89abcdefULL + (uint64_t)r; for (long i = 0; i < n_index; i++) { long k = (long)(next_rand() % (uint64_t)(2 * nrows)); sqlite3_bind_int64(q_index, 1, k); while (sqlite3_step(q_index) == SQLITE_ROW) { checksum += (uint64_t)sqlite3_column_int64(q_index, 0); checksum ^= (uint64_t)sqlite3_column_int64(q_index, 1); rows_seen++; } sqlite3_reset(q_index); } /* ---- phase 3: rowid range scans (cursor movement across pages) */ rng = 0xfeedfaceULL + (uint64_t)r; for (long i = 0; i < n_range; i++) { long lo = (long)(next_rand() % (uint64_t)(nrows - 128)) + 1; sqlite3_bind_int64(q_range, 1, lo); sqlite3_bind_int64(q_range, 2, lo + 63); while (sqlite3_step(q_range) == SQLITE_ROW) { checksum += (uint64_t)sqlite3_column_int64(q_range, 1); checksum ^= (uint64_t)sqlite3_column_int64(q_range, 2); rows_seen++; } sqlite3_reset(q_range); } /* ---- phase 4: index range scans (index cursor movement + compare) */ rng = 0xdeadbeefULL + (uint64_t)r; for (long i = 0; i < n_irange; i++) { long lo = (long)(next_rand() % (uint64_t)(2 * nrows - 256)); sqlite3_bind_int64(q_irange, 1, lo); sqlite3_bind_int64(q_irange, 2, lo + 127); while (sqlite3_step(q_irange) == SQLITE_ROW) { checksum += (uint64_t)sqlite3_column_int64(q_irange, 0); checksum ^= (uint64_t)sqlite3_column_int64(q_irange, 1); rows_seen++; } sqlite3_reset(q_irange); } } sqlite3_finalize(q_point); sqlite3_finalize(q_index); sqlite3_finalize(q_range); sqlite3_finalize(q_irange); sqlite3_close(db); fprintf(stderr, "rows=%ld checksum=%llu\n", rows_seen, (unsigned long long)checksum); return 0; }