How do you inspect the key order of a composite index?

index_info reports the key columns of an index with their sequence positions. Compare that prefix against a query's filtering and ordering needs.

SQLite example and interpretation

An event index on (chain,height) lists chain first. Metadata establishes the definition. Inspect the plan to see whether a given query actually uses it.

CREATE TABLE e(chain TEXT,height INTEGER);
CREATE INDEX event_order ON e(chain,height);
SELECT seqno,name FROM pragma_index_info('event_order') ORDER BY seqno;

Result [[0, "chain"], [1, "height"]]

中文

怎样核对复合索引里哪个键排在最前?

index_info 按序列位置列出指定索引的键列,可用于对照查询过滤与排序所需的前缀。

SQLite 示例与适用条件

给链上事件建 (chain,height) 索引,查询元数据可核对 chain 在首位。元数据只能说明定义,实际使用情况要看查询计划。

CREATE TABLE e(chain TEXT,height INTEGER);
CREATE INDEX event_order ON e(chain,height);
SELECT seqno,name FROM pragma_index_info('event_order') ORDER BY seqno;

结果 [[0, "chain"], [1, "height"]]

References / 参考来源

Keep reading / 继续阅读