Oracle Hints-/*+cardinality()*/模拟表的数据量

本文通过示例解释了在 Oracle SQL 查询中使用 /*+cardinality()*/ 提示如何模拟不同数据量,以影响执行计划。通过对两个全表扫描的表进行连接操作,并调整 cardinality 的值,展示了该提示如何改变成本估算,从而优化查询性能。实验结果显示,当 cardinality 值增大时,查询成本上升,执行时间延长。
  1* select /*+cardinality(t 1000000000)*/count(*) from t,t t1 where t.object_id=t1.object_id
SQL> /

Execution Plan
----------------------------------------------------------
Plan hash value: 791582492

----------------------------------------------------------------------------
| Id  | Operation	    | Name | Rows  | Bytes | Cost (%CPU)| Time	   |
----------------------------------------------------------------------------
|   0 | SELECT STATEMENT    |	   |	 1 |	10 |  6216  (91)| 00:01:15 |
|   1 |  SORT AGGREGATE     |	   |	 1 |	10 |		|	   |
|*  2 |   HASH JOIN	    |	   |  1000M|  9536M|  6216  (91)| 00:01:15 |
|   3 |    TABLE ACCESS FULL| T    | 73081 |   356K|   285   (1)| 00:00:04 |
|   4 |    TABLE ACCESS FULL| T    |  1000M|  4768M|  2794  (90)| 00:00:34 |
----------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   2 - access("T"."OBJECT_ID"="T1"."OBJECT_ID")

SQL> ed
Wrote file afiedt.buf

  1* select count(*) from t,t t1 where t.object_id=t1.object_id
SQL> /

Execution Plan
----------------------------------------------------------
Plan hash value: 791582492

----------------------------------------------------------------------------
| Id  | Operation	    | Name | Rows  | Bytes | Cost (%CPU)| Time	   |
----------------------------------------------------------------------------
|   0 | SELECT STATEMENT    |	   |	 1 |	10 |   571   (1)| 00:00:07 |
|   1 |  SORT AGGREGATE     |	   |	 1 |	10 |		|	   |
|*  2 |   HASH JOIN	    |	   | 73081 |   713K|   571   (1)| 00:00:07 |
|   3 |    TABLE ACCESS FULL| T    | 73081 |   356K|   285   (1)| 00:00:04 |
|   4 |    TABLE ACCESS FULL| T    | 73081 |   356K|   285   (1)| 00:00:04 |
----------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   2 - access("T"."OBJECT_ID"="T1"."OBJECT_ID")

The `/*+parallel(20)*/` hint is a directive used in SQL queries to inform the database optimizer to attempt to execute the query in parallel using a specific degree of parallelism. In this case, the number `20` indicates that the query should be executed using 20 parallel processes or threads. This type of hint is particularly useful in environments with large datasets and high-performance computing capabilities, as it can significantly reduce query execution time by distributing the workload across multiple processors or nodes. Parallel execution can be beneficial for queries that involve large table scans, joins, or sorts, where the cost of dividing the work and coordinating the results is outweighed by the gains in execution speed. The `/*+parallel(20)*/` hint can be placed within the SQL statement, typically right after the `SELECT`, `INSERT`, `UPDATE`, or `DELETE` keyword, to influence the execution plan generated by the optimizer. Here is an example of how the `/*+parallel(20)*/` hint might be used in a `SELECT` statement: ```sql SELECT /*+parallel(20)*/ * FROM large_table WHERE some_column = 'some_value'; ``` In this example, the database will attempt to execute the `SELECT` operation using 20 parallel processes. It is important to note that the actual degree of parallelism achieved may depend on various factors such as the database configuration, the hardware resources available, and the current system load. When using the `/*+parallel(20)*/` hint, it is also important to consider the impact on the overall system performance, as excessive parallelism can lead to resource contention and degradation of performance for other operations. Therefore, the use of parallel hints should be carefully evaluated and tested in the specific environment where the query will be executed. It is also worth noting that not all database systems support the `/*+parallel*/` hint in the same way, and the syntax or functionality may vary between different database vendors. For instance, Oracle Database supports the `PARALLEL` hint, while in other systems, such as MySQL, parallel query execution is not supported in the same manner and would require different techniques or tools to achieve similar results. When considering the use of the `/*+parallel(20)*/` hint, it is advisable to consult the documentation provided by the specific database vendor to understand the full implications and best practices associated with parallel query execution.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值