为什么主键的简单Hibernate findOne()需要这么长时间?

时间:2017-08-14 15:28:40

标签: java sql-server spring hibernate

我将其与an earlier question分开以消除“保存前”选择'混乱。在这个例子中,我试图用主键做一个简单的findOne()。这是针对现有的sqlserver db,具有最新版本的spring boot和spring数据。

我有日志集,所以我可以看到hibernate生成的sql。在此示例中,根据日志记录时间,此查询大约需要4秒。这是使用主键查询。当我运行在数据库工具(如dbvisualizer)中生成的hibernate的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个。

为什么这个选择花了这么长时间?特别是只在我的春季应用程序中,而不是像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> {
}

以及从@Service所在的@Service类中查找Customer的代码:

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=?

1 个答案:

答案 0 :(得分:2)

通过将以下参数添加到数据源中的连接字符串来解决此问题。

sendStringParametersAsUnicode=false 

更多细节: http://www.jochenhebbrecht.be/site/2014-05-01/java/fixing-slow-queries-running-sql-server-using-jpa-hibernate-and-jtds