将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 风萧萧 阅读(2024) | 评论 (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)编辑 收藏

Problem Statement

     When editing a single line of text, there are four keys that can be used to move the cursor: end, home, left-arrow and right-arrow. As you would expect, left-arrow and right-arrow move the cursor one character left or one character right, unless the cursor is at the beginning of the line or the end of the line, respectively, in which case the keystrokes do nothing (the cursor does not wrap to the previous or next line). The home key moves the cursor to the beginning of the line, and the end key moves the cursor to the end of the line.

You will be given a int, N, representing the number of character in a line of text. The cursor is always between two adjacent characters, at the beginning of the line, or at the end of the line. It starts before the first character, at position 0. The position after the last character on the line is position N. You should simulate a series of keystrokes and return the final position of the cursor. You will be given a String where characters of the String represent the keystrokes made, in order. 'L' and 'R' represent left and right, while 'H' and 'E' represent home and end.

Definition

    
Class: CursorPosition
Method: getPosition
Parameters: String, int
Returns: int
Method signature: int getPosition(String keystrokes, int N)
(be sure your method is public)
    

Constraints

- keystrokes will be contain between 1 and 50 'L', 'R', 'H', and 'E' characters, inclusive.
- N will be between 1 and 100, inclusive.

Examples

0)
    
"ERLLL"
10
Returns: 7
First, we go to the end of the line at position 10. Then, the right-arrow does nothing because we are already at the end of the line. Finally, three left-arrows brings us to position 7.
1)
    
"EHHEEHLLLLRRRRRR"
2
Returns: 2
All the right-arrows at the end ensure that we end up at the end of the line.
2)
    
"ELLLELLRRRRLRLRLLLRLLLRLLLLRLLRRRL"
10
Returns: 3
3)
    
"RRLEERLLLLRLLRLRRRLRLRLRLRLLLLL"
19
Returns: 12

This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved.

答案:


 1
 2public class CursorPosition {
 3    public int getPosition(String keystrokes, int N){
 4        int position = 0;
 5        String s = "";
 6        for(int i = 0; i < keystrokes.length(); i++){
 7            s = keystrokes.substring(i, i+1);
 8            if("L".equals(s)){
 9                if(position == 0continue;
10                position--;
11            }

12            if("R".equals(s)){
13                if(position == N) continue;
14                position++;
15            }

16            if("H".equals(s)){
17                position = 0;
18            }

19            if("E".equals(s)){
20                position = N;
21            }

22
23        }

24
25        return position;
26
27    }

28    /**
29     * @param args
30     */

31    public static void main(String[] args) {
32        CursorPosition cursorPosition = new CursorPosition();
33        int cursor = cursorPosition.getPosition("ERLLL"10);
34        System.out.println("cursor:" + cursor);
35    }

36
37}

38
posted @ 2005-11-27 23:42 风萧萧 阅读(921) | 评论 (2)编辑 收藏

Problem Statement

     A square matrix is a grid of NxN numbers. For example, the following is a 3x3 matrix:
 4 3 5
 2 4 5
 0 1 9
One way to represent a matrix of numbers, each of which is between 0 and 9 inclusive, is as a row-major String. To generate the String, simply concatenate all of the elements from the first row followed by the second row and so on, without any spaces. For example, the above matrix would be represented as "435245019".

You will be given a square matrix as a row-major String. Your task is to convert it into a String[], where each element represents one row of the original matrix. Element i of the String[] represents row i of the matrix. You should not include any spaces in your return. Hence, for the above String, you would return {"435","245","019"}. If the input does not represent a square matrix because the number of characters is not a perfect square, return an empty String[], {}.

Definition

    
Class: MatrixTool
Method: convert
Parameters: String
Returns: String[]
Method signature: String[] convert(String s)
(be sure your method is public)
    

Constraints

- s will contain between 1 and 50 digits, inclusive.

Examples

0)
    
"435245019"
Returns: {"435", "245", "019" }
The example above.
1)
    
"9"
Returns: {"9" }
2)
    
"0123456789"
Returns: { }
This input has 10 digits, and 10 is not a perfect square.
3)
    
"3357002966366183191503444273807479559869883303524"
Returns: {"3357002", "9663661", "8319150", "3444273", "8074795", "5986988", "3303524" }

This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved.

答案:


 1public class MatrixTool {
 2
 3    public String[] convert(String s){
 4        if (s == null || s.length() == 0 || s.length() > 50){
 5            return new String[]{};
 6        }

 7        int length = s.length();
 8        int n = (int)Math.sqrt(length);
 9        if(n*== length){
10            String[] result = new String[n];
11            for(int i = 0; i < n; i++){
12                result[i] = s.substring(i*n, i*+ n);
13            }

14            return result;
15        }
else {
16            return new String[]{};
17        }

18    }

19
20    /**
21     * @param args
22     */

23    public static void main(String[] args) {
24        MatrixTool matrix = new MatrixTool();
25        String[] result = matrix.convert("3357002966366183191503444273807479559869883303524");
26        for(int i = 0; i < result.length; i++){
27            System.out.println(result[i]);
28        }

29    }

30
31}

