Database optimization and problems
Preface , I have been developing reports for nearly a year, and most of the time I write sql. I want to share with you what I have learned about databases. Here I mainly talk about commonly used database optimizations.
What is database optimization
My understanding is that manipulating data takes the least amount of time and is more accurate. Here are some common SQL optimization techniques and common problems in lifeGenerally, when encountering deduplicated data, the distinct function is generally used, which can be replaced by group by. Note that distinct cannot be used in conjunction with count(1).
Example
SELECT DISTINCT(mgr) FROM emp
– Use group by instead of distinct function
SELECT mgr FROM emp GROUP BY mgr
– correct writing
SELECT COUNT(1), mgr FROM emp GROUP BY mgr
– wrong writing
SELECT COUNT(1), DISTINCT(mgr) FROM empWhen using group by, not all the columns to be queried should be placed behind group by, sometimes it can be used in max (column name)
example
– the results of the two queries are the same, the first one is more efficient
SELECT ename,max (job),max(mgr) FROM emp GROUP BY enameSELECT ename,job,mgr FROM emp GROUP BY ename,job,mgr
Pay attention when comparing sizes
Example
SELECT * FROM EMP WHERE MGR>3
after optimization
select * from emp where mgr>=4When doing a query, try not to perform a full table query
Example
– full table query
SELECT * FROM EMP – SELECT mgr FROM EMP
for a certain columnThe difference between count(1) and count(*)
count(1) is to count the first column, and does not count the phenomenon that the first column is empty,
but phenomenon that the statistics are emptyThe difference between sum() and count()
sum is the sum of a certain column, count is the count
sum(0) is 0The problem of finding percentages
Example
There will be no 0 displayed before the decimal point. The best solution is
SELECT to_char(round(800/1000,2)*100,'999')||'%' FROM dualGenerally, when the result of count(1) has no data, it will display 0. Generally, if there are conditions later, it will not display zero.
Minimize associations between large tables, especially outer joins, and use nested SELECT instead ,
such as
SELECT a.deptno,b.deptno FROM emp a,dept b WHERE a.deptno=b.deptno
– Performance
SELECT (SELECT b.deptno FROM dept b WHERE aadeptno=b.deptno) a,a.deptno FROM emp aThere is an index, but when scanning the whole table, you need to pay attention to the parameters passed in. It is not necessarily that the index is not easy to use. After using the function
on the field to compare, the index cannot be used.in 可以改成exists函数
例子
SELECT * FROM emp a,dept b
WHERE a.deptno=b.deptno
–优化
SELECT * FROM emp a WHERE Exists(SELECT 1 FROM dept WHERE a.deptno=b.deptno)SELECT * FROM emp a WHERE a.deptno in(select b.deptno from dept d where a.deptno=b.deptno)
–优化
SELECT * FROM emp a WHERE exists(select b.deptno from dept d where a.deptno=b.deptno)count()与sum()
例子
select count(*)from emp where mgr>100
select count(*) from emp where mgr>200
优化
select count(case when mgr>100 then 1 else null end),
count(case when mgr>200 then 1 else null end),from empThe execution order of SQL statements is from bottom to top and from right to left, so when writing a select statement, write the most restrictive condition at the bottom
Use uppercase for SQL statements; because Oracle always parses SQL statements first, converts lowercase letters to uppercase, and executes them
Avoid using NOT on indexed columns In general,
we want to avoid using NOT on indexed columns, NOT will have the same effect as using functions on indexed columns. When ORACLE "encounters" NOT, it will stop using the index instead Perform a full table scan.union all replaces union, because union all has automatic sorting function
replace or with in
Avoid IS NULL and IS NOT NULL on indexed columns
For multiple time ranges, the query result is the time without duplicate time ranges
Example
SELECT MIN(cykssj), MAX(cyjssj)
FROM (SELECT cykssj,
cyjssj,
SUM(broken) OVER(ORDER BY cykssj, cyjssj) flag
FROM (SELECT t. *,
(CASE
WHEN cykssj <= MAX(cyjssj)
OVER(ORDER BY cykssj,
cyjssj ROWS BETWEEN UNBOUNDED
PRECEDING AND 1 PRECEDING) THEN
0
ELSE
1
END) AS broken
FROM fw_zjjh t))
GROUP BY flag;Grading function
example
select ZZMC, LEVEL from xt_zz
WHERE ROWNUM<2
start WITH zzdm='01' – from which level code query
connect by prior zzdm = sjdm – condition of classificationDetermine whether a string contains a certain string or character
Example
– Query xxmc that includes the word work order in xxmc
select c.xxmc rom kfgdxx c where instr(c.xxmc,'work order')>0with as 用法
select * from
(SELECT LEVEL AS lv FROM DUAL CONNECT BY LEVEL < 20
) tt
WHERE tt.lv > 10 AND tt.lv < 15
– after optimization
with TT as(SELECT LEVEL AS lv FROM DUAL CONNECT BY LEVEL < 20 )
select lv from TT
WHERE lv > 10 AND lv < 15Total by column
Example
select nvl(to_char(t.cjsj,'MM'),'total'),sum(t.hwl) ,sum(t.zhwl) from FW_TYSL_95598 t GROUP BY ROLLUP(to_char(t.cjsj,' MM'))You can use sum, group by and listagg group by for column transfer
If you encounter that both columns and rows are dynamically changed, there are two ways to achieve it. You can first check the columns and store them in a temporary table, and then check the rows and store them in another temporary table. Finally, a double-layer cycle can be implemented. Another method is to realize dynamic changes of rows in the foreground and column changes in the data.
Example
1 AB
2 BC
3 BA
should show
1 AB
2 BC
– principle of implementation
select count(1)from(
select 2||1 from dual
union
select 1||2 from dual
minus
select 1||2 from dual)