将Java进行到底
将Java进行到底
posts - 15,  comments - 66,  trackbacks - 0

Sql优化是一项复杂的工作,以下的一些基本原则是本人看书时所记录下来的,很明确且没什么废话:

1.  索引的使用:

1.当插入的数据为数据表中的记录数量的10%以上,首先需要删除该表的索引来提高数据的插入效率,当数据插入后,再建立索引。

2.避免在索引列上使用函数或计算,在where子句中,如果索引是函数的一部分,优化器将不再使用索引而使用全表扫描。如:

低效:select * from dept where sal*12 >2500;

高效:select * from dept where sal>2500/12;

(3).避免在索引列上使用not !=”,索引只能告诉什么存在于表中,而不能告诉什么不存在于表中,当数据库遇到not !=”时,就会停止使用索引而去执行全表扫描。

(4).索引列上>=代替>

 低效:select * from emp where deptno > 3

 高效:select * from emp where deptno >=4

两者的区别在于,前者dbms将直接跳到第一个deptno等于4的记录,而后者将首先定位到deptno等于3的记录并且向前扫描到第一个deptno大于3的。

(5).非要对一个使用函数的列启用索引,基于函数的索引是一个较好的方案。

2. 游标的使用:

   当在海量的数据表中进行数据的删除、更新、插入操作时,用游标处理的效率是最慢的,但是游标又是必不可少的,所以正确使用游标十分重要:

   (1). 在数据抽取的源表中使用时间戳,这样每天的维表数据维护只针对更新日期为最新时间的数据来进行,大大减少需要维护的数据记录数。

   (2). insertupdate维表时都加上一个条件来过滤维表中已经存在的记录,例如:

insert into dim_customer select * from ods_customer where ods_customer.code not exists (dim_customer.code)

 ods_customer为数据源表。dim_customer为维表。

   (3). 使用显式的游标,因为隐式的游标将会执行两次操作,第一次检索记录,第二次检查too many rows这个exception,而显式游标不执行第二次操作。

3.  据抽取和上载时的sql优化:

(1). Where 子句中的连接顺序:

oracle采用自下而上的顺序解析where子句,根据这个原理,表之间的连接必须写在其他where条件之前,那些可以过滤掉大量记录的条件必须写在where子句的末尾。如:

低效:select * from emp e where sal>5000 and job = ‘manager’ and 25<(select count (*) from emp where mgr=e.empno);

高效:select * from emp e where 25<(select count(*) from emp where mgr=e.empno) and sal>5000 and job=’manager’;

   (2). 删除全表时,用truncate 替代 delete,同时注意truncate只能在删除全表时适用,因为truncateddl而不是dml

   (3). 尽量多使用commit

只要有可能就在程序中对每个delete,insert,update操作尽量多使用commit,这样系统性能会因为commit所释放的资源而大大提高。

   (4). exists替代in ,可以提高查询的效率。

   (5). not exists 替代 not in

   (6). 优化group by

提高group by语句的效率,可以将不需要的记录在group by之前过滤掉。如:

低效:select job, avg(sal) from emp group by job having job = ‘president’ or job=’manager’;

高效: select job, avg(sal) from emp having  job=’president’ or job=’manager’ group by job;

   (7). 有条件的使用union-all 替代 union:这样做排序就不必要了,效率会提高35倍。

   (8). 分离表和索引

       总是将你的表和索引建立在不同的表空间内,决不要将不属于oracle内部系统的对象存放到system表空间内。同时确保数据表空间和索引表空间置于不同的硬盘控制卡控制的硬盘上。


转自:http://blog.csdn.net/eigo/archive/2006/03/02/614157.aspx
posted @ 2006-03-04 20:34 风萧萧 阅读(448) | 评论 (0)编辑 收藏

/*
建表:
dept:
 deptno(primary key),dname,loc
emp:
 empno(primary key),ename,job,mgr,sal,deptno
*/

1 列出emp表中各部门的部门号,最高工资,最低工资
select max(sal) as 最高工资,min(sal) as 最低工资,deptno from emp group by deptno;

2 列出emp表中各部门job为'CLERK'的员工的最低工资,最高工资
select max(sal) as 最高工资,min(sal) as 最低工资,deptno as 部门号 from emp where job = 'CLERK' group by deptno;

