在pg11之后,引入了indnkeyatts字段,根据官方文档解释其作用:The number of key columns in the index, not counting any included columns, which are merely stored and do not participate in the index semantics。
第一感觉,和indnatts字段差不多,但两者的说明存在差异。后者是参与索引的列的计数,前者是关键列的计数。
于是做了一个小测试,如下:
新建test表
CREATE TABLE test (
a _int8 NULL,
b _int8 NULL,
c _int8 NULL
);
CREATE INDEX test_a_idx ON test (a);
--单列索引
CREATE INDEX test_a_idx1 ON test (a,b);
--组合索引
CREATE INDEX test_a_idx2 ON test (a,b) include (c);
--使用inclue创建非键列
---查询各个索引的计数
SELECT
indnkeyatts,indnatts,i_c.relname
FROM
pg_index i
JOIN
pg_class c ON c.oid = i.indrelid
JOIN
pg_class i_c ON i_c.oid = i.indexrelid
JOIN
pg_am am ON am.oid = i_c.relam
LEFT JOIN
pg_attribute a ON a.attrelid = c.oid AND a.attnum = ANY(i.indkey)
JOIN
pg_namespace n ON n.oid = c.relnamespace
WHERE
c.relname = 'test'
结果如下
indnkeyatts | indnatts | relname |
---|---|---|
2 | 3 | test_a_idx2 |
2 | 3 | test_a_idx2 |
2 | 3 | test_a_idx2 |
1 | 1 | test_a_idx |
2 | 2 | test_a_idx1 |
2 | 2 | test_a_idx1 |
感觉差异就在于对include字段的处理上。不知道还有其他情况,会影响计数的值,有了解的可以留言讨论下。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义