SQL statement performance adjustment (1)

Source: Internet
Author: User

I. INDEX usage Problems

1. Is INDEX used or not? This is a problem.

Whether full table scan or index range scan mainly considers the SQL query speed issue. Here we mainly focus on the number of records read. According to donald k. BURLESON, the principle of using index range scanning is:
For tables with raw data sorting, index range scanning should be used for queries that read less than 40% of the number of table records. A full table scan should be performed for queries that read more than 40% of the number of table records.
For unordered tables, index range scanning should be used for queries that read less than 7% of the table's records. Conversely, full table scanning should be performed for queries that read more than 7% of the table's records.
Note: In different books, the percentage values for reading records using indexes are not consistent. This is basically an empirical value. However, the lower the percentage of records read, the more effective the index is.

2. If an INDEX is created on a column, what SQL queries use indexes? What SQL queries do not use indexes?
The following SQL statement does not use indexes:
Implicit conversion of data types exists, for example:
Select * from staff_member where staff_id = '201312 ';
The column has mathematical operations, such:
Select * from staff_member where salary * 2 <10000;
Not equal to (<>), for example:
Select * from staff_member where dept_no <> 2001;
Use the substr string function, for example:
Select * from staff_member where substr (last_name, 1, 4) = 'fred ';
'%' Is the first character of the wildcard, for example:
Select * from staff_member where first_name like '% don ';
For example:
Select * from staff_member where first_name | '= 'donald'

3. Function Index

The date type is also very easy to use, and the to_char function is used in SQL statements to query specific range dates. For example, select * from staff_member where TO_CHAR (birth_day, 'yyyy') = '000000'; you can CREATE a function-based INDEX, for example, create index Ind_emp_birth ON staff_member (to_char (birth_day, 'yyyy '));
Ii. SQL statement sorting Optimization

1. Sorting:
SQL contains the group by clause
SQL contains the order by clause
SQL contains the distinct clause
SQL contains minus or union operations
When creating an index

2. Is the sorting performed in the memory or on the disk?

The sorting speed in the memory is 14000 times faster than that in the disk. For dedicated connections, the sorting memory is allocated according to the sort_area_size of INIT. ORA. For multi-threaded service connections, the sorting memory is allocated according to large_pool_size.

Increasing sort_area_size can reduce disk sorting, but the performance of ORACLE will be reduced because the connections used will be allocated to a memory of sort_area_size. To improve the query speed, A large amount of memory may be wasted. Increase the value of sort_multiblock_read_count to read more content each time, reduce the number of running times, and improve performance.

Iii. Adjustment of SQL subqueries

1. Understand associated subqueries and non-associated subqueries.
The following is a non-associated subquery:
Select staff_name from staff_member where staff_id
In (select staff_id from staff_func );
The following is an associated subquery:
Select staff_name from staff_member where staff_id in (select staff_id from staff_func where staff_member.staff_id = staff_func.staff_id );

The returned result sets are the same, but their execution overhead is different:
Overhead of non-join queries-when a non-join query is performed, the subquery is executed only once and the results are sorted and saved in a temporary ORACLE segment, each record is referenced by the parent Query when returned. When a subquery returns a large number of records, sorting these result sets and sorting temporary data segments will increase the system overhead.
Associated query overhead-for records returned to the parent query, the subquery is executed once per row. Therefore, we must ensure that the index is used in subqueries whenever possible.

2. XISTS clause and IN Clause
Correlated subqueries with IN are redundant because the IN clause provides the same functions as related operations IN subqueries. For example:
Select staff_name from staff_member where staff_id in (select staff_id from staff_func where staff_member.staff_id = staff_func.staff_id );
It is not appropriate to specify the EXISTS clause for a non-correlated subquery, because this will produce the flute card product. For example:
Select staff_name from staff_member where staff_id
Exists (select staff_id from staff_func );
Try NOT to use the not in clause. The MINUS clause is faster than the not in clause, although the MINUS clause requires two queries:
Select staff_name from staff_member where staff_id in (select staff_id from staff_member MINUS select staff_id from staff_func where func_id like '123 ');

3. Rewrite the subquery with a standard connection or embedded view whenever possible.


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.