我在 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;
}