老平台的查詢速度很慢,需要進行優化(...說白了就是優化sql語句),老平台用的strus2+hibernate框架,查詢基本都是使用的hsql。
hql是hibernate query language即hibernate查詢語言
優點:
不需要再編寫繁複的sql 語句,針對實體類及其屬性進行查詢
查詢結果是直接存放在list 中的物件,不需要再次封裝
獨立於資料庫(針對不同資料庫進行查詢,跨資料庫,mysql->oracle),
對不同的資料庫根據hibernate dialect 屬性的配置自動生成不同的sql 語句執行
缺點:
涉及到複雜的sql或者資料量大的情況,不好優化
我的專案裡就是一張單錶的分頁查詢,一共2百多萬條資料。(oracle資料庫)
下面是sql分頁查詢時列印出來的原生sql是這樣的。。
12select3*
4from
5 ( select
6 row_.*,7
rownum rownum_
8from
9 ( select
10 tblmerinfo0_.mer_code as
mer1_19_,
11 tblmerinfo0_.mer_name as
mer2_19_,
12 tblmerinfo0_.mer_short_name as
mer3_19_,
13 tblmerinfo0_.mer_city as
mer4_19_,
14 tblmerinfo0_.mer_nation as
mer5_19_,
15 tblmerinfo0_.mer_status as
mer6_19_,
16 tblmerinfo0_.mer_acq_code as
mer7_19_,
17 tblmerinfo0_.mer_type as
mer8_19_,
18 tblmerinfo0_.mer_warning_amt as
mer9_19_,
19 tblmerinfo0_.mer_group_id as
mer10_19_,
20 tblmerinfo0_.mer_commision_type as
mer11_19_,
21 tblmerinfo0_.mer_commision_value as
mer12_19_,
22 tblmerinfo0_.mer_trans_curr as
mer13_19_,
23 tblmerinfo0_.mer_sett_curr as
mer14_19_,
24 tblmerinfo0_.mer_sett_acct_name as
mer15_19_,
25 tblmerinfo0_.mer_sett_acct as
mer16_19_,
26 tblmerinfo0_.mer_cb_code as
mer17_19_,
27 tblmerinfo0_.mer_cb_name as
mer18_19_,
28 tblmerinfo0_.mer_corp as
mer19_19_,
29 tblmerinfo0_.mer_master as
mer20_19_,
30 tblmerinfo0_.mer_rel_man as
mer21_19_,
31 tblmerinfo0_.mer_rel_tel as
mer22_19_,
32 tblmerinfo0_.mer_fax as
mer23_19_,
33 tblmerinfo0_.mer_email as
mer24_19_,
34 tblmerinfo0_.mer_addr as
mer25_19_,
35 tblmerinfo0_.mer_postcode as
mer26_19_,
36 tblmerinfo0_.mer_password as
mer27_19_,
37 tblmerinfo0_.password_initial_val as
passwor28_19_,
38 tblmerinfo0_.password_expired_date as
passwor29_19_,
39 tblmerinfo0_.password_expired as
passwor30_19_,
40 tblmerinfo0_.batch_no as
batch31_19_,
41 tblmerinfo0_.rec_create_tm as
rec32_19_,
42 tblmerinfo0_.rec_update_tm as
rec33_19_,
43 tblmerinfo0_.mer_open_tm as
mer34_19_,
44 tblmerinfo0_.mer_close_tm as
mer35_19_,
45 tblmerinfo0_.mer_day_limit as
mer36_19_,
46 tblmerinfo0_.mer_single_limit as
mer37_19_,
47 tblmerinfo0_.support_brand_flag as
support38_19_,
48 tblmerinfo0_.route_scheme as
route39_19_,
49 tblmerinfo0_.batch_sett_point as
batch40_19_,
50 tblmerinfo0_.delay_sett_days as
delay41_19_,
51 tblmerinfo0_.dcc_flag as
dcc42_19_,
52 tblmerinfo0_.support_func_flag as
support43_19_,
53 tblmerinfo0_.city_code as
city44_19_,
54 tblmerinfo0_.support_dcc_flag as
support45_19_
55from
56swtonline.tbl_mer_info tblmerinfo0_
57order
by58
tblmerinfo0_.mer_code ) row_ )
59where
60 rownum_ <=
2461
and rownum_ >
12
個人分析了一下慢的原因:裡層的查詢像是查詢了所有,然後外層的查詢在分頁。僅個人觀點。如果有大神看到這裡有不同的看法歡迎指出
下面是我個人的原生sql:
1select
*from2(
3select a.*, rownum r from4(
5select
*from tbl_mer_info where1=
1order
bymer_code
6 ) a where rownum <=
247 ) b where b.r >
12
個人分析:與上面的sql不同就是我的rownum先查詢的<=24,然後外層在》12,這樣會大大提交效率
寫到這裡,不禁就開始比較hibernate與mybatis的各自優點,個人覺得還是mybatis好,夠靈活。雖然hibernate針對各個資料庫設定了"方言",號稱相容性好,可是誰會在開發的過程中替換資料庫呢?
物件導向程式設計之屬性的增刪改查
類屬性的增刪改查 class home def init self,name,age,gender self.name name self.age age self.gender gender country 中國 defeat food self,food print s正在吃 s self.na...
物件導向 封裝增刪改查 資料分頁
class f public function mysqliconnect public function insert data lastvalue substr value,0,1 lastkey substr key,0,1 link this mysqliconnect sql insert...
hibernate物件關係
hibernate學習第二天總結 一對一的關係有 共享主鍵見表方式 唯一外來鍵見表方式 共享主鍵建表方式 即一張表的主鍵,做了主鍵又做了外來鍵 person類裡面有乙個passport類的關聯屬性,名字叫做passport,根據傳遞性持久化,要儲存person物件的時候,同時要儲存 passport...