我在postgresql数据库中有一个查询需要花费太多时间,我想添加一个索引以使其更快,但我不知道我在索引中包含哪些字段,因为其中许多字段属于其他表其中一些是外键。
我正在使用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;
}
答案 0 :(得分:1)
如果您在嵌套全文搜索中遇到性能问题,可能需要深入了解Hibernate Search。
Hibernate搜索(使用Lucene)允许快速有效的全文搜索,也可以搜索嵌套对象的属性。
查看您当前的查询,如果您在两边放置文本占位符,Postgresql甚至可能不会使用索引(%keyword%不使用与关键字%相同的执行计划)