Simple optimization of database

Source: Internet
Author: User
Tags one table

1. Efficient SQL statement design:


Typically, you can use the following methods to optimize the performance of SQL for data operations:
(1) Reduce the number of queries to the database, that is, reduce requests for system resources, using distributed database objects such as snapshots and graphs to reduce the number of queries to the database.
(2) Try to use the same or very similar SQL statements to query, so that not only take full advantage of the parsed syntax tree in the SQL shared pool, the likelihood of the data being queried to hit in the SGA will be greatly increased.
(3) Avoid the execution of SQL statements without any conditions. SQL statements that do not have any conditions are usually executed with FTS, the database locates a block of data first, and then sequentially finds other data in order, which is a lengthy process for large tables.
(4) If you have constraints on the data in some tables, it is best to implement the SQL statement in the table with the description integrity rather than the SQL program.

One, operator optimization:

1, in operator

The advantages of SQL in write are easier to write and easy to understand, which is more suitable for modern software development style. But SQL performance with in is always lower, and the steps taken from Oracle to parse SQL with in is the following differences from SQL without in:

Oracle attempts to convert it into a connection to multiple tables, and if the conversion is unsuccessful, it executes the subquery in the inside, then queries the outer table record, and if the conversion succeeds, it directly uses the connection method of multiple tables. This shows that using in SQL at least one more conversion process. General SQL can be converted successfully, but for the inclusion of grouping statistics and other aspects of SQL cannot be converted. Try not to use the in operator in a business-dense SQL.

When optimizing SQL, you often encounter the use of in statements, be sure to replace it with exists, because Oracle in the process of processing in is done by or, even if the use of the index is very slow.

2, not in operator

A strongly-listed recommendation is not used because it cannot apply an index to a table. Replace with not EXISTS or (outer join + empty) scheme

3, is null or NOT NULL operation

Determining whether a field is empty generally does not apply an index, because the B-tree index is not indexed by a null value.

Instead of using other operations of the same function, A is not null changed to A>0 or a> "and so on.

The field is not allowed to be empty, and a default value is used instead of a null value, such as an application where the Status field is not allowed to be empty, and the default is the request.

Avoid using is null on an indexed column and is not NULL to avoid using any nullable columns in the index, and Oracle will not be able to use that index. For single-column indexes, this record will not exist in the index if the column contains a null value. For composite indexes, this record does not exist in the index if each column is empty. If at least one column is not empty, the record exists in the index. For example, if a uniqueness index is established on column A and column B of a table, and the table has a A and a record of a A and a (123,null), ORACLE will not accept the next record (insert) with the same A, B value (123,null). However, if all the index columns are empty, Oracle will assume that the entire key value is empty and null is not equal to NULL. So you can insert 1000 records with the same key value, of course they are empty! Because null values do not exist in the index column, a null comparison of indexed columns in the WHERE clause causes Oracle to deactivate the index.

Inefficient: (Index invalidation)

SELECT ... From DEPARTMENT WHERE Dept_code isnotnull;

Efficient: (Index valid)

SELECT ... From DEPARTMENT WHERE Dept_code >=0;

4, > and < operator (greater than or less than operator)

The greater than or less than the operator generally does not need to adjust, because it has an index will be indexed to find, but in some cases it can be optimized, such as a table has 1 million records, a numeric field A, 300,000 records of a=0,30 Records of the A=1,39 million records of a=2,1 Records of the a=3. There is a big difference between performing a>2 and a>=3, because Oracle finds the index of records for 2 and then compares them, while A>=3 Oracle locates the records index of =3 directly.
replacing > with >=
Efficient:

SELECT ... From DEPARTMENT WHERE Dept_code >=0;

Low efficiency:

Select*from Empwhere DEPTNO >3

The difference between the two is that the former DBMS will jump directly to the first record that dept equals 4, and the latter will first locate the Dept no=3 record and scan forward to the first dept greater than 3.

5. Like operator:


The LIKE operator can apply a wildcard query, where the wildcard combination may reach almost arbitrary queries, but if used poorly it can produce performance problems, such as the "%5400%" query does not reference the index, and the "x5400%" reference to the scope index. A practical example: Use the user identification number behind the business number in the YW_YHJBQK table to inquire the business number yy_bh like '%5400% ' this condition will produce a full table scan if changed to yy_bh like ' x5400% ' OR yy_bh like ' b5400% '
The YY_BH index is used to make two-range queries, and the performance is definitely improved.

6. Replace distinct with exists:
Avoid using DISTINCT in the SELECT clause when submitting a query that contains one-to-many table information, such as a departmental table and an employee table. It is generally possible to consider replacing with exist, exists makes the query faster because the RDBMS core module will return the results immediately after the conditions of the subquery have been met.
Example:
(inefficient):

SelectDistinct dept_no,dept_namefrom DEPT D, EMP ewhere d.dept_no = e.dept_no

(efficient):

