我把這個從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=?
是,所有的記錄? –
..爲什麼它綁定爲'VARCHAR'。哦,你的主鍵是一個varchar ...這是一個有趣的選擇。 – Kayaman
主鍵爲varchar ...就像我說的,現有的分貝...不知道是誰做的或什麼時候;我們正在寫一個新的應用程序對這個舊的分區 – user26270