Mysql中使用where 1=1有什么问题吗

昨天偶然看见一篇文章,提到说如果在mysql查询语句中,使用where 1=1会有性能问题??

这着实把我吸引了,因为我项目中就有不少同事,包括我自己也有这样写的。为了不给其他人挖坑,赶紧学习一下,这样写sql到底有没有性能问题?

where 1=1的使用场景

我们来看下,where 1=1实际是怎么使用的, 先来看一段SQL

<select id="selectCmsCourseList" parameterType="java.util.Map" resultMap="CourseMap">
    SELECT
     a.id,
     a.category_id,
     a.model_id,
     b.*
    FROM
     cms_content a
    LEFT JOIN cms_course b ON a.id = b.id
    WHERE 1=1
    <if test="courseName != null and courseName != ''">
       AND b.course_name like concat(concat("%",#{courseName}),"%")
    </if>
    <if test="disabled != null">
       AND a.disabled = #{disabled}
    </if>
    <if test="categoryId != null">
       AND a.category_id = #{categoryId}
    </if>
</select>

如果用过mybatis的童鞋,看到这段代码应该很熟悉了吧,这里使用where 1=1的作用,就是为了方便后面的条件,能通过if判断,使用AND连接起来,这样即使后面if全部是false,没有参数,这个sql也不会报错。

where 1=1的替换方案

其实上面的这个写法,如果用mybatis,完全可以用where标签来替换,如:

<select id="selectCmsCourseList" parameterType="java.util.Map" resultMap="CourseMap">
    SELECT
     a.id,
     a.category_id,
     a.model_id,
     b.*
    FROM
     cms_content a
    LEFT JOIN cms_course b ON a.id = b.id
    <where>
        <if test="courseName != null and courseName != ''">
           AND b.course_name like concat(concat("%",#{courseName}),"%")
        </if>
        <if test="disabled != null">
           AND a.disabled = #{disabled}
        </if>
        <if test="categoryId != null">
           AND a.category_id = #{categoryId}
        </if>
    </where>
</select>

如果你使用了这个写法,就不存在1=1的问题了,所以也不用纠结,但本着打破砂锅问到底的精神,我们还是得探究一下使用1=1到底有没有性能问题

使用where 1=1有性能问题吗?

先来问问AI

可以看到,AI给到的答复是,基本没有性能问题,更多的是代码风格和可读性!

亲自检测

为了跟进一步测试,我们来写个SQL看看实际性能是否有差别

  1. explain select * from user;

  2. explain select * from user where 1=1;

  3. explain select * from user where id = 8;

  4. explain select * from user where 1=1 and id = 8;

可以看到示例1和示例2, 示例3和示例4,这两组写法,都是有个1=1的区别,但性能都是一样的,因此我们基本可以判定,这个写法,对性能是没有影响的。

总结

如果你在项目中也有使用where 1=1写的SQL,如果从可读性或者代码风格考虑,建议优化掉,但如果担心性能问题,则大可不必过多考虑~

相关推荐

  1. MySQL使用多个Where In语句

    2024-06-13 17:34:02       23 阅读
  2. mysql 里面rowid

    2024-06-13 17:34:02       8 阅读
  3. MySQL WHERE子句的使用和优化方法

    2024-06-13 17:34:02       8 阅读
  4. MySQL的binlog和redolog什么区别?

    2024-06-13 17:34:02       12 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-06-13 17:34:02       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-06-13 17:34:02       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-06-13 17:34:02       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-06-13 17:34:02       18 阅读

热门阅读

  1. 2024年科技趋势与未来展望

    2024-06-13 17:34:02       7 阅读
  2. 【C语言实现PID控制器】

    2024-06-13 17:34:02       5 阅读
  3. 视频处理与显示控制器HDMI转LVDS,支持缩放/旋转

    2024-06-13 17:34:02       5 阅读
  4. 从渲染管线到着色器Shader实践

    2024-06-13 17:34:02       6 阅读
  5. ES6 .entries用法

    2024-06-13 17:34:02       5 阅读
  6. uniapp使用vue3打包H5,android或ios加载白屏

    2024-06-13 17:34:02       3 阅读
  7. Unity 合理跳过启动LOGO动画 多平台官方API

    2024-06-13 17:34:02       9 阅读
  8. 计算机视觉(CV)技术的优势和挑战

    2024-06-13 17:34:02       4 阅读