32
posted @ 2005-11-27 23:40 风萧萧 阅读(707) | 评论 (0)编辑 收藏
     摘要: Problem Statement      A simple line drawing program uses a blank 20 x 20 pixel canvas and a directional cursor that starts at the upper left corner pointing straight down. T...  阅读全文
posted @ 2005-11-27 23:37 风萧萧 阅读(1144) | 评论 (0)编辑 收藏
经典面试题

一、面向对象的三个基本特征
   2、方法重载和方法重写的概念和区别
   3、接口和内部类、抽象类的特性
   4、文件读写的基本类
   **5、串行化的注意事项以及如何实现串行化
   6、线程的基本概念、线程的基本状态以及状态之间的关系
   7、线程的同步、如何实现线程的同步
   8、几种常用的数据结构及内部实现原理。
   9、Socket通信(TCP、UDP区别及Java实现方式)
  **10、Java的事件委托机制和垃圾回收机制
  11、JDBC调用数据库的基本步骤
  **12、解析XML文件的几种方式和区别
  13、Java四种基本权限的定义
  14、Java的国际化
二、JSP
   1、至少要能说出7个隐含对象以及他们的区别
  ** 2、forward 和redirect的区别
   3、JSP的常用指令
三、servlet
   1、什么情况下调用doGet()和doPost()?
   2、servlet的init()方法和service()方法的区别
   3、servlet的生命周期
   4、如何现实servlet的单线程模式
   5、servlet的配置
   6、四种会话跟踪技术
四、EJB
   **1、EJB容器提供的服务
 主要提供声明周期管理、代码产生、持续性管理、安全、事务管理、锁和并发行管理等服务。
   2、EJB的角色和三个对象
 EJB角色主要包括Bean开发者 应用组装者 部署者 系统管理员 EJB容器提供者 EJB服务器提供者
 三个对象是Remote(Local)接口、Home(LocalHome)接口,Bean类
   2、EJB的几种类型
 会话(Session)Bean ,实体(Entity)Bean 消息驱动的(Message Driven)Bean
 会话Bean又可分为有状态(Stateful)和无状态(Stateless)两种
 实体Bean可分为Bean管理的持续性(BMP)和容器管理的持续性(CMP)两种
   3、bean 实例的生命周期
 对于Stateless Session Bean、Entity Bean、Message Driven Bean一般存在缓冲池管理,而对于Entity Bean和Statefull Session Bean存在Cache管理,通常包含创建实例,设置上下文、创建EJB Object(create)、业务方法调用、remove等过程,对于存在缓冲池管理的Bean,在create之后实例并不从内存清除,而是采用缓冲池调度机制不断重用实例,而对于存在Cache管理的Bean则通过激活和去激活机制保持Bean的状态并限制内存中实例数量。
   4、激活机制
 以Statefull Session Bean 为例:其Cache大小决定了内存中可以同时存在的Bean实例的数量,根据MRU或NRU算法,实例在激活和去激活状态之间迁移,激活机制是当客户端调用某个EJB实例业务方法时,如果对应EJB Object发现自己没有绑定对应的Bean实例则从其去激活Bean存储中(通过序列化机制存储实例)回复(激活)此实例。状态变迁前会调用对应的ejbActive和ejbPassivate方法。
   5、remote接口和home接口主要作用
 remote接口定义了业务方法,用于EJB客户端调用业务方法
 home接口是EJB工厂用于创建和移除查找EJB实例
   6、客服端调用EJB对象的几个基本步骤
一、 设置JNDI服务工厂以及JNDI服务地址系统属性
二、 查找Home接口
三、 从Home接口调用Create方法创建Remote接口
四、 通过Remote接口调用其业务方法
五、数据库
   1、存储过程的编写
   2、基本的SQL语句
