如何在多列上创建索引

use*_*108 5 mysql indexing orm hibernate jpa

我们有以下实体关系,其中用户属于特定组织。我的查询看起来像“select * from User where org=:org”或“select * from User where org=:org and type=:type”

我在 User 类上有单独的索引。第一个查询会很好,因为外键元素上有索引。第二个查询是否要求在 org 和 type 列上使用多列索引。如果是这样,我应该如何注释来创建一个这样的索引。

@Entity 
class User {
...

@ManyToOne 
@ForeignKey
@Index
Organization org;

@Index
Type type;    
...
}
Run Code Online (Sandbox Code Playgroud)

Pas*_*ent 2

使用 Hibernate 特定注释可以做到这一点@Table。从文档中:

2.4.1 实体

...

@Table(appliesTo="tableName", indexes = { @Index( name="index1", columnNames={"column1", "column2"} ) } ) 在 table 的列上创建定义的索引tableName。这可以应用于主表或任何辅助表。该@Tables注释允许您在不同的表上应用索引。此注释应出现在@javax.persistence.Table@javax.persistence.SecondaryTable(s)处。

参考