针对该 SQL 语句的优化,可以考虑以下几个方面:

  1. 索引优化:针对 four_dim_productfour_dim_product_dyl 表中的相关字段进行索引创建,以加快查询速度。例如,对于 four_dim_product 表的 org_noproduct_no 字段创建索引,对于 four_dim_product_dyl 表的 org_noproduct_no 字段创建索引。

  2. 子查询优化:将 not exists 子查询改为 left join 查询,可以提高查询性能。修改后的 SQL 语句如下:

    SELECT t.id AS id
    FROM public.four_dim_product t
    LEFT JOIN public.four_dim_product_dyl d ON d.org_no = 'BYYT0' AND d.product_no = t.product_no
    WHERE t.org_no = 'BYYT0' AND d.product_no IS NULL
    

    这样可以将两个表连接起来,只返回 four_dim_product 表中不存在于 four_dim_product_dyl 表中的记录。

  3. 分页查询:如果查询结果量很大,可以考虑使用分页查询,将查询结果分批返回,减少数据传输的压力。

  4. 硬件优化:如果硬件条件允许,可以考虑增加服务器的内存和 CPU,以提升查询性能。

需要根据具体的数据库环境和数据分布情况来选择合适的优化方案。同时,可以通过 EXPLAIN 关键字来分析 SQL 语句的执行计划,以确定是否存在其他可以优化的地方

pg sql语句: select tid as id from publicfour_dim_product t where torg_no=BYYT0 and not existsselect 1 from publicfour_dim_product_dyl where org_no=BYYT0 and product_no=tproduct_nofour_dim_product 和four

原文地址: https://www.cveoy.top/t/topic/ibNg 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录