2012-07-11 65 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

相关问题