How does SQLite filter and sort assets with one index?
Follow three local assets and the SQLite query plan to see how a search can read titles and paths directly from an index.
Example result SQLite 3.54.1 uses a covering index search in place of the table scan and temporary sort. The wood rows are Acorn and Leaf, in that order.
Can one index filter and sort assets?
A local engraving library might filter designs by material and display them by name. The demo uses the SQLite project's embedded database engine to build a three-row catalog in memory. The query picks designs in the wood category and returns each SVG path in title order. Here an independent sample table lets us examine the query.
SQLite 3.54.1 release notes · SQLite project mirror at the pinned tag · Public-domain dedication
Read the matching range from the B-tree
The query uses WHERE material = ? ORDER BY title and binds the material through the C API. Before the index, SQLite scans the table and uses a temporary B-tree for sorting. With an index on (material,title,path), entries for a material sit together and are ordered by title within that group. SQLite can seek to the start of the wood range and advance until the keys leave that material. The index holds both output columns, so the query can read them directly from it.
In the pinned where.c, wherePathSolver weighs access paths and sorting cost. The WHERE_IDX_ONLY flag affects whether the table is read. In wherecode.c, index-scan code builds the start and stop instructions from the constraints. For a forward equality-prefix scan like this, OP_SeekGE can find the start, and OP_IdxGT can detect the end of the matching group. The demo displays the higher-level query plan. Access-path solver · Index-scan code generation
A covering index stores an extra copy of each path and needs maintenance when assets change. For a sortable, paginated asset browser, specify the collation and a stable tie-breaker for identical titles, then inspect the plan for that query. Three in-memory rows illustrate how SQLite reads a query, while write costs and cold-cache performance require a representative file and workload. SQLite query planner documentation
Build and run locally
Use a fresh directory on macOS or Linux with curl, unzip, and a C/C++ toolchain that supports C++17. Compile the upstream amalgamation file sqlite3.c as C, then link it to the demo. The example creates its entire database in memory. It was run on Apple Silicon macOS.
Download the C++17 example · Build commands in plain text
View the example source
#include <sqlite3.h>
#include <iostream>
#include <memory>
#include <stdexcept>
#include <string>
#include <string_view>
void check(sqlite3* db, int status) {
if (status != SQLITE_OK) throw std::runtime_error(sqlite3_errmsg(db));
}
template <typename ReadRow>
void query(sqlite3* db, const std::string& sql, ReadRow readRow) {
sqlite3_stmt* raw = nullptr;
check(db, sqlite3_prepare_v2(db, sql.c_str(), -1, &raw, nullptr));
const std::unique_ptr<sqlite3_stmt, decltype(&sqlite3_finalize)>
statement(raw, sqlite3_finalize);
check(db, sqlite3_bind_text(raw, 1, "wood", -1, SQLITE_STATIC));
int status;
while ((status = sqlite3_step(raw)) == SQLITE_ROW) readRow(raw);
if (status != SQLITE_DONE) check(db, status);
}
int main() try {
if (std::string_view(sqlite3_libversion()) != "3.54.1") {
throw std::runtime_error("This demo pins SQLite 3.54.1");
}
sqlite3* raw = nullptr;
const int status = sqlite3_open(":memory:", &raw);
const std::unique_ptr<sqlite3, decltype(&sqlite3_close)> db(raw, sqlite3_close);
if (!db) throw std::runtime_error("Could not allocate SQLite connection");
check(db.get(), status);
check(db.get(), sqlite3_exec(db.get(), R"SQL(
CREATE TABLE assets (
id INTEGER PRIMARY KEY, material TEXT NOT NULL,
title TEXT NOT NULL, path TEXT NOT NULL
);
INSERT INTO assets VALUES
(1, 'wood', 'Leaf', 'leaf.svg'),
(2, 'wood', 'Acorn', 'acorn.svg'),
(3, 'acrylic', 'Moon', 'moon.svg');
)SQL", nullptr, nullptr, nullptr));
const std::string select =
"SELECT title, path FROM assets WHERE material = ? ORDER BY title";
const auto printPlan = [&] {
query(db.get(), "EXPLAIN QUERY PLAN " + select, [](sqlite3_stmt* row) {
std::cout << " " << sqlite3_column_text(row, 3) << '\n';
});
};
std::cout << "SQLite " << sqlite3_libversion() << '\n'
<< "Before index:\n";
printPlan();
check(db.get(), sqlite3_exec(db.get(),
"CREATE INDEX assets_by_material_title_path ON assets(material,title,path)",
nullptr, nullptr, nullptr));
std::cout << "After index:\n";
printPlan();
std::cout << "Rows:\n";
query(db.get(), select, [](sqlite3_stmt* row) {
std::cout << " " << sqlite3_column_text(row, 0) << " | "
<< sqlite3_column_text(row, 1) << '\n';
});
} catch (const std::exception& error) {
std::cerr << error.what() << '\n';
return 1;
}mkdir sqlite-asset-study && cd sqlite-asset-study
curl -fsSL https://www.sqlite.org/2026/sqlite-amalgamation-3540100.zip -o sqlite.zip
unzip -q sqlite.zip
curl -fsSLO https://www.juyaoai.com/examples/sqlite-asset-index/demo.cpp
cc -O2 -c sqlite-amalgamation-3540100/sqlite3.c -o sqlite3.o
c++ -std=c++17 -I sqlite-amalgamation-3540100 demo.cpp sqlite3.o -pthread -ldl -lm -o sqlite-demo
./sqlite-demoRun output
SQLite 3.54.1
Before index:
SCAN assets
USE TEMP B-TREE FOR ORDER BY
After index:
SEARCH assets USING COVERING INDEX assets_by_material_title_path (material=?)
Rows:
Acorn | acorn.svg
Leaf | leaf.svg
Try dropping path from the end of the index definition. In that run, SQLite still filters and scans in title order using the index prefix and visits the table to fetch each path. SQLite provides EXPLAIN QUERY PLAN for diagnostics, and applications should avoid depending on its output text across releases. Query plan output documentation