2017-08-14 87 views
4

我把这个从an earlier question分开,以消除'选择之前保存'的困惑。在这个例子中,我试图通过主键做一个简单的findOne()。这是针对现有的sqlserver数据库,以及最新版本的spring引导和spring数据。为什么一个简单的Hibernate findOne()通过主键占用这么久?

我有日志记录集,所以我可以看到休眠生成的SQL。在这个例子中,根据记录时间,这个查询大约需要4秒。这是使用主键查询的。当我运行hibernate在像dbvisualizer这样的db工具中生成的sql时,它会在我所期望的秒内返回。

我增加了休眠包装记录到跟踪级别,要尽量看看那里的延迟,并发现了下面,前后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> { 
} 

和代码查找一个客户,从@服务类,其中CustomerRepository中@Autowired

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

是,所有的记录? –

+0

..为什么它绑定为'VARCHAR'。哦,你的主键是一个varchar ...这是一个有趣的选择。 – Kayaman

+0

主键为varchar ...就像我说的,现有的分贝...不知道是谁做的或什么时候;我们正在写一个新的应用程序对这个旧的分区 – user26270

回答

相关问题