2010-09-20 91 views
0

我的對象模型在下面給出,並且希望您輸入關於爲更快的查詢響應(在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))。假設我有這個索引,我會針對下面列出的查詢得到優化的響應。

  1. select * from user where organization = 1 and year = 2010;
  2. select * from user where organization = 1 and year = 2010 and isSytem = true or false; (即系統用戶或應用程序定義的用戶)
  3. select * from user where organization = 1 and year = 2010 and isSytem = false and userType = Manager(即所有管理員)
  4. select * from user where organization = 1 and year = 2010且isSytem = false和userType = Employee(即所有員工)
  5. select * from user where organization = 1 and year = 2010 and isSytem = false and userType = Manager and status = ACTIVE(即活動用戶)
  6. select * from user where organization = 1 and year = 2010 and createdBy ='Sam'or'Joe' [6]是否需要一個不同的多列索引,由上面的3列組成?

  7. 由於我們根據原始假設創建了多列索引,我可以安全地刪除模型中當前定義的各個索引(idx_user_org_id,idx_user_year_id,idx_user_created_by)嗎?

+1

解釋是你最好的朋友在這裏 – Mchl 2010-09-20 08:40:25

回答

2

您應切換列的順序在索引:

(organization, year, isSystem, userType, status, createdBy) 

這使得它可以更好地服務於這兩個查詢:

select * from user where organization=1 and year=2010 and isSystem=false and userType=Manager 
select * from user where organization=1 and year=2010 and isSystem=false and userType=Employee 

確實[6]需要一個不同的多列索引,由以上三列組成?

它並不需要一個新指數 - 它可以使用現有的一個,但在一個不太有效的方式 - 只有前兩列將被使用。儘管爲這個查詢添加一個新的索引看起來不錯。

我可以安全地刪除各個索引

是。您應該刪除未使用的索引,否則它們將佔用磁盤空間並放慢表格修改速度,但不提供任何好處。

+0

好吧,至於[6]有關如果我使用(organization,year,createdBy)創建多列索引,將使用此索引代替以前的索引。我對麼? – user339108 2010-09-20 09:04:05

+0

@ user339108:可能它會使用該索引,是的。您應該運行'EXPLAIN SELECT ...'來確定。 – 2010-09-20 09:24:22

+0

我試着添加一個新的多列索引來解決[6],然後所有查詢似乎都使用新的多列索引來查詢[1-5]中提到的查詢。這似乎不正確,但?讓我知道,我在這裏錯過了什麼? – user339108 2010-09-20 17:56:40

相關問題