原文链接:www.gbase.cn/community/p…
更多精彩内容尽在南大通用GBase技术社区,南大通用致力于成为用户最信赖的数据库产品供应商。
在数据库管理中,索引是优化查询性能的关键。GBase 8s 提供了多种方法来查询和管理索引,包括系统表查询、 oncheck 命令以及 dbschema 工具。本文将介绍这些方法,希望能帮助读者更好地理解和使用 GBase 8s 的索引管理功能。
一、使用系统表查询
- sysindexes - 索引基本信息表
- sysindices - 索引统计信息表
- sysfragments - 索引物理存储表
- sysconstraints - 约束信息表
注:查询时会遇到表的好多字段为0,此时需要更新下表的统计信息再查询 。
二、使用oncheck 命令
oncheck -ci
oncheck –cI
oncheck –pk
oncheck –pK
oncheck –pl
oncheck –pL
(1) oncheck -ci
功能:检查表的索引结构
用法:oncheck -ci 数据库名:表名
如果 检测到不一致,那么它会提示您确认修复该问题索引。如果 oncheck 未找到不一致性,那么以下消息出现:
validating indexes......该消息显示 oncheck 正在检查的索引的名称。
示例:
oncheck -ci test:test_index
Your evaluation license will expire on 2026-05-06 00:00:00
Validating indexes for test:root.test_index...
Index index_id
Index fragment partition rootdbs in DBspace rootdbs
如果指定了 -y (是)选项,那么自动修复索引。如果指定了 -n (否)选项,那么报告该问题,但不进行修复。
示例:
oncheck -ci -n test:test_index
Your evaluation license will expire on 2026-05-06 00:00:00
Validating indexes for test:root.test_index...
Index index_id
Index fragment partition rootdbs in DBspace rootdbs
oncheck -ci -y test:test_index
Your evaluation license will expire on 2026-05-06 00:00:00
Validating indexes for test:root.test_index...
Index index_id
Index fragment partition rootdbs in DBspace rootdbs
(2) oncheck -cI
功能:检查表的索引结构及索引中与 rowid 相关联的键值是否与该行中的键值相同。
oncheck -cI 数据库名:表名
示例:
oncheck -cI test:test_index
Your evaluation license will expire on 2026-05-06 00:00:00
Validating indexes for test:root.test_index...
Index index_id
Index fragment partition rootdbs in DBspace rootdbs
同样,如果指定了 -y (是)选项,那么自动修复索引。如果指定了 -n (否)选项,那么报告该问题,但不进行修复。
(3) oncheck -pk
功能: -pk 选项执行与 -ci 选项相同的检查,此外,在进行检查时,它还会显示所有指定的表中的所有索引的键值。
用法:oncheck -pk 数据库名:表名
示例:
oncheck -pk test:test_index
Index index_id on TBLspace test:root.test_index
Node Type: Root+Leaf
Level 0 Node 0x1 Prev 0 Next 0
Key: 1:
Key: 2:
(4) oncheck -pK
功能:-pK选项执行与 -cI 选项相同的检查,此外,在进行检查时,它还会显示键值和 rowid 。
用法:oncheck -pK 数据库名:表名
示例:
oncheck -pK test:test_index
Index index_id on TBLspace test:root.test_index
Node Type: Root+Leaf
Level 0 Node 0x1 Prev 0 Next 0
Key: 1:
100317 101
Key: 2:
100317 102
(5) oncheck –pl
功能:-pl 选项执行与 -ci 选项相同的检查,并显示键值,但它只检查叶节点索引页。它忽略根和分支节点。
用法:oncheck –pl数据库名:表名
示例:
oncheck -pl test:test_index
Index index_id on TBLspace test:root.test_index
Node Type: Root+Leaf
Level 0 Node 0x1 Prev 0 Next 0
Key: 1:
Key: 2:
(6) oncheck –pL
功能:-pL 选项执行与 -cI 选项相同的检查,并显示键值和 rowid ,但它只检查叶节点索引页。它忽略根和分支节点。
用法:oncheck –pL数据库名:表名
示例:
oncheck -pL test:test_index
Index index_id on TBLspace test:root.test_index
Node Type: Root+Leaf
Level 0 Node 0x1 Prev 0 Next 0
Key: 1:
100317 101
Key: 2:
100317 102
三、使用 dbschema 工具导出索引定义
(1)显示表结构和索引信息:
dbschema -d 数据库名 -t 表名 -ss
-d 指定数据库名 -d mydb
-t 导出指定表
-ss 只导出结构(不包含数据) -d mydb -ss
(2)只显示索引信息:
dbschema -d 数据库名 -t 表名 –si
-si 只导出索引
以上通过本文的介绍,你已经了解了 GBase 8s 中查询索引的多种方法,包括系统表查询、 oncheck 命令和 dbschema 工具。这些方法各有特点,适用于不同的场景。系统表查询适合获取索引的基本信息和统计信息, oncheck 命令适合检查和修复索引结构, dbschema 工具适合导出索引定义。希望这些内容能帮助你在实际工作中更好地管理和优化索引。如果你在使用过程中遇到任何问题,欢迎随时在社区中提问,我们在这里为你提供支持!
原文链接:www.gbase.cn/community/p…
更多精彩内容尽在南大通用GBase技术社区,南大通用致力于成为用户最信赖的数据库产品供应商。