优化mySQL语句 SELECT * (SELECT DEPT_ID, FROM SPSYS_DEPARTMENT WHERE DEPT_ABBR = COST_CENTER AND RECORD_STATUS = '1') DEPT_ID, (SELECT DEPT_NAME FROM SPSYS_DEPARTMENT WHERE DEPT_ABBR = COST_CENTER AND RECORD_STATUS = '1') DEPT_NAME, (SELECT DEPT_NO FROM SPSYS_DEPARTMENT WHERE DEPT_ABBR = COST_CENTER AND RECORD_STATUS = '1') DEPT_NO, FROM BM_BUDGET_REC_EXEC_ORA_IMP WHERE CURRENCY = 'RMB'
时间: 2024-02-14 12:15:00 浏览: 289
HTML_tag.rar_HTML 手册_html t_html tag_html 标签 手册
这个 SQL 查询语句可以进行如下优化:
1. 使用 JOIN 代替子查询,可以提高查询效率:
```
SELECT b.DEPT_ID, d.DEPT_NAME, d.DEPT_NO
FROM BM_BUDGET_REC_EXEC_ORA_IMP b
LEFT JOIN SPSYS_DEPARTMENT d ON b.COST_CENTER = d.DEPT_ABBR
WHERE b.CURRENCY = 'RMB' AND d.RECORD_STATUS = '1';
```
2. 不要使用 SELECT *,只选择需要的列,可以减少查询时间和网络传输的数据量。
```
SELECT b.DEPT_ID, d.DEPT_NAME, d.DEPT_NO
FROM BM_BUDGET_REC_EXEC_ORA_IMP b
LEFT JOIN SPSYS_DEPARTMENT d ON b.COST_CENTER = d.DEPT_ABBR
WHERE b.CURRENCY = 'RMB' AND d.RECORD_STATUS = '1';
```
3. 对于经常查询的列,可以添加索引,以提高查询效率。
```
ALTER TABLE SPSYS_DEPARTMENT ADD INDEX idx_dept_abbr_record_status (DEPT_ABBR, RECORD_STATUS);
```
阅读全文