A.sort_values(by=idx_voltage, inplace=True)什么意思
时间: 2024-05-29 14:10:53 浏览: 100
这是一个Pandas DataFrame对象的方法,用于按照指定的列进行排序。
参数说明:
- `by`:指定要排序的列名或列名列表。
- `inplace`:如果为True,则在原始数据上进行排序,否则返回排序后的新数据。
在这个例子中,`A`是一个DataFrame对象,`idx_voltage`是一个字符串,指定要按照该列进行升序排序。`inplace=True`表示在原始DataFrame对象上进行排序。
相关问题
优化以下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 = ?
首先,可以看到这个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
```
帮我优化以下sql select s.* from ifare.structred_rule s right join ( select MAX(r.VERSION) vers, r.status, r.tbl_no, r.live_from, r.data_type, r. source from ifare.structred_rule r where (r.status in ('8', '')) group by r.status, r.tbl_no, r.live_from, r.data_type, r. source) tp on s.version = tp.vers and s.tbl_no = tp.tbl_no and s.status = tp.status and s.live_from = tp.live_from and s.data_type = tp.data_type and s.SOURCE = tp.source and s.status <> '-1' where s.version ='00001' and s.remark = 'D' and s.audited_date >= '20211010' and s.audited_date <='20211011' order by s.db_date asc, s.tbl_no asc, s.version desc
首先,可以将右连接转换为内连接,因为在右连接的情况下,我们只对右表进行了聚合,而不是左表。这可能会影响查询性能。其次,可以将子查询的结果存储到临时表中,以便后续查询使用。最后,可以为联接条件创建索引以提高查询性能。
下面是优化后的SQL:
```
CREATE TEMPORARY TABLE tmp_structred_rule AS
SELECT MAX(r.VERSION) AS vers, r.status, r.tbl_no, r.live_from, r.data_type, r.source
FROM ifare.structred_rule r
WHERE r.status IN ('8', '')
GROUP BY r.status, r.tbl_no, r.live_from, r.data_type, r.source;
SELECT s.*
FROM ifare.structred_rule s
JOIN tmp_structred_rule tp
ON s.version = tp.vers
AND s.tbl_no = tp.tbl_no
AND s.status = tp.status
AND s.live_from = tp.live_from
AND s.data_type = tp.data_type
AND s.SOURCE = tp.source
WHERE s.status <> '-1'
AND s.version = '00001'
AND s.remark = 'D'
AND s.audited_date >= '20211010'
AND s.audited_date <= '20211011'
ORDER BY s.db_date ASC, s.tbl_no ASC, s.version DESC;
```
同时,为联接条件创建以下索引:
```
CREATE INDEX idx_structred_rule_version ON ifare.structred_rule (version);
CREATE INDEX idx_structred_rule_tbl_no ON ifare.structred_rule (tbl_no);
CREATE INDEX idx_structred_rule_status ON ifare.structred_rule (status);
CREATE INDEX idx_structred_rule_live_from ON ifare.structred_rule (live_from);
CREATE INDEX idx_structred_rule_data_type ON ifare.structred_rule (data_type);
CREATE INDEX idx_structred_rule_source ON ifare.structred_rule (source);
```
这些优化措施应该能够提高查询性能。
阅读全文