老平臺的查詢速度很慢,須要進行優化(...說白了就是優化sql語句),老平臺用的strus2+hibernate框架,查詢基本都是使用的HSQL。sql
HQL是Hibernate Query Language即Hibernate查詢語言數據庫
優勢:mybatis
不須要再編寫繁複的SQL 語句,針對實體類及其屬性進行查詢oracle
查詢結果是直接存放在List 中的對象,不須要再次封裝框架
獨立於數據庫(針對不一樣數據庫進行查詢,跨數據庫,Mysql->Oracle),優化
對不一樣的數據庫根據Hibernate dialect 屬性的配置自動生成不一樣的SQL 語句執行spa
缺點:hibernate
涉及到複雜的sql或者數據量大的狀況,很差優化code
個人項目裏就是一張單表的分頁查詢,一共2百多萬條數據。(oracle數據庫)對象
下面是sql分頁查詢時打印出來的原生sql是這樣的。。
1 2 select 3 * 4 from 5 ( select 6 row_.*, 7 rownum rownum_ 8 from 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_ 55 from 56 SWTONLINE.TBL_MER_INFO tblmerinfo0_ 57 order by 58 tblmerinfo0_.MER_CODE ) row_ ) 59 where 60 rownum_ <= 24 61 and rownum_ > 12
執行時間:63s
我的分析了一下慢的緣由:裏層的查詢像是查詢了全部,而後外層的查詢在分頁。僅我的觀點。若是有大神看到這裏有不一樣的見解歡迎指出
下面是我我的的原生sql:
1 select * from 2 ( 3 select a.*, rownum r from 4 ( 5 select * from tbl_mer_info where 1=1 order by mer_code 6 ) a where rownum <= 24 7 ) b where b.r > 12
執行時間:0.2s
我的分析:與上面的sql不一樣就是個人rownum先查詢的<=24,而後外層在>12,這樣會大大提交效率
寫到這裏,不由就開始比較hibernate與mybatis的各自優勢,我的以爲仍是mybatis好,夠靈活。雖然hibernate針對各個數據庫設定了"方言",號稱兼容性好,但是誰會在開發的過程當中替換數據庫呢?