我遇到以下Hibernate TypedQuery的性能问题:
select generatedAlias0 from MyClass1 as generatedAlias0
where generatedAlias0.class2.uid in (:param0, ..., :paramN)
在实际实施之后,会产生以下查询:
select myclass1_.id, myclass1_.other, ... # 16 fields in total, no special big ones
from myschema.dbo.TblMyClass1 myclass1_ cross join myschema.dbo.TblMyClass2 myclass2_
where myclass1_.myclass2Id=myclass2.Id and (myclass2_.uid in('value1', ... 'valueN'))
其中N每次代表384项。
当我在Toad中执行此查询时,它只需要大约150毫秒,但是从代码执行它需要将近一分钟!
类映射
@Entity
@Table(name = "TblMyClass1", catalog = "myschema", schema = "dbo")
public class MyClass1 implements Serializable {
private static final long serialVersionUID = 8208493383744288872L;
@Id
@Column(name = "Id")
@GeneratedValue(strategy = GenerationType.AUTO)
protected Integer id;
@NotNull
@ManyToOne
@JoinColumn(name = "myclass2Id", referencedColumnName = "Id", nullable = false)
private MyClass2 class2 = null;
@NotNull
@ManyToOne
@JoinColumn(name = "myclass3Id", referencedColumnName = "Id", nullable = false)
private MyClass3 class3 = null;
@NotNull
@ManyToOne
@JoinColumn(name = "myClass4Id", referencedColumnName = "Id", nullable = false)
private MyClass4 myClass4;
@ManyToOne
@JoinColumn(name = "myClass5Uid", referencedColumnName = "UID", nullable = true)
private MyClass5 resultType;
@Column(name = "string2", nullable = true)
private String string2;
// other column fields and getters and setters ...
}
@Entity
@Table(name = "TblMyClass2", schema = "myschema", catalog = "dbo")
public final class MyCLass2 implements Serializable {
private static final long serialVersionUID = 4660579327140751989L;
@Id
@Column(name = "Id")
@GeneratedValue(strategy = GenerationType.AUTO)
protected Integer id;
@Column(name = "uid", nullable = false, updatable = false, unique = true)
private String uid;
// ... other columns fields and getters and setters
}
我只在慢速查询中提供了两个类的映射,因为hibernate的后续查询填充了所有其他链接实体,都表现得非常好。
使用Java VisualVm我发现以下方法占用了99%的时间:
com.microsoft.sqlserver.jdbc.TDSChannel.read()
我正在运行 SQL Server 2008 并使用 sqljdbc4-2.0 和 hibernate4.2.1 与 spring3.2.9 (用于交易管理)
非常感谢任何帮助!