Know your SQL Optimizer

Source: Internet
Author: User

The proper use of the SQL optimizers can have a huge impact on the speed of SQL Execution. both the rule-based and cost-based optimizers have condition comings, and it is up to you to tune each SQL query to use the proper optimizer.

Rule-based rule comings-Often chooses the wrong index to retrieve rows. The "wrong" index may be one that is less selective than another index, causing additional I/O.

Cost-based metrics comings-Often performs unnecessary full tables scans, especially when more than 3 tables are being joined.

One of the first things the Oracle DBA looks at is the default optimizer mode for their database. there are two classes of optimizer modes the rule-based optimizer (RBO) and the cost-based optimizer (CBO ). the Oracle init. ora parameters offer four values for the optimizer_mode parameter.

Optimizer_mode = rule-The first, and oldest optimizer mode isRule. Under the rule-based optimizer, oracle uses heuristics from the data dictionary in order to determine the most alternative tive way to service to an oracle query and translate the declarative SQL command into an actual navigation plan to extract the data. in Memory pre-Oracle8i systems rule-based optimization is faster than cost-based. in fact, Oracle applications used Rule-Based Optimization until release 11i.

Optimizer_mode = first_rows-This is a cost-based optimizer mode that will return rows as soon as possible, even if the overall query runs longer or consumes more resources. the first_rows optimizer mode usually involves choosing a full-index scan over a parallel full-Table scan. because the first_rows mode favors index scans over full-Table scans, the first_rows mode is most appropriate for inline systems where end-user wants to see some results as quickly as possible.

Optimizer_mode = all_rows-This is a cost-based optimizer mode that ensures that the overall query time is minimized, even if it takes longer to receive the first row. this usually involves choosing a parallel full-Table scan over a full-index scan. because the all_rows mode favors full-Table scans, the all_rows mode is best suited for batch-oriented queries where intermediate rows are not required for viewing.

Optimizer_mode = choose-Causes optimizer to choose between rule-based and cost-based approaches.

Always set your driving table

The driving table is the table that is first used by Oracle in processing the query. The driving table shoshould always be the table in the query that returns the smallest number of rows.

The table order still makes a difference in execution time, even when using the cost-based optimizer.

The driving table is the table that will initiate the query and shoshould be the table with the smallest number of rows. ordering the tables in the from clause can make a huge difference in execution time.

Cost-based Optimization-The driving table is first after from clause-place smallest table first after from, and list tables from smallest to largest.

Rule-based Optimization-The driving table is last in from clause-place smallest table last in from clause, and list tables from largest to smallest.

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.