You need to enable JavaScript to run this app. Prisma ORM生成SQL性能优化求助:多表查询效率极低
优惠活动
大模型
产品
解决方案
定价
更多

Prisma ORM生成SQL性能优化求助:多表查询效率极低

关于Prisma多表关联查询性能优化的问题

我使用Prisma编写了一个关联7张表的查询,每张表仅包含2-4条数据。但Prisma自动生成的SQL执行耗时可达2-3分钟,而我手动编写的SQL仅需1ms即可完成。请问除了使用RAW SQL外,是否有其他优化方法?目前我不想放弃使用Prisma,恳请各位提供帮助。

手动编写的SQL示例

SELECT *
    FROM rubricCellAssessment
    INNER JOIN rubricAssessment ON rubricCellAssessment.rubricAssessmentId = rubricAssessment.id
    INNER JOIN rubricCell ON rubricCellAssessment.rubricCellId = rubricCell.id
    INNER JOIN criteriaDescriptor ON rubricCell.criteriaDescriptorId = criteriaDescriptor.id
    INNER JOIN criteria ON criteriaDescriptor.criteriaId = criteria.id
    INNER JOIN skill ON criteria.skillId = skill.id
    INNER JOIN user AS student ON rubricAssessment.studentId = student.id
    INNER JOIN user AS assessor ON rubricAssessment.assessorId = assessor.id
    LEFT JOIN studentContentBase ON rubricAssessment.studentContentBaseId = studentContentBase.id
    WHERE student.userTypeId = 1
      AND assessor.userTypeId IN (2, 1000, 3)
      AND skill.id = 'bd47bfff-760c-4fec-86d2-1063bbd89d63'
      AND criteriaDescriptor.criteriaId = criteria.id
      AND rubricCell.criteriaDescriptorId = criteriaDescriptor.id;

Prisma生成的SQL示例

select
    `lp-mvp`.`RubricCellAssessment`.`id`,
    `lp-mvp`.`RubricCellAssessment`.`createdAt`,
    `lp-mvp`.`RubricCellAssessment`.`updatedAt`,
    `lp-mvp`.`RubricCellAssessment`.`assessmentValue`,
    `lp-mvp`.`RubricCellAssessment`.`rubricCellId`,
    `lp-mvp`.`RubricCellAssessment`.`rubricAssessmentId`
from
    `lp-mvp`.`RubricCellAssessment`