SELECT dept_no,dept_namefrom DEPT D whereexists
(SELECT ' X ' from EMP ewhere e.dept_no = d.dept_no);

Such as:
Replace in with exists instead of not exists instead of in:
In many base-table-based queries, it is often necessary to join another table in order to satisfy one condition. In this case, using EXISTS (or not EXISTS) will usually improve the efficiency of the query. In a subquery, the NOT IN clause performs an internal sort and merge. In either case, the not is the least effective (because it performs a full table traversal of the table in the subquery). To avoid using not, we can change it to an outer join (Outer Joins) or not EXISTS.
Example:
(efficient):

Select*from EMP (base table) WHERE EMPNO >0andexists
(SELECT ' X ' from Deptwhere DEPT. Deptno= EMP. DEPTNO and loc= ' Melb ')

(inefficient):

Select*from EMP (base table) WHERE EMPNO >0and Deptnoin
(SELECT DEP tnofrom DEPT WHERE LOC = ' Melb ')

7. Replace or with union (for indexed columns)


In general, replacing or in the WHERE clause with union will have a good effect. Using or on an indexed column causes a full table scan. Note that the above rules are valid only for multiple indexed columns. If a column is not indexed, the query efficiency may be reduced because you did not select or. In the following example, indexes are built on both loc_id and region.
(efficient):

SELECT loc_id,loc_desc,regionfrom location WHERE loc_id =10
Unionselect loc_id, Loc_desc, regionfrom location WHERE region = ' MELBOURNE '

(inefficient):

SELECT loc_id,loc_desc,regionfrom location WHERE loc_id= 10OR region = ' MELBOURNE '


If you persist in using or, you need to return the least logged index column to the front.

8. Replace or with in


This is a simple and easy-to-remember rule, but the actual execution effect has to be tested, and under Oracle8i, the execution path seems to be the same.
Low efficiency:

Select....from location WHERE loc_id =10or loc_id=20or loc_id=30

Efficient:

SELECT ... From location WHERE loc_in in (10,20,30);

II. structure optimization of SQL statements

1. Avoid using ' * ' in the SELECT clause:

2. Replace Delete with truncate:

Delete the full table record with truncate instead of Delete: (a table with a large amount of data is used in the secondary method)
When you delete a record in a table, in general, the rollback segment (rollback segments) is used to hold information that can be recovered. If you do not have a COMMIT transaction, Oracle restores the data to the state before it was deleted (to be exact, before the delete command was executed) and when the truncate is applied, the rollback segment no longer holds any recoverable information.

3. Replace the HAVING clause with a WHERE clause:


Avoid having a HAVING clause that filters the result set only after all records have been retrieved. This process requires sorting, totals, and so on. If you can limit the number of records through a WHERE clause, you can reduce this overhead. (Non-Oracle) on, where, have the three clauses that can be added conditionally, on is the first execution, where the second, having the last, because on is the non-qualifying records filtered before the statistics, it can reduce the intermediate operation to process the data, It's supposed to be the fastest speed, and where should be faster than having a.

4. SQL statements in uppercase

Because Oracle always parses SQL statements first, the lowercase letters are converted to uppercase and then executed.

5. Use the connector "+" connection string as little as possible in Java code!

6. Avoid changing the type of indexed columns:


Oracle automatically makes simple type conversions to columns when comparing data of different data types. Suppose Empno is an indexed column of a numeric type.

SELECT ... From EMP WHERE EMPNO = ' 123 ' Actually, after the Oracle type conversion, the statement translates to:

SELECT ... From EMP WHERE EMPNO = to_number (' 123 ')

Fortunately, the type conversion did not occur on the index column, and the purpose of the index was not changed. Now, suppose Emp_type is an indexed column of a character type.

SELECT ... From EMP WHERE Emp_type =123

This statement is translated by Oracle to:

SELECT ... From EMP Whereto_number (emp_type) =123

This index will not be used because of the type conversions that occur internally! To avoid the implicit type conversion of your SQL by Oracle, it is a good idea to explicitly show the type conversions. Note When comparing characters to numbers, Oracle takes precedence in converting numeric types to character types

7. Optimize GROUP BY:


The efficiency of the group BY statement can be improved by filtering out unwanted records before group by. The following two
The query returns the same result but the second one is significantly faster.
Low efficiency:

1SELECT Job,avg (SAL) from EMP GROUPby jobhaving job= ' president ' OR JOB = ' MANAGER '

Efficient:

1SELECT Job,avg (SAL) from EMP WHERE JOB = ' President ' OR job= ' MANAGER ' GROUPby job

Database optimization Scheme

1. Using Table partitioning

Partitioning separates data physically, and data from different partitions can be stored in data files on different disks. In this way, when querying this table, only need to scan the table partition, instead of full table scan, significantly shorten the query time, the other partition on different disks will be the data transfer to the table of different disk I/O, a well-provisioned partition can transfer data to disk i/ o The competition is evenly dispersed. You can take this approach when you have a large amount of data. Table partitions can be automatically built by month.

