【问题标题】:which fields should go in an index?哪些字段应该进入索引?
【发布时间】:2012-07-10 21:56:12
【问题描述】:

我在 postgresql 数据库中进行了一个查询,这需要太多时间,我想添加一个索引以使其更快,但我不知道我应该在索引中包含哪些字段,因为其中许多字段属于其他表其中一些是外键。

我正在使用休眠,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;
}

【问题讨论】:

    标签: database hibernate postgresql indexing hql


    【解决方案1】:

    如果您在嵌套全文搜索时遇到性能问题,您可能需要深入研究 Hibernate Search。

    Hibernate 搜索(使用 Lucene)允许快速高效的全文搜索,也可以对嵌套对象的属性进行搜索。

    查看您当前的查询,如果您在两侧放置文本占位符,Postgresql 甚至可能不使用索引(%keyword% 不使用与关键字% 相同的执行计划)

    【讨论】:

    • 您尝试做的事情更适合 Lucene,几乎不可能优化传统的 DBMS 索引来处理一堆文本搜索和或子句......你总是需要扫描所有索引的全部内容。
    猜你喜欢
    • 2013-07-28
    • 1970-01-01
    • 1970-01-01
    • 2021-08-31
    • 2010-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多