Understanding B-tree indexes

Source: Internet
Author: User

Select and optimize Indexes

Understanding B-tree indexes (BANLANCE)

1. Create a table

Createtablecust (

Cust_idnumber,

Last_namevarchar2 (30 ),

First_namevarchar2 (30 ));

2. Determine that multiple SQL queries frequently use the last_name column in The where clause to create the following indexes:

Createindexindex_idx1oncust (last_name );

Insert data

Insertintocustvalues (7, 'acer', 'Scott ');

Insertintocustvalues (4, 'kan ', 'Jim ');

Insertintocustvalues (2, 'fan ', 'bob ');

Insertintocustvalues (9, 'star', 'Kim ');

...

Insertintocustvalues (117, 'acer', 'sid ');

3. Update table statistics to provide sufficient information for the query optimizer.

SQL> execdbms_stats.gather_table_stats (ownname => 'mlq', tabname => 'cust ', cascade => true );

Insert more data and query data from tables and indexes. There are three possible scenarios:

A. All data is indexed in the data block.

Index range scanning: Index quick and full scanning

Selectlast_namefromcustwherelast_name = 'acer'; range scan

Automatic Tracking:

Setautotraceon

Selectcount (last_name) fromcust;

Fast index scanning,

You can view the execution plan and statistical information to obtain more detailed results.

B. Not all information is included in the index.

Selectlast_name, first_namefromcustwherelast_name = 'acer ';

Access first_name through rownid

C. Only access table data blocks

Select * fromcust;

Full scan

The B-tree index is the default oracle index type, which is sufficient for most oltp.

It is very efficient and suitable to make queries much faster. If the index structure contains the column values to be retrieved,

You do not need to access table data blocks. To understand this principle, write an efficient index and determine the columns to create an index,

And determine whether the combined query is more efficient for some queries.

Estimate the space required by an index

Before creating an index, you can use the stored procedure to estimate the required space.

Setsererouton

Execdbms_stats.gather_table_stats (user, 'cust ');

Variableused_bytesnumber

Variablealloc_bytesnumber

Execdbms-space.create_index_cost ('createindexcust _ idx2oncust (first_name) ',: used_bytes,: alloc_bytes );)

Print: used_bytes

Print: alloc_bytes

View output results

This article is from the "mlqiang" blog, please be sure to keep this source http://162234.blog.51cto.com/152234/1302485

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.