六、weblogic
1、 如何给weblogic指定大小的内存?
在启动Weblogic的脚本中(位于所在Domian对应服务器目录下的startServerName),增加set MEM_ARGS=-Xms32m -Xmx200m,可以调整最小内存为32M,最大200M
2、 如何设定的weblogic的热启动模式(开发模式)与产品发布模式?
可以在管理控制台中修改对应服务器的启动模式为开发或产品模式之一。或者修改服务的启动文件或者commenv文件,增加set PRODUCTION_MODE=true。
3、 如何启动时不需输入用户名与密码?
修改服务启动文件,增加 WLS_USER和WLS_PW项。也可以在boot.properties文件中增加加密过的用户名和密码.
4、 在weblogic管理制台中对一个应用域(或者说是一个网站,Domain)进行jms及ejb或连接池等相关信息进行配置后,实际保存在什么文件中?
保存在此Domain的config.xml文件中,它是服务器的核心配置文件。
5、 说说weblogic中一个Domain的缺省目录结构?比如要将一个简单的helloWorld.jsp放入何目录下,然的在浏览器上就可打入http://主机:端口号//helloword.jsp就可以看到运行结果了? 又比如这其中用到了一个自己写的javaBean该如何办?
Domain目录\服务器目录\applications,将应用目录放在此目录下将可以作为应用访问,如果是Web应用,应用目录需要满足Web应用目录要求,jsp文件可以直接放在应用目录中,Javabean需要放在应用目录的WEB-INF目录的classes目录中,设置服务器的缺省应用将可以实现在浏览器上无需输入应用名。
6、 如何查看在weblogic中已经发布的EJB?
可以使用管理控制台,在它的Deployment中可以查看所有已发布的EJB
7、 如何在weblogic中进行ssl配置与客户端的认证配置或说说j2ee(标准)进行ssl的配置
缺省安装中使用DemoIdentity.jks和DemoTrust.jks  KeyStore实现SSL,需要配置服务器使用Enable SSL,配置其端口,在产品模式下需要从CA获取私有密钥和数字证书,创建identity和trust keystore,装载获得的密钥和数字证书。可以配置此SSL连接是单向还是双向的。
   8、在weblogic中发布ejb需涉及到哪些配置文件
不同类型的EJB涉及的配置文件不同,都涉及到的配置文件包括ejb-jar.xml,weblogic-ejb-jar.xmlCMP实体Bean一般还需要weblogic-cmp-rdbms-jar.xml
   9、EJB需直接实现它的业务接口或Home接口吗,请简述理由.
远程接口和Home接口不需要直接实现,他们的实现代码是由服务器产生的,程序运行中对应实现类会作为对应接口类型的实例被使用。
  10、说说在weblogic中开发消息Bean时的persistent与non-persisten的差别
persistent方式的MDB可以保证消息传递的可靠性,也就是如果EJB容器出现问题而JMS服务器依然会将消息在此MDB可用的时候发送过来,而non-persistent方式的消息将被丢弃。
  11、说说你所熟悉或听说过的j2ee中的几种常用模式?及对设计模式的一些看法
   Session Facade Pattern:使用SessionBean访问EntityBean
Message Facade Pattern:实现异步调用
EJB Command Pattern:使用Command JavaBeans取代SessionBean,实现轻量级访问
Data Transfer Object Factory:通过DTO Factory简化EntityBean数据提供特性
Generic Attribute Access:通过AttibuteAccess接口简化EntityBean数据提供特性
Business Interface:通过远程(本地)接口和Bean类实现相同接口规范业务逻辑一致性
EJB架构的设计好坏将直接影响系统的性能、可扩展性、可维护性、组件可重用性及开发效率。项目越复杂,项目队伍越庞大则越能体现良好设计的重要性。
转载自:http://blog.csdn.net/laou2008/archive/2005/11/15/529519.aspx

西门子的一道笔试题

设计一个函数,形式如: int func(unsigned int),要求求出不大于输入参数的最大的素数,比如输入12,返回11。

转载自:http://community.csdn.net/Expert/topic/4368/4368551.xml?temp=.4177057

微软MSN在南大的笔试题

罗马数字共有七个,即
I(1),V(5),X(10),L(50),C(100),D(500),M(1000)。

按照下面三条规则可以表示任意正整数。

重复数次:一个罗马数字重复几次,就表示这个数的几倍。

右加左减:在一个较大的罗马数字的右边记上一个较小的罗马数字,
表示大数字加小数字。在一个较大的数字的左边记上一个较小的罗
马数字,表示大数字减小数字。但是,左减不能跨越等级。
比如,99不可以用IC表示,用XCIX表示

基本数字Ⅰ、X 、C 中的任何一个,自身连用构成数目,或者放在大数的右边连用构成数目,都不能超过三个,比如40不能用XXXX,而用XL表示

设计一个函数,将100(包括100)以内的整数转换成罗马数字,超过100不考虑
int itor(int n,char* buf,int bufLength)
其中,n是要转换的整数,buf是要输出的字符串,bufLength是buf的字符长度
成功,返回0,否则,返回 -1;

比如:
char buf[256];
result = itor(n,buf,sizeof(buf));

when n = 28; result = 0, 输出"XXVIII";
when n = 72; result = 0, 输出"LXXII";

转载自:http://community.csdn.net/Expert/topic/4386/4386877.xml?temp=.411175





posted @ 2005-11-16 09:59 风萧萧 阅读(1048) | 评论 (0)编辑 收藏

<2005年11月>
303112345
6789101112
13141516171819
20212223242526
27282930123
45678910

常用链接

留言簿(8)

随笔分类

随笔档案

文章分类

文章档案

相册

收藏夹

myfriends

opensource

搜索

  •  

最新评论

阅读排行榜

评论排行榜