鉴于以下实体:
@FilterDef(name = "TENANT_FILTER", parameters = {
@ParamDef(name = "tenantId", type = "long")
})
@Filter(name = "TENANT_FILTER", condition = "tenant_id = :tenantId")
public class Pariticipant {
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column(name = "tenant_id")
private Long tenantId;
@Column(name = "firstname")
private String firstName;
@Column(name = "lastname")
private String lastName;
}
我激活过滤器并使用spring数据存储库来查询实体:
Filter filter = entityManager.unwrap(Session.class).enableFilter("TENANT_FILTER");
filter.setParameter(fieldName, 1L);
// query using SpringData Repository
Pariticipant p = repository.getById(10L);
Hibernate生成以下查询:
SELECT participant0_.id AS id2_97_,
participant0_.tenant_id AS tenant_i3_97_,
participant0_.firstname AS firstname_97_,
participant0_.lastname AS lastname_97_
FROM participant participant0_
WHERE participant0_.tenant_id = 1
AND participant0_.id=?
由于id
是主键,如果where子句的顺序是相反的,那么该子句会更有效率。附加过滤器而不是前置于where子句。 e.g:
SELECT participant0_.id AS id2_97_,
participant0_.tenant_id AS tenant_i3_97_,
participant0_.firstname AS firstname_97_,
participant0_.lastname AS lastname_97_
FROM participant participant0_
WHERE participant0_.id=?
AND participant0_.tenant_id = 1
这是一个非常简单的示例,但是查询越复杂,问题就越严重。在我们的情况下,如果过滤器将被附加而不是前置,则每个查询都会受益。
有没有办法影响在where子句中添加过滤器的位置?
答案 0 :(得分:1)
没有办法做到这一点。并且假设条件的顺序对执行速度具有可测量的影响,对许多(大多数?全部?)严重的现代数据库来说都是假的。