CREATE INDEX(创建索引)
适用于 ✅ 开源版 ✅ 专业版 ✅ 企业版
// Create an index on a single column create.createIndex("index").on("table", "column").execute(); // Create an index on several columns create.createIndex("index").on("table", "column1", "column2").execute();
CREATE UNIQUE INDEX(创建唯一索引)
在许多方言中,可以创建一个唯一索引,它就像一个约束(参见 ALTER TABLE 或 CREATE TABLE),但实际上并不是约束。大多数方言会自动创建一个索引来强制执行 UNIQUE
约束,因此使用约束可能会显得更简洁。 UNIQUE INDEX
是这样创建的
// Create an index on a single column create.createUniqueIndex("index").on("table", "column").execute(); // Create an index on several columns create.createUniqueIndex("index").on("table", "column1", "column2").execute();
排序索引
在大多数方言中,索引的列默认按升序排序。 如果您希望创建一个具有不同排序顺序的索引,您可以通过显式提供顺序来实现
// Create a sorted index on several columns create.createIndex("index").on( table(name("table")), field(name("column1")).asc(), field(name("column2")).desc() ).execute();
覆盖索引(带有 INCLUDE 子句)
一些方言在创建索引时支持 INCLUDE
子句。这对于创建覆盖索引很有用。 这些索引“覆盖”整个查询的需求,这样在堆表或聚集索引中找到投影的部分后,就不需要在二级查找中执行操作。 来自 INCLUDE
子句的列的数据将仅位于索引叶节点(对投影有用),而不是索引树结构(对搜索有用),这减少了索引维护开销和索引大小。
如果方言不支持此子句,jOOQ 将简单地将 INCLUDE
列添加到普通索引列列表中。
// Create a covering index with included columns create.createIndex("index").on("table", "search_column").include("projection_column").execute();
部分索引(带有 WHERE 子句)
一些方言在创建索引时支持 WHERE
子句。如果只有部分列数据需要包含在索引中,这对于大幅减少索引大小以及索引维护非常有用。
// Create a partial index create.createIndex("index").on("table", "column").where(field(name("column")).gt(0)).execute();
反馈
您对此页面有任何反馈吗? 我们很乐意听到您的反馈!