Oracle优化器、优化模式、表的连接方式(Hash Join、Nested Loop
查询优化器Oracle的查询优化器(QO)分为两种: 从 Oracle 10g开始,Oracle已放弃RBO,但为了兼容性,仍然可以设置RBO. 优化模式优化模式分为: SQL> show parameter optimizer_mode;
NAME TYPE VALUE
------------------------------------ ----------- ------------------------------
optimizer_mode string ALL_ROWS
SQL>
表的连接方式Hash Join : 把小表的数据存到内存中,并建立HashTable,然后用大表的每条记录来匹配HashTable。两个表关联的字段无需建立索引,查找Hash要比查找索引快; A join in which the database uses the smaller of two tables or data sources to build a hash table in memory. The database scans the larger table,probing the hash table for the addresses of the matching rows in the smaller table. 为了提高效率,需要设置hash_area_size 足够大,如果Hash表占用的内存超过了hash_area_size的大小,就会分页到临时表空间,这会带来一定的性能损耗。 什么时候optimizer 会选择使用 Hash Joins呢? 一、优化器自动选择 二、人工指定 SQL> select /*+use_hash(amy_emp,amy_dept*/ count(*) from amy_emp,amy_dept where amy_emp.deptno=amy_dept.deptno;
a) 这种方法是在oracle7后来引入的,使用了比较先进的连接理论,一般来说,其效率应该好于其它2种连接,但是这种连接只能用在CBO优化器中,而且需要设置合适的hash_area_size参数,才能取得较好的性能。 Nested Loop : 外表驱动内表,外表的每一行都会在内表中进行匹配。与Hash Join不同的是,没有使用内表来生成HashTable,因此内表最好有索引。 It is important to ensure that the inner table is driven from (dependent on) the outer table. If the inner table’s access path is independent of the outer table,then the same rows are retrieved for every iteration of the outer loop,degrading performance considerably. In such cases,hash joins joining the two independent row sources perform better. a) 如果driving row source(外部表)比较小,并且在inner row source(内部表)上有唯一索引,或有高选择性非唯一索引时,使用这种方法可以得到较好的效率。 Sort Merge Join : Sort merge joins can join rows from two independent sources. Hash joins generally perform better than sort merge joins. However,sort merge joins can perform better than hash joins if both of the following conditions exist: However,if a sort merge join involves choosing a slower access method (an index scan as opposed to a full table scan),then the benefit of using a sort merge might be lost. Sort merge joins are useful when the join condition between two tables is an inequality condition such as <,<=,>,or >=. Sort merge joins perform better than nested loop joins for large data sets. You cannot use hash joins unless there is an equality condition. In a merge join,there is no concept of a driving table. The join consists of two steps: If the input is sorted by the join column,then a sort join operation is not performed for that row source. a) 对于非等值连接,这种连接方式的效率是比较高的。 三种连接方式的区别和选择
http://blog.csdn.net/cupid1102/article/details/7591027?locationNum=10&fps=1 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |