架构: (伪码)
我有一个名为BaseEntity的bean ...
@Entity
class BaseEntity {
@OneToMany @CascadeType.ALL
List [Property] properties;
//the use angled braces ommited for the stackoverflow editor to show up properly
}
属性是另一个豆......
@Entity
class Property {
@ManyToOne
Category category;
@OneToOne
Value value;
}
Value实际上是一个抽象类,包含Inheritence.SingleTable和子类,如NumericalValue和DateValue等,以及(在抽象Value类中)@ManyToOne BaseType类型。
我正在尝试编写一个选择BaseEntity对象的查询,这些对象具有一个具有某个名称的类别的Property,并选择其中几个,获取具有任何给定属性的对象,并在不具有任何属性的字段中获取null存在。
select entity.id as id, foo as foo, bar as bar
from BaseEntity entity, Value foo, Value bar
where foo in (select p.value from Property p where p in elements(entity.properties) and p.category.name = 'FOO')
or bar in (select p.value from Property p where p in elements(entity.properties) and p.category.name = 'BAR')
此查询已运行。目前在数据库中有一个匹配的BaseEntity,并且我得到了很多次foo的正确结果,它确实包含了它,但是同样的权利在bar字段中反复使用了很多值。
此外,使用数据库运行并保持其他所有人需要五分钟。
思路:
当然我考虑过使用某种不同的东西,但这并没有解决运行所需的极限时间,我只是不太明白发生了什么。
我希望你,我的同行,可以提出更好的查询方法。非常感谢你!
我会评论,但这个代码对于评论框来说太长了...我运行了一个查询,这个不是很长,以至于它挂起,但是更多的连接并且它确实挂起而不是完成...此查询需要MINUTES执行。
我从我的代码中运行了查询,并在hibernate属性中打开了show_sql。
select baseentity0_.entityId as col_0_0_, property2_.value_valueId as col_1_0_, property4_.value_valueId as col_2_0_, property6_.value_valueId as col_3_0_, property8_.value_valueId as col_4_0_, property10_.value_valueId as col_5_0_, value11_.valueId as valueId9_0_, value12_.valueId as valueId9_1_, value13_.valueId as valueId9_2_, value14_.valueId as valueId9_3_, value15_.valueId as valueId9_4_, value11_.type_typeId as type6_9_0_, value11_.numericalValue as numerica3_9_0_, value11_.textValue as textValue9_0_, value11_.dateValue as dateValue9_0_, value11_.value_entityId as value7_9_0_, value11_.DTYPE as DTYPE9_0_, value12_.type_typeId as type6_9_1_, value12_.numericalValue as numerica3_9_1_, value12_.textValue as textValue9_1_, value12_.dateValue as dateValue9_1_, value12_.value_entityId as value7_9_1_, value12_.DTYPE as DTYPE9_1_, value13_.type_typeId as type6_9_2_, value13_.numericalValue as numerica3_9_2_, value13_.textValue as textValue9_2_, value13_.dateValue as dateValue9_2_, value13_.value_entityId as value7_9_2_, value13_.DTYPE as DTYPE9_2_, value14_.type_typeId as type6_9_3_, value14_.numericalValue as numerica3_9_3_, value14_.textValue as textValue9_3_, value14_.dateValue as dateValue9_3_, value14_.value_entityId as value7_9_3_, value14_.DTYPE as DTYPE9_3_, value15_.type_typeId as type6_9_4_, value15_.numericalValue as numerica3_9_4_, value15_.textValue as textValue9_4_, value15_.dateValue as dateValue9_4_, value15_.value_entityId as value7_9_4_, value15_.DTYPE as DTYPE9_4_ from BaseEntity baseentity0_ inner join BaseEntity_Property properties1_ on baseentity0_.entityId=properties1_.BaseEntity_entityId inner join Property property2_ on properties1_.properties_propertyId=property2_.propertyId inner join Value value11_ on property2_.value_valueId=value11_.valueId inner join BaseEntity_Property properties3_ on baseentity0_.entityId=properties3_.BaseEntity_entityId inner join Property property4_ on properties3_.properties_propertyId=property4_.propertyId inner join Value value12_ on property4_.value_valueId=value12_.valueId inner join BaseEntity_Property properties5_ on baseentity0_.entityId=properties5_.BaseEntity_entityId inner join Property property6_ on properties5_.properties_propertyId=property6_.propertyId inner join Value value13_ on property6_.value_valueId=value13_.valueId inner join BaseEntity_Property properties7_ on baseentity0_.entityId=properties7_.BaseEntity_entityId inner join Property property8_ on properties7_.properties_propertyId=property8_.propertyId inner join Value value14_ on property8_.value_valueId=value14_.valueId inner join BaseEntity_Property properties9_ on baseentity0_.entityId=properties9_.BaseEntity_entityId inner join Property property10_ on properties9_.properties_propertyId=property10_.propertyId inner join Value value15_ on property10_.value_valueId=value15_.valueId, Category category16_, Category category17_, Category category18_, Category category19_, Category category20_ where property2_.category_categoryId=category16_.categoryId and property4_.category_categoryId=category17_.categoryId and property6_.category_categoryId=category18_.categoryId and property8_.category_categoryId=category19_.categoryId and property10_.category_categoryId=category20_.categoryId and category16_.name='Sample Name / Strain' and category17_.name='Item #' and category18_.name='THC_Result' and category19_.name='CBD_Result' and category20_.name='CBN_Result'
好吧,我想跟进,并说两个连接语句的效果非常好,但是五个语句都很荒谬。
select
entity.id as entityId,
strain.value as name,
item.value as itemNum,
thc.value as THC,
cbd.value as CBD,
cbn.value as CBN
from BaseEntity as entity
join entity.properties as strain
join entity.properties as item
join entity.properties as thc
join entity.properties as cbd
join entity.properties as cbn
where strain.category.name = 'Sample Name / Strain'
and item.category.name = 'Item #'
and thc.category.name = 'THC_Result'
and cbd.category.name = 'CBD_Result'
and cbn.category.name = 'CBN_Result'
有关更好的方法的任何建议,使用我的愚蠢模式会更快吗?
答案 0 :(得分:1)
性能问题看起来像在您的子选择中。
如何分手呢
select entity from BaseEntity as entity join entity.properties as property where
property.category.name in ( 'FOO','BAR' )
这将为您提供具有FOO或BAR属性的基本实体列表,然后如果您想限制baseEntity.properties的集合,请使用过滤器或重新查询来获取属性。
from properity where property.category.name in ( 'FOO', 'BAR' )
and property.baseEntity.priKey = :priKey
答案 1 :(得分:0)
您是否覆盖关联的默认提取类型?通常这会导致问题。你可以发布Hibernate生成的查询吗?
答案 2 :(得分:0)
绝对是ccclark的答案让我朝着这个方向前进,我的好朋友也帮助了......
select entity.id as entityId, entity.type as entityType, p1.value as Vendor, p2.value as Weight
from BaseEntity as entity
join entity.properties as p1
join entity.properties as p2
where p1.category.name = 'Vendor' and p2.category.name = 'Total Weight'
确实完全符合我的需要。 谢谢你的支持流动社区!
答案 3 :(得分:0)
这个查询对我来说很合理。所以问题是:
hibernate为该HQL生成了什么SQL?
该数据库为该SQL生成了什么查询计划?
在Sybase或MS SQL Server中:“set showplan on”。 在Oracle中使用SQLDeveloper / Toad并选择计划/救护车。
您可能需要在类别表中使用类别名称的索引,在属性表中使用类别ID和实体ID的索引,以及值表中的主键,值id。