mysql - 如何在多列上创建索引

标签 mysql hibernate orm jpa indexing

我们有以下实体关系,其中用户属于特定组织。我的查询看起来像“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;    
...
}

最佳答案

这可以使用 Hibernate 特定的 @Table 注释来实现。来自文档:

2.4.1 Entity

...

@Table(appliesTo="tableName", indexes = { @Index( name="index1", columnNames={"column1", "column2"} ) } ) creates the defined indexes on the columns of table tableName. This can be applied on the primary table or any secondary table. The @Tables annotation allows your to apply indexes on different tables. This annotation is expected where @javax.persistence.Table or @javax.persistence.SecondaryTable(s) occurs.

引用

关于mysql - 如何在多列上创建索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3725545/

相关文章:

java - 使用 JPA 1.0 来替代 @ElementCollection 来持久化 Map<String,String>?

php - 将当前日期时间插入自定义 WordPress 日期时间列

spring - 初始化 c3p0 池期间获取尝试失败

php - MYSQL 父子同表; PHP 嵌套在 parent 中的 child 作为多维数组

java - 跨连接子类问题进行限制条件查询

java - 在 Spring+JPA 中更新级联子级时出现 UnsupportedOperationException

python - 在sqlalchemy中使用column_property包括整数列

php - Doctrine 多级继承映射

mysql - 按列对 MySQL 行进行排序,但不按字母顺序排序

MySQL MIN/MAX 所有行