Sql Statement Turing part1

本文深入探讨了SQL查询优化过程,包括生成潜在计划、成本估算和选择最优计划的方法。详细解释了如何根据应用需求设置优化目标,并通过实例展示了不同级别的配置选项,包括全表优化、部分行优化和最佳响应时间优化。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Optimizer Modes
RBO
CBO


The query optimizer performs the following steps:


1.The optimizer generates a set of potential plans for the SQL statement based on available access paths and hints.


2.The optimizer estimates the cost of each plan based on statistics in the data dictionary for the data distribution and storage characteristics of the tables,indexes,and partitions accessed by the statement.
The cost is an estimated value proportional to the expected resource use needed to execute the statement with a particular plan.The optimizer calculates the cost of access paths and join orders based on the estimated computer resources,which inclueds I/O,CPU,and memory.


Serial plans with higher costs take more time to execute than those with smaller costs.When using a parrallel plan,however,resouce use is not directly related to elapsed time.


3.The optimizer compares the costs of the plans and chooses the one with the lowest cost.


推荐书籍:Cost-Based Oracle fundamentals -by Jonathan Lewis


Optimized Goal
By default,the goal of the query optimizer is the best throughout.This means that it chooses the least amount of resources necessary to process all rows accessed by the statement.Oracle can also optimize a statement with the goal of best reponse time,This means that it uses the least amount of resources necessary to process the first row accessed by a SQL statement.


/*thoughtout是吞吐率,是一秒钟内客户端与服务器之间的传送数据量;
reponse time是指响应时间,如查询1000条数据,返回第一条的时间;
以上两种对应着不同的优化目标与方法*/


Choose a goal for the optimizer based on the needs of your application.
* For applications performed in batch,such as Oracle Reports applications,optimize for best throughout.Usually,throughput is more important in batch applications,because the user initiating the application is only concerned with the time necessary for the application to complete.Response time is less important,because the user does not examine the results of individual statements while the application is running.
* For interactive applications,such as Oracle Forms applications or SQL*Plus queries,optimize for best response time.Usually,response time is important in interactive applications,because the interactive user is waiting to see the first row or first few rows accessed by the statement.


/*betch是批处理,interacive是交互的*/


Setting the Mode
The optimizer's behavior when choosing an optimization approach and goal for a SQL statement is affected by the following factors:
*OPTIMIZER_MODE Initialization Parameter.
*Optimizer SQL Hints for Changing the Query Optimizer Goal.
*Query Optimizer Statistics in the Data Dictionary.


*At the instance level /*即alter system set optimizer_mode=*/
optimizer_mode = {Choose|Rule|First_rows|First_rows_n|All_rows}
*At the session level:
alter session set optimizer_mode={choose|Rule|First_rows|First_rows_n|All_rows}


*At the statement level: -Using hints


三个级别,第三个能覆盖第二个,第二个能覆盖第一个;


All_ROWS
The optimizer uses a cost-based approach for all SQL statements in the session regardless of the presence of statistics and optimizes with a goal of best throughput(minimum resource use to complete the entire statement).This is the default value /*获得最大的吞吐率为目标*/


FIRST_ROWS_n
The optimizer uses a cost-based approach,regardless of the presence of statistics,and optimizes with a goal of best response time to return the first n number of rows;n can equal 1,10,100,1000.


FIRST_ROWS:10g,11g不用管,为了向后兼容而设。


Using Hints in SQL
CREATE index gen_idx on customers(cust_gender);
select /*+ INDEX(customers gen_idx)*/
cust_last_name,cust_street_address,
cust_postal_code
FROM sh.customers
WHERE UPPER(cust_gender) = 'M';
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值