-- 创建索引CREATEINDEX sys_orders_log_orderstype_requestid ON sys_orders_log ( orders_type, request_id );-- 用不上的索引CREATEINDEX sys_orders_ordersstate_lockstate_orderstype_goodstype ON sys_orders ( orders_state, lock_state, orders_type, goods_type );CREATEINDEX sys_orders_lockstate ON sys_orders ( lock_state );CREATEINDEX sys_orders_lockstate_ordersstate ON sys_orders ( lock_state , orders_state );-- 分析索引 sys_orders_logEXPLAINselect*from sys_orders_log
where orders_type ='0'and request_id ='60e71f48000000000100b25e';-- 分析索引 sys_ordersEXPLAINselect*from sys_orders
where(orders_state ='0'or orders_state ='1')and lock_state ='0'and orders_type ='0'and goods_type ='0';-- 分析索引 联合查询 EXPLAINselect a.*from sys_orders as a
leftjoin(select*from sys_orders_log
where orders_type ='0'and request_id ='60e71f48000000000100b25e')as b
on a.note_id = b.note_id
where a.orders_state ='0'and a.lock_state ='0'and a.orders_type ='0'and a.goods_type ='0'and b.note_id isnull;-- 优化后的查询select
a.*where a.lock_state ='0'and a.orders_type ='0'and a.goods_type ='0'and a.orders_state in('0','1')and a.note_id notin(select
note_id
from
sys_orders_log
where
request_id ='60e7235b000000000101e9e4'and orders_type ='0')-- 优化前的查询select a.*from sys_orders as a
leftjoin(select note_id
from sys_orders_log
where request_id ='60e7235b000000000101e9e4'and orders_type ='0')as b
on a.note_id = b.note_id
where a.orders_state in('0','1')and a.lock_state ='0'and a.orders_type ='0'and a.goods_type ='0'and b.note_id isnull;-- 使用EXPLAIN查询之后确实又发现有变快
2.MyBatis
<!--在用--><selectid="selectSysOrdersTaskApiPull"parameterType="com.alibaba.fastjson.JSONObject"resultMap="SysOrdersResult">
select a.*
from sys_orders a
where a.lock_state = #{lockState}
and a.orders_type = #{ordersType}
and a.goods_type = #{goodsType}
and a.orders_state in ('0', '1')
and a.note_id not in (
select note_id
from sys_orders_log
where request_id = #{requestId}
and orders_type = #{ordersType})
order by rand()
limit 1
</select><!--过时--><selectid="selectSysOrdersTaskApiPull"parameterType="com.alibaba.fastjson.JSONObject"resultMap="SysOrdersResult">
select a.*
from sys_orders as a
left join(
select note_id
from sys_orders_log
where request_id = #{requestId}
and orders_type = #{ordersType}) as b
on a.note_id = b.note_id
where (a.orders_state = '0' or a.orders_state = '1')
and a.lock_state = #{lockState}
and a.orders_type = #{ordersType}
and a.goods_type = #{goodsType}
and b.note_id is null
order by rand()
limit 1
</select><!-- 单个测试 sys_orders --><selectid="selectSysOrdersTaskApiPull"parameterType="com.alibaba.fastjson.JSONObject"resultMap="SysOrdersResult">
select *
from sys_orders
where orders_state in ('0', '1')
and lock_state = #{lockState}
and orders_type = #{ordersType}
and goods_type = #{goodsType}
order by rand()
limit 1
</select><!-- 单个测试 sys_orders_log --><selectid="selectSysOrdersTaskApiPull"parameterType="com.alibaba.fastjson.JSONObject"resultMap="SysOrdersResult">
select note_id
from sys_orders_log
where request_id = #{requestId}
and orders_type = #{ordersType}
</select>