SELECT * from rpt_pl_piglet_plan_fatten_source WHERE farm_id="318c85b3-11e1-11eb-a56c-7cd30ac44e04" AND op_week = (SELECT op_week from rpt_pl_piglet_plan_fatten_source WHERE farm_id="318c85b3-11e1-11eb-a56c-7cd30ac44e04" ORDER BY op_week DESC LIMIT 1) 优化
时间: 2023-04-10 14:02:48 浏览: 130
这个 SQL 查询语句可以通过以下方式进行优化:
1. 使用索引:在 farm_id 和 op_week 字段上创建索引,可以加快查询速度。
2. 避免子查询:可以使用 JOIN 操作来避免子查询,例如:
SELECT t1.* FROM rpt_pl_piglet_plan_fatten_source t1
INNER JOIN (
SELECT MAX(op_week) AS max_op_week FROM rpt_pl_piglet_plan_fatten_source WHERE farm_id="318c85b3-11e1-11eb-a56c-7cd30ac44e04"
) t2 ON t1.op_week = t2.max_op_week AND t1.farm_id = "318c85b3-11e1-11eb-a56c-7cd30ac44e04"
这个查询语句使用了一个内部查询来获取最大的 op_week 值,然后使用 INNER JOIN 操作将其与原始表连接起来,以获取符合条件的行。这个查询语句比原始的查询语句更有效率。
相关问题
优化下这个代码 select sum(auth_amt) sum_auth_amt from auth_cont auth left join (select * from RPT_DUE_LOAN_ACC_M loan where loan.send_flag = '2' and loan.prd_userdf_type != '3017' and loan.bank_id = 162000 and (loan.cif_no in (select cif_no from RPT_DUE_LOAN_ACC_M a where 1 = 1 AND LOAN.YEAR = '2021' AND LOAN.MONTH = '12' AND LOAN.ACCOUNT_STATUS NOT IN ('0', '2') AND ((LOAN.LOAN_BAL > 0 OR LOAN.IN_INTST > 0 OR LOAN.OUT_INTST > 0 OR LOAN.CMPD_INTST > 0) or substr(LOAN.SETTL_DATE, 0, 6) = '202112') AND LOAN.MANG_BR_NO IN (SELECT BR_NO FROM TBL_ORG_DEPARTMENTS START WITH BR_NO = '162000' CONNECT BY PRIOR BR_NO = UP_ONE) group by cif_no and (loan.cif_no in (select cif_no from RPT_DUE_LOAN_ACC_M a where 1 = 1 AND LOAN.YEAR = '2021' AND LOAN.MONTH = '12' AND LOAN.ACCOUNT_STATUS NOT IN ('0', '2') AND ((LOAN.LOAN_BAL > 0 OR LOAN.IN_INTST > 0 OR LOAN.OUT_INTST > 0 OR LOAN.CMPD_INTST > 0) or substr(LOAN.SETTL_DATE, 0, 6) = '202112') AND LOAN.MANG_BR_NO IN (SELECT BR_NO FROM TBL_ORG_DEPARTMENTS START WITH BR_NO = '162000' CONNECT BY PRIOR BR_NO = UP_ONE) group by cif_no group by cif_no) loan on loan.cif_no = auth.cif_no where auth_sts = '1' AND LOAN.YEAR = '2021' AND LOAN.MONTH = '12' AND LOAN.ACCOUNT_STATUS NOT IN ('0', '2') AND ((LOAN.LOAN_BAL > 0 OR LOAN.IN_INTST > 0 OR LOAN.OUT_INTST > 0 OR LOAN.CMPD_INTST > 0) or substr(LOAN.SETTL_DATE, 0, 6) = '202112') AND LOAN.MANG_BR_NO IN (SELECT BR_NO FROM TBL_ORG_DEPARTMENTS START WITH BR_NO = '162000' CONNECT BY PRIOR BR_NO = UP_ONE)
答:SELECT SUM(AUTH_AMT)SUM_AUTH_AMT FROM AUTH_CONT AUTH LEFT JOIN(SELECT * FROM RPT_DUE_LOAN_ACC_M LOAN WHERE LOAN.SEND_FLAG = 2 AND LOAN.PRD_USERDF_TYPE != 3017 AND LOAN.BANK_ID = 162000 AND(LOAN.CIF_NO IN(SELECT CIF_NO FROM RPT_DUE_LOAN_ACC_M A WHERE LOAN.YEAR = 2021 AND LOAN.MONTH = 12 AND LOAN.ACCOUNT_STATUS NOT IN(0,2)AND((LOAN.LOAN_BAL>0 OR LOAN.IN_INTST>0 OR LOAN.OUT_INTST>0 OR LOAN.CMPD_INTST>0)OR SUBSTR(LOAN.SETTL_DATE,0,6)= 202112)AND LOAN.MANG_BR_NO IN(SELECT BR_NO FROM TBL_ORG_DEPARTMENTS START WITH BR_NO = 162000 CONNECT BY PRIOR BR_NO = UP_ONE)GROUP BY CIF_NO)ON LOAN.CIF_NO = AUTH.CIF_NO WHERE AUTH_STS = 1 AND LOAN.YEAR = 2021 AND LOAN.MONTH = 12 AND LOAN.ACCOUNT_STATUS NOT IN(0,2)AND((LOAN.LOAN_BAL>0 OR LOAN.IN_INTST>0 OR LOAN.OUT_INTST>0 OR LOAN.CMPD_INTST>0)OR SUBSTR(LOAN.SETTL_DATE,0,6)= 202112)AND LOAN.MANG_BR_NO IN(SELECT BR_NO FROM TBL_ORG_DEPARTMENTS START WITH BR_NO = 162000 CONNECT BY PRIOR BR_NO = UP_ONE)GROUP BY CIF_NO)LOAN ON LOAN.CIF_NO = AUTH.CIF_NO WHERE AUTH_STS = 1。
INSERT OVERWRITE TABLE dw_rpt_report60010105049 SELECT ( SELECT MAX(char3) FROM orsuser_kara.para_nationality WHERE char2 = dw.tran_country ) AS tran_country, col2, col3, ... FROM dw_rpt_report60010105049 dw WHERE EXISTS ( SELECT 1 FROM orsuser_kara.para_nationality WHERE char2 = dw.tran_country ) 不用子查询作字段的写法,换一种写法
可以使用 JOIN 实现类似的功能,以下是可能的写法:
```
INSERT OVERWRITE TABLE dw_rpt_report60010105049
SELECT
COALESCE(n.char3, dw.tran_country) AS tran_country,
dw.col2,
dw.col3,
...
FROM dw_rpt_report60010105049 dw
LEFT JOIN orsuser_kara.para_nationality n
ON dw.tran_country = n.char2
WHERE n.char2 IS NOT NULL
```
这里使用了 LEFT JOIN,将原表和 `orsuser_kara.para_nationality` 表进行 JOIN 操作。在 JOIN 操作中,使用 `COALESCE` 函数将 `orsuser_kara.para_nationality` 表中的 `char3` 字段和原表中的 `tran_country` 字段合并成一个字段。如果 `orsuser_kara.para_nationality` 表中不存在与原表中的 `tran_country` 字段匹配的值,则 `COALESCE` 函数会返回原表中的 `tran_country` 字段。最后使用 WHERE 子句过滤掉没有匹配的记录。
阅读全文