V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
zwj2885
V2EX  ›  程序员

兄弟们,来吐槽一段代码

  •  
  •   zwj2885 · 2020-11-02 16:17:47 +08:00 · 2125 次点击
    这是一个创建于 1468 天前的主题,其中的信息可能已经有所发展或是发生改变。

    需求很明确,要查询某个时间段内获得某种积分(关注等行为,消费等)最多的人。然后就有了一段如下的 mybatis 的配置文件。 <select id="selectForMember" resultType="java.lang.String"> SELECT member_code AS memberCode FROM mem_member_point WHERE point_status = '1' <if test="params.list != null and params.list.size() >0" > AND member_code in ( <foreach collection="params.list" item="m" index="index" separator="," > #{m} </foreach> ) </if> AND DATE_FORMAT(create_time, '%Y-%m-%d') < #{params.endTime} AND DATE_FORMAT(create_time, '%Y-%m-%d') >= #{params.beginTime} GROUP BY memberCode ORDER BY SUM(point_value) DESC LIMIT 1 </select> 然后,如大家所愿。发展了 1 万个会员的时候,感觉很爽,秒出。当发展了 200 万会员的时候,画风变成了以下这个样子。 FROM mem_member_point WHERE point_status = '1' AND member_code IN ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?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, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?

    大概就是这种,200 万问号啊卧槽。 五味杂陈啊心情。

    12 条回复    2020-11-03 09:48:45 +08:00
    zwj2885
        1
    zwj2885  
    OP
       2020-11-02 16:18:21 +08:00
    格式乱了,不知道咋保持格式。
    boris93
        2
    boris93  
       2020-11-02 16:48:36 +08:00 via Android
    .............
    我不知道说什么
    Orz
    aragakiyuii
        3
    aragakiyuii  
       2020-11-02 16:52:20 +08:00 via iPhone
    有些数据库 in 是有数量限制的吧,还有提交 sql 的大小限制...

    原表 join 一下或者放到临时表里 join 一下再或者代码里 for 一下?
    leafre
        4
    leafre  
       2020-11-02 17:06:54 +08:00   ❤️ 2
    那就 NOT IN
    SkyYu822
        5
    SkyYu822  
       2020-11-02 17:13:42 +08:00
    angryfish
        6
    angryfish  
       2020-11-02 18:00:03 +08:00 via iPhone
    那怎么做比较好?
    个人觉得,要是同一个库的,之类 join 两个表就行了,不要把 params.list 通过代码过去出来。
    但如果不是一个库,请大神表演
    arthas2234
        7
    arthas2234  
       2020-11-02 18:12:22 +08:00
    这个 member_code 有什么规律,可以加个冗余字段来区分
    luckyrayyy
        8
    luckyrayyy  
       2020-11-02 18:14:54 +08:00
    量大的话就没必要读取全量了啊,滚动读
    kazeik
        9
    kazeik  
       2020-11-02 18:33:43 +08:00 via iPhone
    完了,手机出问题了,全是问号
    wellsc
        10
    wellsc  
       2020-11-02 18:37:21 +08:00
    我的眼睛
    DEVN
        11
    DEVN  
       2020-11-02 18:46:26 +08:00 via iPhone
    我不理解你们的业务,但是 in 绝对不能这么用!
    zwj2885
        12
    zwj2885  
    OP
       2020-11-03 09:48:45 +08:00
    @DEVN 是的
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   1841 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 23ms · UTC 16:32 · PVG 00:32 · LAX 08:32 · JFK 11:32
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.