优化以下SQL使其查询速度更快SELECT P.LOT_NO UNIT_ID, '0' AS UNIT_TYPE, '宗地' AS UNIT_TYPE_DES, NVL(BD.PARCEL_CODE_TMP, P.LOT_NO || P.SITE) UNIT_CODE, P.PARCEL_NO, P.LOC_CANTON, BD.REAL_UNIT_NO AS UNIT_CODE_GB, P.LU_LOCATION, P.SITE_KEY, P.CHG_TYPE, P.LU_AREA AS UNIT_AREA, P.PARCEL_CODE FROM V_LD_PARCEL P LEFT JOIN V_BD_PARCEL BD ON BD.LOT_NO = P.LOT_NO where 2 > 1 and not exists (select 1 from v_bk_reg_idx idx where idx.reg_unit_code = p.lot_no and idx.reg_unit_type = '0' and idx.buss_reg_type = '1101' and (idx.status_book = '1' or idx.status_book = '9')) and not exists(select 1 from v_bk_reg_idx idx where (idx.reg_unit_code, idx.reg_unit_type) in (select rise_id, '1' from bd_rise br, bd_parcel bp where br.par_lot_no = bp.lot_no and bp.lot_no = p.lot_no) and idx.buss_reg_type = '1200') and p.parcel_no = ?
时间: 2024-01-11 19:05:40 浏览: 141
首先,可以看到这个SQL语句中有两个子查询。这些子查询可能会导致性能问题,因此可以尝试优化它们。
以下是可能的优化措施:
1. 将子查询转换为JOIN语句
将第一个子查询转换为JOIN语句,可以避免使用NOT EXISTS,从而提高性能。例如:
```
SELECT P.LOT_NO UNIT_ID,
'0' AS UNIT_TYPE,
'宗地' AS UNIT_TYPE_DES,
NVL(BD.PARCEL_CODE_TMP, P.LOT_NO || P.SITE) UNIT_CODE,
P.PARCEL_NO,
P.LOC_CANTON,
BD.REAL_UNIT_NO AS UNIT_CODE_GB,
P.LU_LOCATION,
P.SITE_KEY,
P.CHG_TYPE,
P.LU_AREA AS UNIT_AREA,
P.PARCEL_CODE
FROM V_LD_PARCEL P
LEFT JOIN V_BD_PARCEL BD ON BD.LOT_NO = P.LOT_NO
LEFT JOIN v_bk_reg_idx idx ON idx.reg_unit_code = p.lot_no
AND idx.reg_unit_type = '0'
AND idx.buss_reg_type = '1101'
AND (idx.status_book = '1' OR idx.status_book = '9')
WHERE 2 > 1
AND idx.reg_unit_code IS NULL
AND P.PARCEL_NO = ?
```
同样,将第二个子查询转换为JOIN语句也可以提高性能:
```
SELECT P.LOT_NO UNIT_ID,
'0' AS UNIT_TYPE,
'宗地' AS UNIT_TYPE_DES,
NVL(BD.PARCEL_CODE_TMP, P.LOT_NO || P.SITE) UNIT_CODE,
P.PARCEL_NO,
P.LOC_CANTON,
BD.REAL_UNIT_NO AS UNIT_CODE_GB,
P.LU_LOCATION,
P.SITE_KEY,
P.CHG_TYPE,
P.LU_AREA AS UNIT_AREA,
P.PARCEL_CODE
FROM V_LD_PARCEL P
LEFT JOIN V_BD_PARCEL BD ON BD.LOT_NO = P.LOT_NO
LEFT JOIN v_bk_reg_idx idx ON idx.reg_unit_code = p.lot_no
AND idx.reg_unit_type = '0'
AND idx.buss_reg_type = '1101'
AND (idx.status_book = '1' OR idx.status_book = '9')
LEFT JOIN v_bk_reg_idx idx2 ON idx2.reg_unit_code = (SELECT rise_id FROM bd_rise br, bd_parcel bp WHERE br.par_lot_no = bp.lot_no AND bp.lot_no = p.lot_no)
AND idx2.reg_unit_type = '1'
AND idx2.buss_reg_type = '1200'
WHERE 2 > 1
AND idx.reg_unit_code IS NULL
AND idx2.reg_unit_code IS NULL
AND P.PARCEL_NO = ?
```
2. 添加索引
为V_LD_PARCEL和V_BD_PARCEL表添加适当的索引,可以加速JOIN操作。例如:
```
CREATE INDEX idx_v_ld_parcel_parcel_no ON V_LD_PARCEL (PARCEL_NO);
CREATE INDEX idx_v_bd_parcel_lot_no ON V_BD_PARCEL (LOT_NO);
```
3. 减少查询返回的行数
如果查询返回的行数非常大,可以考虑分页或者限制返回的行数。这可以减少查询的执行时间。例如,可以添加以下子句来限制返回的行数:
```
WHERE ROWNUM <= 100
```
阅读全文