1、系统设计
1、纵向、横向分割表,减少表的尺寸
纵向:字段多。按业务主题分割。根据页大小。
横向:数据多。按条件分割表。
eg:按年存储表,历史数据表和当期数据更新表。
2、将数据处理交给DB,编写存储过程
1)减少网络的开销;
2)存储过程是编译、优化过,速度快。
3、只读查询操作优化方法
1)数据量小的数据,放到程序缓存中;
2)物化视图,减除了为引用视图的查询动态建立结果集
的开销
3)适当的数据冗余,以减少数据库表之间的链接操作,
提高查询速度。
2、SQL语句
1、 不使用游标。
declare cursor c_job is select * from emp for update;
c_row c_job%rowtype;
begin
for c_row in c_job loop
if c_row.sal<10000 then
newsal:=c_row.sal*2;
update emp set sal=newsal where current of c_job;
end if;
end loop;
end;
Update emp set sal=case when sal<10000 then sal*2 else sal end;
避免使用not in
• Select * from emp
Where dept_id not in (select id from
dept)
Select a.*
From emp a left outer join dept b on
a.dept_id=b.id
Where b.id is null
• 避免使用in
• Select * from emp
Where dept_id in (select id from dept
where dept_name like ‘研发%’);
Select * from emp a Where exists
(select 1 from dept b where a.dept_id=b.id and
b.dept_name like ‘研发%’);
Select a.*
From emp a, dept b
Where a.dept_id=b.id and b.dept_name like ‘研发%’;
使用group by不使用distinct
• Select distinct dept_id from emp;
• Select dept_id from emp group by
dept_id;
不要在一句话里再三地使用相同的函数,
浪费资源,将结果放在变量里再调用更快。
• 用OR的字句可以分解成多个查询,并且通
过UNION链接多个查询。它们的速度只与
是否使用索引有关,如果查询需要用到联
合索引,用UNION all执行的效率更高。
内存临时表的使用
• Select * into #temp_emp from emp
Where dept_id=‘0101’;
• CREATE GLOBAL TEMPORARY TABLE
temp_emp(……);
• declare global temporary table t_emp
like emp not logged with replace on
commit preserve rows;
3、部署
参数优化:内存、CPU
• 存储优化:将数据、日志、索引文件使用
不同的IO设备。