3 对于emp中最低工资小于1000的部门,列出job为'CLERK'的员工的部门号,最低工资,最高工资
select max(sal) as 最高工资,min(sal) as 最低工资,deptno as 部门号 from emp as b
where job='CLERK' and 1000>(select min(sal) from emp as a where a.deptno=b.deptno) group by b.deptno

4 根据部门号由高而低,工资有低而高列出每个员工的姓名,部门号,工资
select deptno as 部门号,ename as 姓名,sal as 工资 from emp order by deptno desc,sal asc

5 写出对上题的另一解决方法
(请补充)

6 列出'张三'所在部门中每个员工的姓名与部门号
select ename,deptno from emp where deptno = (select deptno from emp where ename = '张三')

7 列出每个员工的姓名,工作,部门号,部门名
select ename,job,emp.deptno,dept.dname from emp,dept where emp.deptno=dept.deptno

8 列出emp中工作为'CLERK'的员工的姓名,工作,部门号,部门名
select ename,job,dept.deptno,dname from emp,dept where dept.deptno=emp.deptno and job='CLERK'

9 对于emp中有管理者的员工,列出姓名,管理者姓名(管理者外键为mgr)
select a.ename as 姓名,b.ename as 管理者 from emp as a,emp as b where a.mgr is not null and a.mgr=b.empno

10 对于dept表中,列出所有部门名,部门号,同时列出各部门工作为'CLERK'的员工名与工作
select dname as 部门名,dept.deptno as 部门号,ename as 员工名,job as 工作 from dept,emp
where dept.deptno *= emp.deptno and job = 'CLERK'

11 对于工资高于本部门平均水平的员工,列出部门号,姓名,工资,按部门号排序
select a.deptno as 部门号,a.ename as 姓名,a.sal as 工资 from emp as a
where a.sal>(select avg(sal) from emp as b where a.deptno=b.deptno) order by a.deptno

12 对于emp,列出各个部门中平均工资高于本部门平均水平的员工数和部门号,按部门号排序
select count(a.sal) as 员工数,a.deptno as 部门号 from emp as a
where a.sal>(select avg(sal) from emp as b where a.deptno=b.deptno) group by a.deptno order by a.deptno

13 对于emp中工资高于本部门平均水平,人数多与1人的,列出部门号,人数,按部门号排序
select count(a.empno) as 员工数,a.deptno as 部门号,avg(sal) as 平均工资 from emp as a
where (select count(c.empno) from emp as c where c.deptno=a.deptno and c.sal>(select avg(sal) from emp as b where c.deptno=b.deptno))>1
group by a.deptno order by a.deptno

14 对于emp中低于自己工资至少5人的员工,列出其部门号,姓名,工资,以及工资少于自己的人数
select a.deptno,a.ename,a.sal,(select count(b.ename) from emp as b where b.sal<a.sal) as 人数 from emp as a
where (select count(b.ename) from emp as b where b.sal<a.sal)>5


转自:http://blog.csdn.net/woolceo/archive/2006/03/02/614094.aspx

posted @ 2006-03-04 20:31 风萧萧 阅读(2023) | 评论 (1)编辑 收藏
在开发部署PORTAL项目时,遇到异常:
Exception:weblogic.management.ApplicationException: prepare failed for content_repo.jar Module: content_repo.jar Error: Exception preparing module: EJBModule(content_repo.jar,status=NEW) Unable to deploy EJB: content_repo.jar from content_repo.jar: Class not found: com.bea.content.repo.i18n.RepoExceptionTextFormatter java.lang.NoClassDefFoundError: Class not found: com.bea.content.repo.i18n.RepoExceptionTextFormatter at weblogic.ejb20.compliance.EJBComplianceChecker.check([Ljava.lang.Object;)V(EJBComplianceChecker.java:287)

我在weblogic81 sp3的doc中没有发现com.bea.content.repo.i18n这个package.

重新安装了weblogic sp4,就不再出现这个错误了。
posted @ 2006-02-15 23:14 风萧萧 阅读(602) | 评论 (0)编辑 收藏

<2006年2月>
2930311234
567891011
12131415161718
19202122232425
2627281234
567891011

常用链接

留言簿(8)

随笔分类

随笔档案

文章分类

文章档案

相册

收藏夹

myfriends

opensource

搜索

  •  

最新评论

阅读排行榜

评论排行榜