2012-07-11 29 views
0

我在postgresql數據庫中查詢需要太多的時間,我喜歡添加一個索引使其更快,但我不知道哪些字段shouls我包含在索引中,因爲它們中的許多屬於其他表和其中一些是外鍵。哪些字段應該放在索引中?

我使用Hibernate和HQL查詢是這個:從它看起來像一個我下面包括模型自動生成

SELECT i FROM Item i 
LEFT JOIN i.model.kind AS k 
LEFT JOIN i.model.kind.subkind AS s 
WHERE i.file is null " + 
AND i.identifier is not null 
AND i.identifier != '' 
AND i.place is not null 
AND i.place.id = :placeId 
AND (upper(i.serial) LIKE upper(:keyword) 
    OR upper(i.code) LIKE upper(:keyword) 
    OR upper(i.law.law) LIKE upper(:keyword) 
    OR upper(i.model.model) LIKE upper(:keyword) 
    OR upper(k.kind) LIKE upper(:keyword) 
    OR upper(s.subkind) LIKE upper(:keyword) 
    OR upper(i.model.factory.factory) LIKE upper(:keyword) 
) 
ORDER BY i.code, i.id 

數據庫的架構。

哪些字段會在索引中包含?

謝謝。

public class Item { 
    @Id 
    private Long id; 

    private String identifier; 
    private String code; 
    private String serial; 

    @ManyToOne 
    private File file; 

    @ManyToOne 
    private Law law; 

    @ManyToOne 
    private Place place; 

    @ManyToOne 
    private Model model; 
} 

public class File { 
    @Id 
    private Long id; 
    private String file; 
} 

public class Law { 
    @Id 
    private Long id; 
    private String law; 
} 

public class Place { 
    @Id 
    private Long id; 
    private String place; 
} 

public class Model { 
    @Id 
    private Long id; 

    private String model; 

    @ManyToOne 
    private Factory factory; 

    @ManyToOne 
    private Kind kind; 
} 

public class Factory { 
    @Id 
    private Long id; 

    private String factory; 
} 

public class Kind { 
    @Id 
    private Long id; 

    private String kind; 

    @ManyToOne 
    private Subkind subkind; 
} 

public class Subkind { 
    @Id 
    private Long id; 

    private String subkind; 
} 

回答

1

如果您在嵌套全文搜索時遇到性能問題,您可能需要進入Hibernate Search。

Hibernate搜索(使用Lucene)允許快速和有效的全文搜索,也可以嵌套對象的屬性。

縱觀目前的youre查詢時,PostgreSQL可能無法即使你把textplaceholders兩側(%關鍵字%沒有使用相同的執行計劃作爲關鍵字%)

+0

什麼你想要做的是使用一個索引更適合於Lucene,幾乎不可能優化傳統的DBMS索引以處理大量文本搜索和或子句......您將始終需要掃描所有索引的全部內容。 – Mainguy 2012-07-11 14:31:15

相關問題