【发布时间】:2010-09-20 08:29:21
【问题描述】:
我的对象模型如下所示,并希望您输入要创建的索引数量以加快查询响应(在 h2、mysql 上)。以下模型给出了假设和问题。
@Entity
@Table(name = "user")
public class User {
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "id", unique = true, nullable = false, insertable = false, updatable = false)
private Integer id;
@ManyToOne(fetch = FetchType.LAZY)
@ForeignKey(name = "fk_user_org_id")
@Index(name = "idx_user_org_id")
@JoinColumn(name = "org_id", nullable = false, referencedColumnName = "id")
@NotNull
private Organization organization;
@ManyToOne(fetch = FetchType.LAZY)
@ForeignKey(name = "fk_user_year_id")
@Index(name = "idx_user_year_id")
@JoinColumn(name = "year", nullable = false, referencedColumnName = "id")
@NotNull
private Year year;
@ManyToOne(fetch = FetchType.LAZY)
@ForeignKey(name = "fk_user_created_by")
@Index(name = "idx_user_created_by")
@JoinColumn(name = "created_by", nullable = false, referencedColumnName = "id")
@NotNull
private User createdBy;
@Column(name = "name", nullable = false)
private String name;
@Column(name = "desc")
private String desc;
@Column(name = "is_system", length = LEN_1)
@Type(type = "org.hibernate.type.YesNoType")
private boolean isSystem = false;
@Column(name = "user_type", nullable = false)
private UserType userType;
@Column(name = "status", nullable = false)
@NotNull
private Status status;
}
我们的计划是使用多列索引而不是单列索引(即根据 (organization, year, isSystem, status, userType, createdBy) 创建索引 user_idx)。假设我有这个索引,我会得到下面列出的查询的优化响应吗?
- select * from user where organization=1 and year=2010;
- select * from user where organization=1 and year=2010 and isSytem=true or false; (即系统用户或应用程序定义的用户)
- select * from user where organization=1 and year=2010 and isSytem=false and userType=Manager(即所有经理)
- select * from user where organization=1 and year=2010 and isSytem=false and userType=Employee(即所有员工)
- select * from user where organization=1 and year=2010 and isSytem=false and userType=Manager and status=ACTIVE(即活跃用户)
select * from user where organization=1 and year=2010 and createdBy='Sam' or 'Joe' [6] 是否需要不同的多列索引,由上述 3 列组成?
由于我们按照我最初的假设创建了一个多列索引,我可以安全地删除模型中当前定义的单个索引(idx_user_org_id、idx_user_year_id、idx_user_created_by)吗?
【问题讨论】:
-
EXPLAIN 是你最好的朋友
标签: mysql database jpa indexing