开裆裤狠拉风 2021-12-02 11:43 采纳率: 25%
浏览 319
已结题

mybatis中使用一对多导致查询效率慢 求解决

1.一对多中按年月分组

<resultMap id="stuClassAwayQuery" type="com.wlpy.common.vo.report.StuPrecollectedVo">
        <result column="stuId" jdbcType="INTEGER" property="stuId" />
        <result column="classId" jdbcType="VARCHAR" property="classId" />
        // 数据过多  for查询导致效率低
        <collection property="yearQueryVO" select="yearQuery" column="{stuId=id,classId=classId}">
            <result column="year" jdbcType="VARCHAR" property="year" />
            <result column="month" jdbcType="VARCHAR" property="month" />
            <result column="money" jdbcType="VARCHAR" property="money" />
        </collection>
 </resultMap>
SELECT
    ar.class_id,
    ar.stu_id 
FROM
    apply_relation AS ar

img

SELECT
    DATE_FORMAT( scr.attendance_time, '%Y-%m-%d' ) AS `year`,
    DATE_FORMAT( scr.attendance_time, '%m' ) AS `month`,
    IFNULL( sum( scr.course_price ), 0 ) AS money 
FROM
    stu_course_relation AS scr 
WHERE
    scr.class_id = 1 #{classId}
    AND scr.stu_id = #{stuId}
GROUP BY
    DATE_FORMAT(
        scr.attendance_time,
    '%Y%m')
  • 写回答

3条回答 默认 最新

  • _Onelone丶 2021-12-02 13:04
    关注

    看一下这条sql的执行计划,按需求建立查询索引。

    评论

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 2月23日
  • 创建了问题 12月2日

悬赏问题

  • ¥20 python忆阻器数字识别
  • ¥15 无法输出helloworld
  • ¥15 高通uboot 打印ubi init err 22
  • ¥20 PDF元数据中的XMP媒体管理属性
  • ¥15 R语言中lasso回归报错
  • ¥15 网站突然不能访问了,上午还好好的
  • ¥15 有没有dl可以帮弄”我去图书馆”秒选道具和积分
  • ¥15 semrush,SEO,内嵌网站,api
  • ¥15 Stata:为什么reghdfe后的因变量没有被发现识别啊
  • ¥15 振荡电路,ADS仿真