2. Use of aliases


Alias is a large database application technique, that is, the table name, column name in the query with a letter alias, query speed is 1.5 times times faster than the construction of the connection table.

3. Optimized design of indexed index

Indexes can greatly speed up the query speed of a database, which maps the logical values in a table to a secure rowid, so that the index can quickly locate the physical address of the data. When querying a large table with indexes, the index data may run out of all the block cache space, and Oracle has to read and write the disk frequently to get the data, so after partitioning a large table, you can create a partitioned index based on the corresponding partition. But personally, not all tables need to be indexed and indexed only for large data volumes.

Cons: First, it takes time to create indexes and maintain indexes, and this time increases as the amount of data increases. Second, the index needs to occupy the physical space, in addition to the data table to occupy the data space, each index also occupies a certain amount of physical space, if you want to establish a clustered index, then the space will be larger. Thirdly, when the data in the table is added, deleted and modified, the index should be maintained dynamically, thus reducing the maintenance speed of the data.

Indexes need to be maintained: in order to maintain the performance of the system, indexes must be maintained after the index has been created and the index pages have been broken due to frequent operations such as adding, deleting, modifying the data.

4. Adjust the hard disk I/O


This step was done before the development of the information system. The database administrator can place the data files that comprise the same tablespace on different hard disks to achieve I/O load balancing between the drives. The following principles should also be followed in cases where the disk is relatively wealthy:

separating tables and indexes;

Create user tablespace, separate disk from System tablespace;

Specify a different table space when creating tables and indexes;

Create a dedicated table space for rollback segments to prevent space competition from affecting the completion of the transaction;

Create temporary tablespace for sorting operations, as much as possible to prevent database fragmentation from being present in more than one table space.

In the process of using materialized views, we can basically "treat it as an actual data table" without worrying about the efficiency, optimization, etc. of the underlying table of the view itself.

Materialized view

1. For complex and high-consumption queries, if used frequently, the materialized view should be built

2. Materialized view is a typical performance optimization method with space-time change

3. Use materialized views for frequently updated tables

4. Select the appropriate refresh mode

The general view is virtual, and materialized views are real data regions that occupy storage space.

Of course, materialized views differ in the creation and management of views and in the general view. In contrast, materialized views occupy a certain amount of storage space, while the system refreshes materialized views also need to spend a certain amount of resources, but it is in exchange for efficiency and flexibility.
Reduce IO and network transmission times

1. Try to use fewer database requests, obtain the required data, can be taken out of the same time without multiple extraction

2. For bulk operations of frequently operating databases, use stored procedures to reduce unnecessary network transfers

Deadlock and blockage

1. For data that needs to be updated frequently, avoid putting it in long transactions, so as not to cause a chain reaction

2. It is not a necessity, it is best not to add your own lock outside of the Oracle lock mechanism

3. Reduce transaction size and commit transactions in a timely manner

4. Avoid cross-database distributed transactions as much as possible because of the complexity of the environment, which can easily lead to blocking

5. Use bitmap indexing with caution, which is prone to deadlock when updating

Automatically increase table partitioning:

The program can be performed as an Oracle job execution before 28th of each month (considering the reason for 28 days in February), automatically adding partitions to the partition table under that user.

Create or Replace procedure guan_add_partition
/*
/* Add partitions automatically for all partitioned tables under one user. The partition is listed as the date type, and the partition name is similar to the following: p200706.
/*create by David
*/
as
V_table_name varchar2 ();
V_partition_name VARCHAR2 (50);
V_month char (6);
V_add_month_1 char (6);
v_sql_string VARCHAR2 (2000);
V_add_month varchar2 (20);
Cursor Cur_part is select DISTINCT U.table_name,max (p.partition_name) max_part_name from User_tables u,user_tab_ Partitions p
where u.table_name=p.table_name and u.partitioned = ' YES '
Group by U.table_name;
Begin
Select To_char (sysdate, ' yyyymm ') to v_month from dual;
Select To_char (Add_months (sysdate,1), ' yyyymm ') to v_add_month_1 from dual;
Select To_char (add_months (trunc (sysdate, ' mm '), 2), ' Yyyy-mm-dd ') into the v_add_month from dual;
Open Cur_part;
Loop
Fetch cur_part into v_table_name,v_partition_name;
Exit when Cur_part%notfound;
If To_number (substr (v_partition_name,2)) <=to_number (substr (v_month,1)) then

V_sql_string: = ' ALTER TABLE ' | | v_table_name| | ' Add Partition P ' | | v_add_month_1| |
' VALUES less THAN (to_date (' | | | v_add_month| | ', ' YYYY-MM-DD ') tablespace users ';
Execute immediate v_sql_string;
Else
Null
End If;
End Loop;
Close Cur_part;
End

Forget to reprint which big God's blog, if there is infringement, please contact me delete (this blog is only for personal study use)

Simple optimization of database

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.