Mybatis中distinct的用法问题

有看到这样的写法,想要优化一下

<select id="selectProperty">
  select distinct t.*
  from table t
  where t.property1 = #{porperty1}
  and t.property2 = #{porperty2}
</select>

想去除select *,改为

<sql id="ALL_COLUMNS">
    property1, property2, property3
</sql>

<select id="selectProperty">
  select distinct <include refid="ALL_COLUMNS"/>
  from table t
  where t.property1 = #{porperty1}
  and t.property2 = #{porperty2}
</select>

想请教修改后效果应该是和原来一致的吧,会不会有什么问题。

同时还有一个疑问,distinct * 理论上把所有字段都不同的记录才看做不同,其中主键id是增长的,这样子加distinct的意义在哪呢,id不同,那么理论上这样写distinct也没有查出重复记录从而去重的执行场景呢。

阅读 4.6k
1 个回答

有主键还不如不加,加了还会变慢

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题