gpt4 book ai didi

java - Hibernate 性能 - 对控制台日志中的许多选择查询

转载 作者:太空宇宙 更新时间:2023-11-04 12:56:09 24 4
gpt4 key购买 nike

我认为我的 hibernate 性能有问题。也许我做错了什么。我正在使用 Spring 4 和 Hibernate 4。我在数据库中有两个表,Driver 和 Fleet 连接了 ManyToMany。表DRIVER_FLEET将它们连接起来,由driverID和fleetID组成。

这是来自 Driver 类的示例,它代表了我的连接的样子。

@ManyToMany(fetch = FetchType.LAZY, cascade=CascadeType.PERSIST)  
@JoinTable(name="DRIVER_FLEET",
joinColumns= { @JoinColumn(name="driverID")},
inverseJoinColumns={@JoinColumn(name="fleetID")})
private List<Fleet> fleet = new ArrayList<Fleet>();
public List<Fleet> getFleet() {
return fleet;
}
public void setFleet(List<Fleet> fleet) {
this.fleet = fleet;
}

这是舰队级:

@ManyToMany(mappedBy = "Driver")
public Set<Driver> getDriver() {
return getDriver();
}

现在,当我使用 hibernate save() 将 Driver 和 Fleet 保存到数据库中时,我在控制台中得到以下结果:

Hibernate: select driver0_.driverID as driverID1_2_, driver0_.commision as commisio2_2_, driver0_.dateOfBirth as dateOfBi3_2_, driver0_.driverDriveRateAmount as driverDr4_2_, driver0_.driverDriveRateCurrency as driverDr5_2_, driver0_.driverNumber as driverNu6_2_, driver0_.driverPayRateAmount as driverPa7_2_, driver0_.driverPayRateCurrency as driverPa8_2_, driver0_.driverPerOrderAmount as driverPe9_2_, driver0_.driverPerOrderCurrency as driverP10_2_, driver0_.firstName as firstNa11_2_, driver0_.gender as gender12_2_, driver0_.lastName as lastNam13_2_, driver0_.middleInitials as middleI14_2_ from Driver driver0_
Hibernate: select fleet0_.driverID as driverID1_2_0_, fleet0_.fleetID as fleetID2_0_0_, fleet1_.fleetID as fleetID1_3_1_, fleet1_.fleetName as fleetNam2_3_1_ from DRIVER_FLEET fleet0_ inner join Fleet fleet1_ on fleet0_.fleetID=fleet1_.fleetID where fleet0_.driverID=?
Hibernate: select fleet0_.driverID as driverID1_2_0_, fleet0_.fleetID as fleetID2_0_0_, fleet1_.fleetID as fleetID1_3_1_, fleet1_.fleetName as fleetNam2_3_1_ from DRIVER_FLEET fleet0_ inner join Fleet fleet1_ on fleet0_.fleetID=fleet1_.fleetID where fleet0_.driverID=?
Hibernate: select fleet0_.driverID as driverID1_2_0_, fleet0_.fleetID as fleetID2_0_0_, fleet1_.fleetID as fleetID1_3_1_, fleet1_.fleetName as fleetNam2_3_1_ from DRIVER_FLEET fleet0_ inner join Fleet fleet1_ on fleet0_.fleetID=fleet1_.fleetID where fleet0_.driverID=?
Hibernate: select fleet0_.driverID as driverID1_2_0_, fleet0_.fleetID as fleetID2_0_0_, fleet1_.fleetID as fleetID1_3_1_, fleet1_.fleetName as fleetNam2_3_1_ from DRIVER_FLEET fleet0_ inner join Fleet fleet1_ on fleet0_.fleetID=fleet1_.fleetID where fleet0_.driverID=?
Hibernate: select fleet0_.driverID as driverID1_2_0_, fleet0_.fleetID as fleetID2_0_0_, fleet1_.fleetID as fleetID1_3_1_, fleet1_.fleetName as fleetNam2_3_1_ from DRIVER_FLEET fleet0_ inner join Fleet fleet1_ on fleet0_.fleetID=fleet1_.fleetID where fleet0_.driverID=?
Hibernate: select driver0_.driverID as driverID1_2_, driver0_.commision as commisio2_2_, driver0_.dateOfBirth as dateOfBi3_2_, driver0_.driverDriveRateAmount as driverDr4_2_, driver0_.driverDriveRateCurrency as driverDr5_2_, driver0_.driverNumber as driverNu6_2_, driver0_.driverPayRateAmount as driverPa7_2_, driver0_.driverPayRateCurrency as driverPa8_2_, driver0_.driverPerOrderAmount as driverPe9_2_, driver0_.driverPerOrderCurrency as driverP10_2_, driver0_.firstName as firstNa11_2_, driver0_.gender as gender12_2_, driver0_.lastName as lastNam13_2_, driver0_.middleInitials as middleI14_2_ from Driver driver0_
Hibernate: select fleet0_.driverID as driverID1_2_0_, fleet0_.fleetID as fleetID2_0_0_, fleet1_.fleetID as fleetID1_3_1_, fleet1_.fleetName as fleetNam2_3_1_ from DRIVER_FLEET fleet0_ inner join Fleet fleet1_ on fleet0_.fleetID=fleet1_.fleetID where fleet0_.driverID=?
Hibernate: select fleet0_.driverID as driverID1_2_0_, fleet0_.fleetID as fleetID2_0_0_, fleet1_.fleetID as fleetID1_3_1_, fleet1_.fleetName as fleetNam2_3_1_ from DRIVER_FLEET fleet0_ inner join Fleet fleet1_ on fleet0_.fleetID=fleet1_.fleetID where fleet0_.driverID=?
Hibernate: select fleet0_.driverID as driverID1_2_0_, fleet0_.fleetID as fleetID2_0_0_, fleet1_.fleetID as fleetID1_3_1_, fleet1_.fleetName as fleetNam2_3_1_ from DRIVER_FLEET fleet0_ inner join Fleet fleet1_ on fleet0_.fleetID=fleet1_.fleetID where fleet0_.driverID=?
Hibernate: select fleet0_.driverID as driverID1_2_0_, fleet0_.fleetID as fleetID2_0_0_, fleet1_.fleetID as fleetID1_3_1_, fleet1_.fleetName as fleetNam2_3_1_ from DRIVER_FLEET fleet0_ inner join Fleet fleet1_ on fleet0_.fleetID=fleet1_.fleetID where fleet0_.driverID=?
Hibernate: select fleet0_.driverID as driverID1_2_0_, fleet0_.fleetID as fleetID2_0_0_, fleet1_.fleetID as fleetID1_3_1_, fleet1_.fleetName as fleetNam2_3_1_ from DRIVER_FLEET fleet0_ inner join Fleet fleet1_ on fleet0_.fleetID=fleet1_.fleetID where fleet0_.driverID=?
Hibernate: insert into Driver (commision, dateOfBirth, driverDriveRateAmount, driverDriveRateCurrency, driverNumber, driverPayRateAmount, driverPayRateCurrency, driverPerOrderAmount, driverPerOrderCurrency, firstName, gender, lastName, middleInitials) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
Hibernate: insert into DRIVER_LICENSE (expirationDate, stateIssued, driverID, licenseID) values (?, ?, ?, ?)
Hibernate: insert into DRIVER_FLEET (driverID, fleetID) values (?, ?)

