Month: March 2011

Oracle:字段顺序也会影响效率

摘自《Troubleshooting Oracle Performance》 Oracle从数据块中取某个字段的数据时,它需要知道这个字段在该行数据中所处的位置;而确定位置的方法很土: 要像知道第2个字段的位置,必须先知道第1个字段所占的长度,然而用这个偏移量去找第2个字段;同理,取第3个字段时,要先知道第2个字段的位置和长度。 所以说, 取越靠后的字段,效率就越低。 所以,越要频繁读取的字段,越应该放前面。

Oracle: 通过数组接口实现批量数据传输

摘自《Troubleshooting Oracle Performance》 如果要按相同的SQL插入一万条数据,一条一条插的效率是很低的;可以把所有数据组装成“数组”,然后只执行一次插入即可。 JDBC的batch update的实现其实就是利用了数据库的这种机制

Oracle行预取

没有行预取时,数据是一条一条传到客户端的; 而有了行预取,比如10,则数据会按每次10条传给客户端。 这样做可以明显降低网络传输次数,提高效率。 Oracle Jdbc Driver 提供了行预取的设置:    connectionProperties.put("defaultRowPrefetch","1")

Oracle:Nested Loops, Merge Join, Hash Join

摘自《Troubleshooting Oracle Performance》 Nested Loops   一个Outer Loop,一个Inner Loop,外部的循环驱动内部的循环 Merge Join   两边的数据集会先按连接的字段排序,然后再连接 Hash Join   左边的数据集会用来构建一个哈希表,等哈希表建好之后,再逐条查看右边的数据集,看它们是否可以命中这张哈希表 比较    Nested Loops可以在产生第一条记录时即开始返回数据,Hash Join有时可以,Merge Join完全不行    如果目标是尽快返回所有记录      1.若查询的选择性较高,则Hash Join最佳      2.否则,则应使用Nested Loops    

Oracle: SQL Hints — leading, use_nl, ordered

leading     leading(a) 即指示在Nested Loop时使用a表作为Outer Loop use_nl     use_nl(a) 指示连接a表时,要使用Nested Loop并指定a作为Inner Loop ordered    select /*+ ordered */ … from t1, t2, t3 where t1.c = t2.c and t2.c = t3.c 指示:先找出t1的记录,然后根据它找出t2的记录,最后再根据t2的结果找出t3的记录   

Oracle: 索引 与… where a = xx and b = xx and c = xx

摘自《Troubleshooting Oracle Performance》 对于B-树索引   如果在a,b,c三列分别建一个索引,则      … where a = xx and b = xx and c = xx 仍然非常低效   如果在a,b,c三列上建立联合索引,则        … where a = xx and b = xx and c = xx 会高效的多      而… where a = xx and b = xx  也会比较高效,即使c列没有出现在where子句 对于位图索引   如果在a,b,c三列分别建一个索引,则      … where …

Oracle: 索引 与… where a = xx and b = xx and c = xx Read More »

Oracle: 索引与 … where upper(a) = XX

摘自《Troubleshooting Oracle Performance》 1.如果索引建在 a 列上,则    … where upper(a) = XX 用不上索引    … where a = upper(xx) 仍可以用得上索引 2.如果索引建在 upper(a) 这个函数上,或者实现了upper(a)的虚拟列上, 则      … where upper(a) = XX 可以用得上索引

Oracle: 索引与 … where a like xx

摘自《Troubleshooting Oracle Performance》 … where a like ‘A%’ 可以用上索引,即Index Range Scan 但 … where a like ‘%A’则会导致全表扫描或全索引扫描 也就是说,如果以通配符开头,就会导致效率低的模糊搜索

Oracle: 索引与 … where a is null

摘自《Troubleshooting Oracle Performance》 假设 a 列上建了一条索引 1. 对于B-树索引    … where a is null 会导致全表扫描,因为B-树索引不存储NULL值    若a+b上有联合索引,则        … where a is null and b = xx 仍会使用索引 2.对于位图索引       … where a is null 仍然会使用索引,因为位图索引会存储NULL值