Oracle collects index statistics

Source: Internet
Author: User
I believe everyone is very familiar with the index structure. It is composed of roots, branches, and leaves. Of course, this refers to the common B + tree indexes. We still use Oracle to collect table and column statistics

I believe everyone is very familiar with the index structure. It is composed of roots, branches, and leaves. Of course, this refers to the common B + tree indexes. We still use Oracle to collect table and column statistics

I believe everyone is very familiar with the index structure. It is composed of roots, branches, and leaves. Of course, this refers to the common B + tree indexes. We still use the test table in Oracle collection table and column statistics (see. The following describes the index statistics.

I. How to query index statistics

User_ind_statistics is required to query index statistics. The following is a typical query statement.

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

The specific meanings of these columns are as follows:

① Blevel: that is, B-Tree level. For example, from root to branch to leaf, blevel is 2, but the index height is blevel + 1, that is, 3.

② Leaf_block: Number of leaf blocks in the index.

③ Distinct_keys: the total number of unique key values in the index.

④ Num_rows: number of key values in the index.

⑤ Clustering_factor: Clustering factor, which is used to represent the degree of sorting between indexes and data. The minimum value of this factor is the number of non-empty data blocks in the table, and the maximum value is the key number of the index. Next we will study how to calculate clustering factors.

Ii. How to calculate clustering Factors

The following is a script for calculating clustering factors,

Create or replace function clustering_factor (
P_owner IN VARCHAR2,
P_table_name IN VARCHAR2,
P_column_name IN VARCHAR2
) RETURN NUMBER IS
Rochelle cursor SYS_REFCURSOR;
Rochelle clustering_factor BINARY_INTEGER: = 0;
Rochelle block_nr BINARY_INTEGER: = 0;
Rochelle previus_block_nr BINARY_INTEGER: = 0;
Rochelle file_nr BINARY_INTEGER: = 0;
Rochelle previus_file_nr BINARY_INTEGER: = 0;
BEGIN
OPEN l_cursor
'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_previus_file_nr <> l_file_nr)
THEN
L_clustering_factor: = l_clustering_factor + 1;
End if;
Rochelle previus_block_nr: = Rochelle block_nr;
Rochelle previus_file_nr: = l_file_nr;
End loop;
CLOSE l_cursor;
RETURN l_clustering_factor;
END;
/

Some meanings of this function are described. First, a function is defined, which contains three parameters: Owner, table name, and column name. Several return value variables are also defined. A cursor is defined. The cursor returns the block number and file number of each record based on the input parameters. Traverse the cursor to extract the data block number and file number of each record. If the data block number is not the same as the previous data block number, or the file number is not the same as the previous one, then, the clustering factor increases by one.

The following describes the correctness of the algorithm,

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

It can be seen that the clustering factor calculated using the function in the script is exactly the same as the clustering_factor column in oracle.

Related reading:

Oracle collects statistics, causing indexes to be monitored

Oracle extended statistics

Oracle determines the expired statistics

Collection of Oracle 11g statistics

Collect statistics from Oracle

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.