从这个控制台日志中,您可以看到我有两行这样的行

Hibernate: select fleet0_.driverID as driverID1_2_0_, fleet0_.fleetID as fleetID2_0_0_, fleet1_.fleetID as fleetID1_3_1_, fleet1_.fleetName as fleetNam2_3_1_ from DRIVER_FLEET fleet0_ inner join Fleet fleet1_ on fleet0_.fleetID=fleet1_.fleetID where fleet0_.driverID=?

问题是,如果表 DRIVER_FLEET 中有更多行,那么这些选择查询就有更多行。我是否有可能在 ManyToMany 连接中犯了错误?我的表现可以更好吗?

最佳答案

当我们等待 @Milan 更新他的问题(因为 @JBNizet 指出了一些严重问题)时,让我们尝试远景回答:

仅仅因为您在日志中发现多个重复行,并不意味着该事件发生了多次。在某些情况下,您的日志记录框架可能会为单个事件记录多条消息。

例如,当您在类包的不同深度设置多个记录器时,如下所示:

<configuration>

<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%msg%n</pattern>
</encoder>
</appender>

<logger name="chapters">
<appender-ref ref="STDOUT" />
</logger>

<logger name="chapters.configuration">
<appender-ref ref="STDOUT" />
</logger>

<logger name="chapters.configuration.Foo">
<appender-ref ref="STDOUT" />
</logger>

<root level="debug">
<appender-ref ref="STDOUT" />
</root>
</configuration>

您将在控制台中通过 Logback 获得事件 4x由于默认情况下日志记录是累积的,请参阅 additivity="false" 进行修复。

关于java - Hibernate 性能 - 对控制台日志中的许多选择查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35372422/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com