File size: 6,464 Bytes
cf6b956 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 | /*
* 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 <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#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 <db-path> <rounds> <n_point> <n_index> <n_range> "
"<n_irange> <nrows>\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;
}
|