Reference

SQL & Redis

SQLite, PostgreSQL, Snowflake and Redis syntax, queries and everyday operations.

Scope and conventions

SQL examples name their database and assume the sample schema where indicated. Redis examples are native redis-cli commands, with optional Search/JSON features labeled. Run independent examples in your own scratch database; driver binding syntax and transaction guarantees differ.

Indexes & plans

Recognize an index and its access contract

Redis

FT.CREATE declares a Search index schema over HASH or JSON documents. TAG, TEXT and NUMERIC have distinct query semantics; SORTABLE supports the requested sorting access. The index is additional state, not a relational integrity constraint.

Fragment

Redis · Recognize an index and its access contract
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
FT.CREATE ref:other-items ON HASH PREFIX 1 ref:other-items: SCHEMA name TEXT category TAG qty NUMERIC item_id NUMERIC SORTABLE

FT.CREATE ref:json-items ON JSON PREFIX 1 ref:json-items: SCHEMA $.qty AS qty NUMERIC
JSON.SET ref:json-items:1 $ '{"qty":2}'
FT.SEARCH ref:json-items "@qty:[2 2]" RETURN 1 qty DIALECT 2

Native commands in one redis-cli session; # lines are explanatory, not commands. Examples use disposable ref:* keys.

Explain selected access paths

Redis

FT.EXPLAIN describes Search query interpretation. FT.PROFILE executes a supported Search/Aggregate query to collect profile evidence. Neither is a SQL physical plan or interchangeable with SQLite VM/full-scan counters.

Fragment

Redis · Explain selected access paths
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
FT.EXPLAIN ref:items "@qty:[1 +inf]" DIALECT 2

Native commands in one redis-cli session; # lines are explanatory, not commands. Examples use disposable ref:* keys.

Order the columns of a composite index

Redis

Redis has no multicolumn index. The native equivalent of a leftmost prefix is a sorted set whose members all have score 0, with a composite key encoded in each member. ZRANGE ... BYLEX returns a lexicographic range, so a prefix query works and a query on a later component does not. Zero-pad numeric parts so they sort as text. The application must update this index whenever it writes the source record. In practice: use FT.CREATE (see “Recognize an index”) for multi-field queries, and a lex set only for one fixed access pattern.

Fragment

Redis · Order the columns of a composite index
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
# member = category:qty(zero-padded):item_id
ZADD ref:items:by-category-qty 0 paper:0002:1 0 paper:0000:2 0 lamp:0001:3
# category = paper  ->  paper:0000:2, paper:0002:1
ZRANGE ref:items:by-category-qty [paper: (paper; BYLEX
# category = paper AND qty >= 1  ->  paper:0002:1
ZRANGE ref:items:by-category-qty [paper:0001 (paper; BYLEX

Native commands in one redis-cli session (Redis 8+, which bundles Search, JSON and time series; earlier versions need the matching modules). # lines are explanatory, not commands. Examples use disposable ref:* keys.

Index a subset of rows or a computed value

Redis

FT.CREATE ... FILTER takes an expression that decides which keys are indexed. It works like a partial index: keys that fail the filter are left out of the index but stay in the keyspace. Prefixes limit which keys are considered at all. There is no general expression index, so store a computed field (for example a lower-cased name) in the hash and index that field. In practice: use FILTER for a stable subset such as in-stock records, and remember that a key leaves the index when an update makes it fail the filter.

Fragment

Redis · Index a subset of rows or a computed value
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
HSET ref:pi:1 name Notebook category paper qty 2
HSET ref:pi:2 name Pen category paper qty 0
FT.CREATE ref:pi-in-stock ON HASH PREFIX 1 ref:pi: FILTER "@qty > 0" SCHEMA category TAG name TEXT
# Only ref:pi:1 is indexed.
FT.SEARCH ref:pi-in-stock "@category:{paper}" RETURN 1 name DIALECT 2

Native commands in one redis-cli session (Redis 8+, which bundles Search, JSON and time series; earlier versions need the matching modules). # lines are explanatory, not commands. Examples use disposable ref:* keys.

Read a query plan and decide what to fix

Redis

Redis has no query planner. Each command documents its time complexity, and SLOWLOG records commands that exceeded the configured threshold. For Search queries, FT.PROFILE runs the query and reports, per iterator, the reading operations and estimated matches, plus time spent in each result processor. In practice: if SLOWLOG shows KEYS or full-collection commands on large keys, replace them with SCAN or bounded ranges. If an FT.PROFILE iterator reads far more entries than it returns, narrow the query with TAG or NUMERIC filters.

Fragment

Redis · Read a query plan and decide what to fix
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
SLOWLOG GET 5
FT.PROFILE ref:pi-in-stock SEARCH QUERY "@category:{paper}" DIALECT 2
# Read "Iterators profile" (Number of reading operations,
# Estimated number of matches) and "Result processors profile".

Native commands in one redis-cli session (Redis 8+, which bundles Search, JSON and time series; earlier versions need the matching modules). # lines are explanatory, not commands. Examples use disposable ref:* keys.