mhub-sqlcollect 使用說明
此模塊用于收集項(xiàng)目中可能影響效率的分頁查詢SQL,主要用于項(xiàng)目開發(fā)階段的SQL優(yōu)化
背景說明
MHUB框架中,我們使用了PageHelper 作為分頁插件,它會(huì)根據(jù)你使用的數(shù)據(jù)庫來將你的**ByPage的sql改裝成一個(gè)分頁查詢的sql,并順帶生成一個(gè)查詢總數(shù)的sql,這條sql的生成使用了SQL解析工具SQLParser,當(dāng)解析失敗時(shí)(不規(guī)范的sql或自定義函數(shù)等,都可能導(dǎo)致解析失?。?,查詢總數(shù)的sql會(huì)直接使用select count(0) from ( 原sql ),效率可想而知。
怎么解決呢?MHUB里提供了@MhubCount注解(com.woyi.annotation.MhubCount),該注解在DAO層使用,使用參照如下:
/** 分頁查詢sql */
@MhubCount(paramTypes = {ChatRecord.class, MPage.class}, queryCountMethodName = "getChatRecordByPageForCount")
Page<ChatRecord> getChatRecordByPage(@Param("chatRecord")ChatRecord chatRecord, MPage page);
/** 優(yōu)化過的count查詢sql */
Long getChatRecordByPageForCount(@Param("chatRecord")ChatRecord chatRecord, MPage page);
mapper里對(duì)應(yīng)的getChatRecordByPageForCount寫優(yōu)化過的count查詢sql
SQL收集的配置方法
1. 運(yùn)行sys_sql_collection.sql(MySQL, Oracle),創(chuàng)建表
2. 添加依賴 mhub-sqlcollect.jar
compile "com.woyitech:mhub-sqlcollect:2.0"
3. applicationContext-public.xml中mybatis配置添加攔截器
<bean id="queryPageInterceptor" class="com.woyi.mhub.expand.sqlcollection.interceptor.QueryPageInterceptor" />
** 注意其位置必須在pageHelper后 **
** 樣例:**
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
<property name="mapperLocations" value="classpath*:com/woyi/**/**/mappers/${jdbc.databasetype}/*Mapper.xml"></property>
<property name="plugins">
<array>
<bean id="pageHelper" class="com.github.pagehelper.PageHelper">
<property name="properties">
<!--注意 方言全部使用小寫字母 -->
<!--mysql, mariadb, sqlite, oracle, hsqldb, postgresql, sqlserver, db2, informix -->
<value>
dialect=${jdbc.databasetype}
</value>
</property>
</bean>
<!-- 必須在PageHelper后 -->
<bean id="queryPageInterceptor" class="com.woyi.mhub.expand.sqlcollection.interceptor.QueryPageInterceptor" />
</array>
</property>
</bean>
4. 添加菜單
/web/sqlCollection/list/web/sqlCollection/list
初始化菜單
INSERT INTO `sys_menu`
(menuid`,`menustatus`,`menuname`,`menucode`,`menuparentcode`,`menutype`,`menuurl`,`menulevl`,`menusort`,`menumemo`,`deleteable`,`menupositionname`,`opentype`,`grantable`,`creator`)
VALUES
('e570d738-77f9-11e7-8373-00155d023b00','1','SQL收集器','mhub_expand_sqlcollect','mhub_baseapp','0','/web/sqlCollection/list','2','6','','0','','0','0','eb9e2211-f652-11e5-af4e-50465d555c06' );