where
    ((`lp-mvp`.`RubricCellAssessment`.`id`) in (
    select
        `t0`.`id`
    from
        `lp-mvp`.`RubricCellAssessment` as `t0`
    inner join `lp-mvp`.`RubricAssessment` as `j0` on
        (`j0`.`id`) = (`t0`.`rubricAssessmentId`)
    where
        ((`j0`.`id`) in (
        select
            `t1`.`id`
        from
            `lp-mvp`.`RubricAssessment` as `t1`
        inner join `lp-mvp`.`User` as `j1` on
            (`j1`.`id`) = (`t1`.`studentId`)
        where
            (`j1`.`userTypeId` = 1
                and `t1`.`id` is not null))
        and `t0`.`id` is not null))
    and (`lp-mvp`.`RubricCellAssessment`.`id`) in (
    select
        `t0`.`id`
    from
        `lp-mvp`.`RubricCellAssessment` as `t0`
    inner join `lp-mvp`.`RubricCell` as `j0` on
        (`j0`.`id`) = (`t0`.`rubricCellId`)
    where
        ((`j0`.`id`) in (
        select
            `t1`.`id`
        from
            `lp-mvp`.`RubricCell` as `t1`
        inner join `lp-mvp`.`CriteriaDescriptor` as `j1` on
            (`j1`.`id`) = (`t1`.`criteriaDescriptorId`)
        where
            ((`j1`.`id`) in (
            select
                `t2`.`id`
            from
                `lp-mvp`.`CriteriaDescriptor` as `t2`
            inner join `lp-mvp`.`Criteria` as `j2` on
                (`j2`.`id`) = (`t2`.`criteriaId`)
            where
                ((`j2`.`id`) in (
                select
                    `t3`.`id`
                from
                    `lp-mvp`.`Criteria` as `t3`
                inner join `lp-mvp`.`Skill` as `j3` on
                    (`j3`.`id`) = (`t3`.`skillId`)
                where
                    (`j3`.`id` = "bd47bfff-760c-4fec-86d2-1063bbd89d63"
                        and `t3`.`id` is not null))
                and `t2`.`id` is not null))
            and `t1`.`id` is not null))
        and `t0`.`id` is not null))
    and (`lp-mvp`.`RubricCellAssessment`.`id`) in (
    select
        `t0`.`id`
    from
        `lp-mvp`.`RubricCellAssessment` as `t0`
    inner join `lp-mvp`.`RubricAssessment` as `j0` on
        (`j0`.`id`) = (`t0`.`rubricAssessmentId`)
    where
        ((`j0`.`id`) in (
        select
            `t1`.`id`
        from
            `lp-mvp`.`RubricAssessment` as `t1`
        inner join `lp-mvp`.`User` as `j1` on
            (`j1`.`id`) = (`t1`.`assessorId`)
        where
            (`j1`.`userTypeId` in (2,1000,3)
                and `t1`.`id` is not null))
        and `t0`.`id` is not null))
    and (`lp-mvp`.`RubricCellAssessment`.`id`) in (
    select
        `t0`.`id`
    from
        `lp-mvp`.`RubricCellAssessment` as `t0`
    inner join `lp-mvp`.`RubricCell` as `j0` on
        (`j0`.`id`) = (`t0`.`rubricCellId`)
    where
        ((`j0`.`id`) in (
        select
            `t1`.`id`
        from
            `lp-mvp`.`RubricCell` as `t1`
        inner join `lp-mvp`.`CriteriaDescriptor` as `j1` on
            (`j1`.`id`) = (`t1`.`criteriaDescriptorId`)
        where
            ((`j1`.`id`) in (
            select
                `t2`.`id`
            from
                `lp-mvp`.`CriteriaDescriptor` as `t2`
            inner join `lp-mvp`.`Criteria` as `j2` on
                (`j2`.`id`) = (`t2`.`criteriaId`)
            where
                ((`j2`.`id`) in (
                select
                    `t3`.`id`
                from
                    `lp-mvp`.`Criteria` as `t3`
                inner join `lp-mvp`.`Skill` as `j3` on
                    (`j3`.`id`) = (`t3`.`skillId`)
                where
                    (`j3`.`id` = "bd47bfff-760c-4fec-86d2-1063bbd89d63"
                        and `t3`.`id` is not null))
                and `t2`.`id` is not null))
            and `t1`.`id` is not null))
        and `t0`.`id` is not null)))

优化方法

  • 调整Prisma查询写法,采用嵌套关联模式
    当前Prisma生成的SQL是多层嵌套的IN子查询,低效原因在于查询写法可能是通过根级多次过滤关联表。改成从主表RubricCellAssessment出发,用include或select嵌套关联所有依赖表,并在关联层级内添加过滤条件,而非在根级重复过滤。这样Prisma会生成类似手动SQL的JOIN语句,避免冗余子查询。

  • 确保关联字段与过滤字段都有索引
    即便数据量小,嵌套子查询也难以利用索引。检查所有外键字段(如rubricAssessmentId、rubricCellId、criteriaId等)是否创建了索引;同时给过滤用到的userTypeId、skill.id字段添加索引,能直接提升查询匹配效率。

  • 开启Prisma的关联JOIN配置
    在较新版本的Prisma中,可在schema.prisma的generator块添加relationJoins = true配置,强制Prisma使用JOIN而非嵌套子查询处理关联关系,生成的SQL会更接近手动编写的高效版本。

  • 移除查询中的冗余条件
    手动SQL里的criteriaDescriptor.criteriaId = criteria.id、rubricCell.criteriaDescriptorId = criteriaDescriptor.id属于关联的默认约束,Prisma关联查询时会自动处理,无需额外添加。多余条件会导致Prisma生成冗余的子查询,增加执行开销。

内容的提问来源于stack exchange,提问作者Aibynn

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.31 05:03:36