SQL优化

1.limit返回前多少条

SELECT *
FROM Persons
LIMIT 5

查询Persons表的所有内容,返回前五条。(PageHelper.startPagek开启分页,在SQL语句执行之前使用,相当于在SQL语句后边加上LIMIT)

2.UNION 操作符用于合并两个或多个 SELECT 语句的结果集。

应尽量避免在 where 子句中使用 or 来连接条件,否则将导致引擎放弃使用索引而进行全表扫描

SELECT id FROM work where id=2
UNION ALL
SELECT id FROM work where hobby="人体宴 "

相当于

SELECT id FROM work where id=2 or  hobby="人体宴 "

UNION ALL内容重复不会覆盖。只用UNION,多条数据重复时,只显示一条。

3. in 和 not in

会导致全表扫描,如:

select id from t where num in(1,2,3)    

对于连续的数值,能用 between 就不要用 in 了:

select id from t where num between 1 and 3  

4.不要在 where 子句中的“=”左边进行函数、算术运算或其他表达式运算,否则系统将可能无法正确使用索引。

5.多表查询

在resultmup中加入

<resultMap id="BaseResultMap" type="com.yfy.OpenAccount.pojo.BWsUserLoginAuth">
......
......
......
 <association property="bCustInfo" javaType="com.yfy.OpenAccount.pojo.BCustInfo">
      <id column="CustID" jdbcType="INTEGER" property="custId" />
      <result column="CustBrief" jdbcType="VARCHAR" property="custBrief" />
    </association>
</resultMap>
 <select id="selectOperator" resultMap="BaseResultMap">
   /*根据条件查询*/
    select B_WsUserLoginAuth.* ,B_CustInfo.CustBrief from B_WsUserLoginAuth,B_CustInfo
    <where>
      <if test="custid!=null">
        and  B_WsUserLoginAuth.CustID=#{custid}
      </if>
      <if test="custBrief!=null and custBrief!=''">
        and B_CustInfo.CustBrief  like '%${custBrief}%'
      </if>
      <if test="telNo!=null and telNo!=''">
        and B_WsUserLoginAuth.TelNo = #{telNo}
      </if>
      and B_WsUserLoginAuth.CustID= B_CustInfo.CustID
    </where>
    order by B_WsUserLoginAuth.TelNo
  </select>
  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值