sql优化,模糊查询

来源:5-5 千万记录,如何快速商品分页?

清晨等我可好

2021-09-30

老师您好就是项目中的一个列表展示,涉及到了四到五个表的连接查询,而且还会需要对一些字段进行模糊查询。
我想要请问下像这种情况可以如何优化?

写回答

1回答

清晨等我可好

提问者

2021-09-30

SELECT
t1.*,
t2.app_name,
t3.customer_name,
t3.customer_type,
t3.customer_id AS customerNo,
t4.CONTRACT_NAME,
t4.CONTRACT_ID AS contractNo,
t4.CONTRACT_SIGN
FROM
view_interface AS t1
LEFT JOIN gw_app t2 ON t1.app_id = t2.app_id
LEFT JOIN customer_info AS t3 ON t1.customer_id=t3.id
LEFT JOIN CONTRACT_DETAILS AS t4 ON t1.contract_id=t4.ID
where 1=1
<if test="appName!=null and appName != ''">
    and t2.app_name like CONCAT('%',#{appName},'%')
</if>
<if test="associateInterface!=null and associateInterface != ''">
    and t1.associate_interface like CONCAT('%',#{associateInterface},'%')
</if>
<if test="customerName!=null and customerName != ''">
    and t3.customer_name like CONCAT('%',#{customerName},'%')
</if>
<if test="customerType!=null and customerType != ''">
    and t3.customer_type=#{customerType}
</if>
<if test="productName!=null and productName != ''">
    and t1.product_name like CONCAT('%',#{productName},'%')
</if>
<if test="contractNo!=null and contractNo != ''">
    and t4.CONTRACT_ID like CONCAT('%',#{contractNo},'%')
</if>
<if test="contractName!=null and contractName != ''">
    and t4.CONTRACT_NAME like CONCAT('%',#{contractName},'%')
</if>
<if test="startTime != null">
    and t1.start_time <![CDATA[ >= ]]> #{startTime}
</if>
<if test="endTime != null">
    and t1.end_time <![CDATA[ <= ]]> #{endTime}
</if>

比如说这个sql查询

0
1
神思者
建议你使用模糊查询的时候先用其他条件确定大致搜索范围,这样模糊查询筛选的记录才会少一些
2021-09-30
共1条回复

阿里新零售数据库设计与实战 (升级版)

解锁“新零售”业务让数据库实战能力再上一层楼

2111 学习 · 701 问题

查看课程