開源項目地址: https://github.com/chenrenfei/sagacity-sqltoy
感受sqltoy之美: https://chenrenfei.github.io/sqltoy/#/
更新內容:
1、緩存翻譯對應的緩存更新機制增加增量更新
2、查詢結果計算增加環比計算,請參見sqltoy-showcase下的QueryCaseTest類
sqltoy的代表性特性展示:
1、最優雅的sql編寫模式
select *
from sqltoy_device_order_info t
<where>
<if test="orderId!=null">
and t.ORDER_ID=#{orderId}
</if>
<if test="authedOrganIds!=null">
and t.ORGAN_ID in
<foreach collection="authedOrganIds" item="order_id" separator="," open="(" close=")">
#{order_id}
</foreach>
</if>
<if test="staffIds!=null">
and t.STAFF_ID in
<foreach collection="staffIds" item="staff_id" separator="," open="(" close=")">
#{staff_id}
</foreach>
</if>
<if test="beginDate!=null">
and t.TRANS_DATE>=#{beginDate}
</if>
<if test="endDate!=null">
and t.TRANS_DATE<#{endDate}
</if>
</where>
select *
from sqltoy_device_order_info t
where #[t.ORDER_ID=:orderId]
#[and t.ORGAN_ID in (:authedOrganIds)]
#[and t.STAFF_ID in (:staffIds)]
#[and t.TRANS_DATE>=:beginDate]
#[and t.TRANS_DATE<:endDate]
2、最具特色的緩存翻譯和緩存條件檢索,讓複雜查詢最接近單表查詢
訂單號 | 客戶ID | 商品ID | 下單日期 | 商品數量 | 商品價格 | 訂單金額 | 訂單狀態 | 業務員ID | 部門 |
---|---|---|---|---|---|---|---|---|---|
S0001 | C10001 | 101 | 2020-03-10 | 10 | 3000 | 30000 | 02 | 1001 | N002 |
要求查詢日期在2020年1月10日至3月20日、客戶名稱中含<<星雲科技>>字元的全部訂單信息,要求顯示商品名稱、客戶名稱、業務員姓名、部門名稱、訂單狀態中文
往常的做法:硬功夫硬碰硬系列
關聯客戶表做like
關聯商品表查詢品名
關聯員工信息表顯示員工名字
關聯機構表顯示機構名稱
關聯數據字典翻譯狀態
<sql id="order_showcase">
<!-- 通過緩存對最終結果代碼進行翻譯,顯示名稱 -->
<translate cache="organIdName" columns="ORGAN_NAME" />
<translate cache="staffIdName" columns="STAFF_NAME" />
<translate cache="goodsIdName" columns="GOODS_NAME" />
<translate cache="customIdName" columns="CUSTOM_NAME" />
<translate cache="dictKeyName" cache-type="ORDER_STATUS" columns="STATUS_NAME" />
<filters>
<!-- 將查詢參數customName通過緩存進行類似like檢索獲取匹配的customId數組作為查詢條件 -->
<cache-arg cache-name="customIdName" param="customName" alias-name="customIds" />
</filters>
<value>
<![CDATA[
select
ORDER_ID ,
TOTAL_QUANTITY,
TOTAL_AMT,
ORGAN_ID ,
ORGAN_ID ORGAN_NAME,-- 緩存翻譯
STAFF_ID ,
STAFF_ID STAFF_NAME,
SIGN_TIME,
CUSTOM_ID,
CUSTOM_ID CUSTOM_NAME,
GOODS_ID ,
GOODS_ID GOODS_NAME,
STATUS,
STATUS STATUS_NAME
from od_order_info t1
where #[SIGN_TIME>=:beginTime]
#[and SIGN_TIME <=:endTime]
-- 這裡就是緩存條件檢索
#[and CUSTOM_ID in (:customIds)]
]]>
</value>
</sql>
3、最高層級的分頁查詢優化:@fast 實現先分頁后關聯,page-optimize 將分頁2次查詢變成1.4次查詢。
<!-- 快速分頁和分頁優化演示 --> <sql id="sqltoy_fastPage"> <!-- 分頁優化器,通過緩存實現查詢條件一致的情況下在一定時間周期內緩存總記錄數量,從而無需每次查詢總記錄數量 --> <!-- alive-max:最大存放多少個不同查詢條件的總記錄量; alive-seconds:查詢條件記錄量存活時長(比如120秒,超過閥值則重新查詢) --> <page-optimize alive-max="100" alive-seconds="120" /> <value> <![CDATA[ select t1.*,t2.ORGAN_NAME -- @fast() 實現先分頁取10條(具體數量由pageSize確定),然後再關聯 from @fast(select t.* from sqltoy_staff_info t where t.STATUS=1 #[and t.STAFF_NAME like :staffName] order by t.ENTRY_DATE desc ) t1 left join sqltoy_organ_info t2 on t1.organ_id=t2.ORGAN_ID ]]> </value> <!-- 這裡為極特殊情況下提供了自定義count-sql來實現極致性能優化 --> <!-- <count-sql></count-sql> --> </sql>
[admin
]