【发布时间】:2018-01-22 11:55:26
【问题描述】:
我将其与an earlier question 分开,以消除“保存前选择”的混淆。在这个例子中,我试图通过主键做一个简单的 findOne() 。这是针对现有的 sqlserver db,带有最新版本的 spring boot 和 spring data。
我已经设置了日志记录,所以我可以看到 hibernate 生成的 sql。在这个例子中,根据记录时间,这个查询大约需要 4 秒。这是使用主键查询。当我运行hibernate在dbvisualizer之类的数据库工具中生成的sql时,它会在亚秒内返回,正如我所期望的那样。
我将hibernate打包日志提高到TRACE级别,尝试查看延迟在哪里,发现4秒延迟前后如下:
2017-08-14 09:51:35.345 DEBUG 7532 --- [nio-8085-exec-1] org.hibernate.SQL : select customer0_.customer_id as customer1_4_0_, customer0_.first_name as first_na2_4_0_, customer0_.last_name as last_nam3_4_0_ from xbr_customer_tab customer0_ where customer0_.customer_id=?
Hibernate: select customer0_.customer_id as customer1_4_0_, customer0_.first_name as first_na2_4_0_, customer0_.last_name as last_nam3_4_0_ from xbr_customer_tab customer0_ where customer0_.customer_id=?
2017-08-14 09:51:35.470 TRACE 7532 --- [nio-8085-exec-1] o.h.r.j.i.ResourceRegistryStandardImpl : Registering statement [org.apache.tomcat.jdbc.pool.StatementFacade$StatementProxy[Proxy=25287222; Query=select customer0_.customer_id as customer1_4_0_, customer0_.first_name as first_na2_4_0_, customer0_.last_name as last_nam3_4_0_ from xbr_customer_tab customer0_ where customer0_.customer_id=?; Delegate=SQLServerPreparedStatement:6]]
2017-08-14 09:51:35.471 TRACE 7532 --- [nio-8085-exec-1] o.h.e.jdbc.internal.JdbcCoordinatorImpl : Registering last query statement [org.apache.tomcat.jdbc.pool.StatementFacade$StatementProxy[Proxy=25287222; Query=select customer0_.customer_id as customer1_4_0_, customer0_.first_name as first_na2_4_0_, customer0_.last_name as last_nam3_4_0_ from xbr_customer_tab customer0_ where customer0_.customer_id=?; Delegate=SQLServerPreparedStatement:6]]
2017-08-14 09:51:35.479 TRACE 7532 --- [nio-8085-exec-1] o.h.type.descriptor.sql.BasicBinder : binding parameter [1] as [VARCHAR] - [40666316]
2017-08-14 09:51:35.488 TRACE 7532 --- [nio-8085-exec-1] o.h.l.p.e.i.AbstractLoadPlanBasedLoader : Bound [2] parameters total
2017-08-14 09:51:39.426 TRACE 7532 --- [nio-8085-exec-1] o.h.r.j.i.ResourceRegistryStandardImpl : Registering result set [SQLServerResultSet:6]
2017-08-14 09:51:39.434 TRACE 7532 --- [nio-8085-exec-1] o.h.l.p.e.p.i.ResultSetProcessorImpl : Processing result set
2017-08-14 09:51:39.434 DEBUG 7532 --- [nio-8085-exec-1] o.h.l.p.e.p.i.ResultSetProcessorImpl : Starting ResultSet row #0
2017-08-14 09:51:39.436 DEBUG 7532 --- [nio-8085-exec-1] l.p.e.p.i.EntityReferenceInitializerImpl : On call to EntityIdentifierReaderImpl#resolve, EntityKey was already known; should only happen on root returns with an optional identifier specified
2017-08-14 09:51:39.436 TRACE 7532 --- [nio-8085-exec-1] l.p.e.p.i.EntityReferenceInitializerImpl : hydrating entity state
我也想知道为什么它说绑定了 2 个参数,而 sql 中只有 1 个。
任何想法为什么这个选择需要这么长时间?尤其是仅在我的 spring 应用程序中,而不是在 dbvisualizer 之类的其他客户端中?
这是实体:
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "my_customer_table")
public class Customer {
@Id
private String customer_id;
private String first_name;
private String last_name;
protected Customer() {}
public Customer(String firstName, String lastName) {
this.first_name = firstName;
this.last_name = lastName;
}
}
这是 CustomerRepository
import com.....Customer;
import org.springframework.data.repository.CrudRepository;
public interface CustomerRepository extends CrudRepository<Customer, String> {
}
以及从 CustomerRepository 所在的 @Autowired 所在的 @Service 类查找客户的代码:
Customer customer = customerRepository.findOne(customerId);
这里是生成的sql:
select customer0_.customer_id as customer1_4_0_, customer0_.first_name as first_na2_4_0_, customer0_.last_name as last_nam3_4_0_ from my_customer_table customer0_ where customer0_.customer_id=?
【问题讨论】:
-
是所有的日志记录吗?
-
..为什么绑定为
VARCHAR。哦,您的主键是 varchar...这是一个有趣的选择。 -
主键作为 varchar...是的,就像我说的,预先存在的 db...不确定是谁或何时做的;我们正在针对这个旧数据库编写一个新应用程序
-
@AmerQarabsa 还有很多日志记录;你要多少钱?
-
@user26270,从应用程序代码运行查询时是否会发生阻塞?尝试使用 SQL Server 扩展事件跟踪来捕获 rpc_starting 和 rpc_completed 事件。这将验证服务器执行时间。逻辑读取数应为单位数,由 PK 索引选择单行。
标签: java sql-server spring hibernate