相信大家对索引结构非常熟悉了,它是由根、支、叶组成。当然这里指的是常用的B+树索引。我们依然使用《Oracle收集表与列统计信息
相信大家对索引结构非常熟悉了,它是由根、支、叶组成。当然这里指的是常用的B+树索引。我们依然使用《Oracle收集表与列统计信息》(见 )里的测试表。下面分析索引统计信息的相关内容。
一、如何查询索引统计信息
查询索引统计信息需要用到user_ind_statistics,下面是典型的查询语句。
SELECT INDEX_NAME AS NAME,
BLEVEL,
LEAF_BLOCKS AS LEAF_BLKS,
DISTINCT_KEYS AS DST_KEYS,
NUM_ROWS,
CLUSTERING_FACTOR AS CLUST_FACT,
AVG_LEAF_BLOCKS_PER_KEY AS LEAF_PER_KEY,
AVG_DATA_BLOCKS_PER_KEY AS DATA_PER_KEY
FROM USER_IND_STATISTICS
WHERE TABLE_NAME = 'T';
NAME BLEVEL LEAF_BLKS DST_KEYS NUM_ROWS CLUST_FACT LEAF_PER_KEY DATA_PER_KEY
---------- ---------- ---------- ---------- ---------- ---------- ------------ ------------
T_PK 1 2 1000 1000 978 1 1
T_VAL1_I 1 2 445 509 500 1 1
T_VAL2_I 1 3 6 1000 176 1 29
这里的几列具体含义是:
①blevel:也就是B-Tree level,比如从根到支再到叶,blevel为2,但索引的高度是blevel+1也就是3。
②leaf_block:索引中的叶子块数。
③distinct_keys:索引中的唯一键值总数。
④num_rows:索引中的键值数。
⑤clustering_factor:聚簇因子,它用来表征索引和数据之间的排序程度。这个因子的最小值是表里非空数据块的个数,最大值是索引的键数。下面研究如何计算聚簇因子。
二、如何计算聚簇因子
下面是计算聚簇因子的脚本,
CREATE OR REPLACE FUNCTION clustering_factor (
p_owner IN VARCHAR2,
p_table_name IN VARCHAR2,
p_column_name IN VARCHAR2
) RETURN NUMBER IS
l_cursor SYS_REFCURSOR;
l_clustering_factor BINARY_INTEGER := 0;
l_block_nr BINARY_INTEGER := 0;
l_previous_block_nr BINARY_INTEGER := 0;
l_file_nr BINARY_INTEGER := 0;
l_previous_file_nr BINARY_INTEGER := 0;
BEGIN
OPEN l_cursor FOR
'SELECT dbms_rowid.rowid_block_number(rowid) block_nr, '||
' dbms_rowid.rowid_to_absolute_fno(rowid, '''||
p_owner||''','''||
p_table_name||''') file_nr '||
'FROM '||p_owner||'.'||p_table_name||' '||
'WHERE '||p_column_name||' IS NOT NULL '||
'ORDER BY ' || p_column_name;
LOOP
FETCH l_cursor INTO l_block_nr, l_file_nr;
EXIT WHEN l_cursor%NOTFOUND;
IF (l_previous_block_nr l_block_nr OR l_previous_file_nr l_file_nr)
THEN
l_clustering_factor := l_clustering_factor + 1;
END IF;
l_previous_block_nr := l_block_nr;
l_previous_file_nr := l_file_nr;
END LOOP;
CLOSE l_cursor;
RETURN l_clustering_factor;
END;
/
这个函数表示的一些含义说明一下,首先定义了一个函数,包含三个参数:所属、表名、列名,还定义了若干个返回值变量。接着定义了一个游标,,该游标是根据所传入的参数,返回每条记录所在的块号、文件号。接着遍历游标,提取每一个记录的数据块号与文件号,若数据块号不与前一个数据块号相同,或者文件号不与前一个文件号相同,则聚簇因子加一。
下面验证这个算法的正确性,
SELECT I.INDEX_NAME,
I.CLUSTERING_FACTOR,
CLUSTERING_FACTOR(USER, I.TABLE_NAME, IC.COLUMN_NAME) AS MY_CLSTF
FROM USER_INDEXES I, USER_IND_COLUMNS IC
WHERE I.TABLE_NAME = 'T'
AND I.INDEX_NAME = IC.INDEX_NAME;
INDEX_NAME CLUSTERING_FACTOR MY_CLSTF
------------------------------ ----------------- ----------
T_PK 972 972
T_VAL1_I 506 506
T_VAL2_I 178 178
可以看出,结果中用脚本中的函数算出的聚簇因子与oracle自带的clustering_factor列结果完全一致。
相关阅读:
Oracle收集统计信息导致索引被监控
Oracle扩展的统计信息
Oracle确定过期的统计信息
关于Oracle 11g 统计信息的收集
Oracle 收集统计值 收集统计信息

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.

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 is worth learning because it is a powerful open source database management system suitable for data storage, management and analysis. 1) MySQL is a relational database that uses SQL to operate data and is suitable for structured data management. 2) The SQL language is the key to interacting with MySQL and supports CRUD operations. 3) The working principle of MySQL includes client/server architecture, storage engine and query optimizer. 4) Basic usage includes creating databases and tables, and advanced usage involves joining tables using JOIN. 5) Common errors include syntax errors and permission issues, and debugging skills include checking syntax and using EXPLAIN commands. 6) Performance optimization involves the use of indexes, optimization of SQL statements and regular maintenance of databases.

MySQL is suitable for beginners to learn database skills. 1. Install MySQL server and client tools. 2. Understand basic SQL queries, such as SELECT. 3. Master data operations: create tables, insert, update, and delete data. 4. Learn advanced skills: subquery and window functions. 5. Debugging and optimization: Check syntax, use indexes, avoid SELECT*, and use LIMIT.

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.

MySQL is an open source relational database management system that is widely used in Web development. Its key features include: 1. Supports multiple storage engines, such as InnoDB and MyISAM, suitable for different scenarios; 2. Provides master-slave replication functions to facilitate load balancing and data backup; 3. Improve query efficiency through query optimization and index use.

SQL is used to interact with MySQL database to realize data addition, deletion, modification, inspection and database design. 1) SQL performs data operations through SELECT, INSERT, UPDATE, DELETE statements; 2) Use CREATE, ALTER, DROP statements for database design and management; 3) Complex queries and data analysis are implemented through SQL to improve business decision-making efficiency.

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


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

Dreamweaver Mac version
Visual web development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software