我的對象模型在下面給出,並且希望您輸入關於爲更快的查詢響應(在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;
}
我們的計劃是用的,而不是單一的列索引多列索引(即基於創建索引user_idx(組織,一年,isSystem,狀態,用戶類型,createdBy))。假設我有這個索引,我會針對下面列出的查詢得到優化的響應。
- 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且isSytem = false和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)嗎?
解釋是你最好的朋友在這裏 – Mchl 2010-09-20 08:40:25