Oracle 11g中dynamic sampling自动调节(auto-adjusted)机制
对Oracle而言,基于成本优化器CBO工作的基础是系统、对象统计量和CPU成本计算公式。而大多数情况下,收集统计量是一个异步单独的
对Oracle而言,基于成本优化器CBO工作的基础是系统、对象统计量和CPU成本计算公式。而大多数情况下,收集统计量是一个异步单独的过程。无论是在9i,还是目前主流11g,发挥CBO作用都需要我们有一个统计量收集或者分析的过程。
在9i时代,CBO和RBO是交替工作的。Oracle是不会“主动”的将对象统计量进行收集(那个时期也成为分析analyze)。系统调优人员也大都是解决统计量缺失、CBO优化器缺陷问题,目前能看到的很多hint也大都在9i时期编写系统。
到了10g之后,两个情况让CBO彻底取代RBO。一个是CBO成为Oracle默认的优化器使用类型,SQL语句默认情况下就使用CBO进行解析。另一个是自动统计量夜间收集作业的推出,Oracle可以自动找时间进行统计量收集。应该说,大部分的SQL语句从10g之后都是使用CBO进行执行计划生成。
但是,无论如何,都存在统计量不及时的情况。一个新创建的数据表或者发生大量变化的数据集合,如果没有手工的数据收集,CBO总是工作在统计量缺失或者陈旧统计量的情况下。
RHEL/CentOS下 Oracle 11g客户端配置
1、从Dynamic Sampling谈起
为了应对统计量缺失的情况,Oracle推出了Dynamic Sampling(12c之后称为Dynamic Statistic)动态采样收集的技术策略。简单点说,如果数据表等对象没有统计量存在,Oracle有需要统计量生成执行计划,数据库会进行一次临时性的数据收集动作。根据不同的采用比例,Oracle实时的采集部分数据块,应用查询条件进行小规模试算。最后根据统计规则,将结果集合放大后,作为结果集合统计量和row source纳入到CBO体系里面。
从SQL执行的角度看,,Dynamic Sampling给Oracle用户带来两个方面好处:
ü 关联条件SQL语句精准估算。对于常见的统计量,如选择率、数据分布,都是以单列为中心进行计数的。如果SQL语句中对应的是两个相关联的where条件,那么单列统计量计算出来的结果集合往往是偏小。如果采用Dynamic Sampling,采用动作是实时进行计算的,可以消除关联where条件影响;
ü CBO普遍应用。应该说,没有统计量,CBO是没有任何工作的可能的!Dynamic Sampling虽然不能给Oracle提供出最精确的统计量,但是起码可以让CBO运行提供基本条件;
从不利的方面看,Dynamic Sampling也带来一些问题。比如,和驻留在数据字典中的统计量不同,Dynamic Sampling并不会将收集采用值保存在数据字典里面,而是一次SQL就进行一次收集动作。采样比例如果高,就意味着parse前的动态收集动作消耗资源高。如果采样比例低,性能虽然可以好一些,但是存在着“低效”执行计划生成的纪律。
这其实是一个矛盾。如果数据表比较小,采样比例小一些,执行计划效率低一些其实也没有过多问题。但是如果数据表很大,SQL语句属于关键SQL,这样低效的执行计划其实就意味着风险。
Oracle中,是通过optimizer_dynamic_sampling设置采样比例。11gR2中,默认为2。
SQL> show parameter dyn
NAME TYPE VALUE
------------------------------------ ----------- ------------------------------
optimizer_dynamic_sampling integer 2
为了应对由于动态采样带来的问题,在11g里面针对特殊的数据对象和SQL场景,Oracle CBO采取了自动调节采样率的优化策略。
下面我们进行相应的实验。
2、环境介绍
我们使用Oracle 11gR2版本进行测试,默认的采样控制参数是2。
SQL> select * from v$version;
BANNER
-------------------------------------------
Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - Production
PL/SQL Release 11.2.0.3.0 - Production
CORE 11.2.0.3.0 Production
TNS for Linux: Version 11.2.0.3.0 - Production
NLSRTL Version 11.2.0.3.0 - Production
SQL> show parameter dyn
NAME TYPE VALUE
-------------- ----------- ----------------
optimizer_dynamic_sampling integer 2
创建一个大数据表,体积为2G。
SQL> select tablespace_name, bytes/1024/1024/1024 from dba_segments where owner='SYS' and segment_name='T';
TABLESPACE_NAME BYTES/1024/1024/1024
------------------------------ --------------------
TESTTBL 2.099609375
Owner列上面创建索引IDX_T_OWNER。
3、自动sampling adjusted
当前采用取值为2,由于数据表比较大,采用并行查询策略。
SQL> explain plan for select /*+ parallel(t, 2) */count(*) from t;
Explained
SQL> select * from table(dbms_xplan.display);

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











The main role of MySQL in web applications is to store and manage data. 1.MySQL efficiently processes user information, product catalogs, transaction records and other data. 2. Through SQL query, developers can extract information from the database to generate dynamic content. 3.MySQL works based on the client-server model to ensure acceptable query speed.

InnoDB uses redologs and undologs to ensure data consistency and reliability. 1.redologs record data page modification to ensure crash recovery and transaction persistence. 2.undologs records the original data value and supports transaction rollback and MVCC.

Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

MySQL index cardinality has a significant impact on query performance: 1. High cardinality index can more effectively narrow the data range and improve query efficiency; 2. Low cardinality index may lead to full table scanning and reduce query performance; 3. In joint index, high cardinality sequences should be placed in front to optimize query.

The basic operations of MySQL include creating databases, tables, and using SQL to perform CRUD operations on data. 1. Create a database: CREATEDATABASEmy_first_db; 2. Create a table: CREATETABLEbooks(idINTAUTO_INCREMENTPRIMARYKEY, titleVARCHAR(100)NOTNULL, authorVARCHAR(100)NOTNULL, published_yearINT); 3. Insert data: INSERTINTObooks(title, author, published_year)VA

MySQL is suitable for web applications and content management systems and is popular for its open source, high performance and ease of use. 1) Compared with PostgreSQL, MySQL performs better in simple queries and high concurrent read operations. 2) Compared with Oracle, MySQL is more popular among small and medium-sized enterprises because of its open source and low cost. 3) Compared with Microsoft SQL Server, MySQL is more suitable for cross-platform applications. 4) Unlike MongoDB, MySQL is more suitable for structured data and transaction processing.

InnoDBBufferPool reduces disk I/O by caching data and indexing pages, improving database performance. Its working principle includes: 1. Data reading: Read data from BufferPool; 2. Data writing: After modifying the data, write to BufferPool and refresh it to disk regularly; 3. Cache management: Use the LRU algorithm to manage cache pages; 4. Reading mechanism: Load adjacent data pages in advance. By sizing the BufferPool and using multiple instances, database performance can be optimized.

MySQL efficiently manages structured data through table structure and SQL query, and implements inter-table relationships through foreign keys. 1. Define the data format and type when creating a table. 2. Use foreign keys to establish relationships between tables. 3. Improve performance through indexing and query optimization. 4. Regularly backup and monitor databases to ensure data security and performance optimization.
