SQL database optimization Summary: Millions of database Optimization Solutions,

Source: Internet
Author: User
Tags website performance

SQL database optimization Summary: Millions of database Optimization Solutions,

There are many tutorials on SQL Optimization on the Internet, but they are messy. I have made some preparations recently, and I will share some of them with you. There are some errors and deficiencies. Please correct them.

(1) select the most efficient table name sequence (only valid in the rule-based seo/'target = '_ blank'> Optimizer ): the ORACLE parser processes the table names in the FROM clause in the order FROM right to left. The table written in the FROM clause (basic table driving table) will be processed first, when the FROM clause contains multiple tables, You must select the table with the least number of records as the base table. If more than three tables are connected for query, You need to select an intersection table as the base table, which is the table referenced by other tables.

(2) join order in the WHERE clause.: ORACLE uses the bottom-up sequence to parse the WHERE clause. According to this principle, the join between tables must be written before other WHERE conditions, the conditions that can filter out the maximum number of records must be written at the end of the WHERE clause.

(3) Avoid using '*' in the SELECT clause: during ORACLE parsing, '*' is converted to all column names in sequence, this task is completed by querying the data dictionary, which means it will take more time

(4) Reduce the number of visits to the get = '_ blank'> Database: ORACLE has performed a lot of work internally: parsing SQL statements, estimating index utilization, binding variables, read data blocks;

(5) In

SQL * Plus, SQL * Forms AND Pro * C

To add the retrieved data volume for each get = '_ blank'> database access. The recommended value is 200.

(6) use the DECODE function to reduce processing time: Use the DECODE function to avoid repeated scanning of the same record or joining the same table.

(7) simple integration, unrelated get = '_ blank'> database access: If you have a few simple get = '_ blank'> database query statements, you can integrate them into a query (even if there is no relationship between them)

(8) delete duplicate records: the most efficient method for deleting duplicate records (because ROWID is used) is shown in the following example:

DELETE FROM EMP E WHERE E.ROWID > (SELECT MIN(X.ROWID) FROM EMP X WHERE X.EMP_NO = E.EMP_NO);

(9) replace DELETE with TRUNCATE: When deleting records in a table, a rollback segment is usually used to store recoverable information. if you do not have a COMMIT transaction, ORACLE will recover the data to the State before the deletion (which is precisely the State before the deletion command is executed). When TRUNCATE is used, rollback segments no longer store any recoverable information. after the command is run, the data cannot be restored. therefore, few resources are called and the execution time is short. (The translator Press: TRUNCATE applies only to deleting the entire table, and TRUNCATE is DDL rather than DML)

(10) Use COMMIT as much as possible: as long as possible, use COMMIT as much as possible in the program. In this way, the program's performance is improved, and the demand will be reduced due to the resources released by COMMIT: resources released by COMMIT:. information used to restore data on the rollback segment. b. the lock obtained by the Program Statement c. space d in redo log buffer. ORACLE manages the internal costs of these three types of resources

(11) replace HAVING clause with the Where clause: avoid HAVING clause. HAVING filters the result set only after all records are retrieved. this process requires sorting, total, and other operations. if the WHERE clause can be used to limit the number of records, this overhead can be reduced. (in non-oracle) where on, where, and having can be added, on is the first statement to execute, where is the second clause, and having is the last clause, because on filters out records that do not meet the conditions before making statistics, it can reduce the data to be processed by intermediate operations. It is reasonable to say that the speed is the fastest, where should also be faster than having, because it performs sum only after filtering data, and on is used only when two tables are joined, so in a table, then we can compare where with having. In the case of single-Table query statistics, if the filter condition does not involve fields to be calculated, the results will be the same, but the where technology can be used, having cannot be used. In terms of speed, the latter must be slow. If a calculated field is involved, the value of this field is uncertain before calculation, according to the workflow written in the previous article, the where function time is completed before computing, and having works only after computing. In this case, the results are different. In multi-table join queries, on takes effect earlier than where. The system first combines multiple tables into a temporary table based on the join conditions between tables, then filters them by where, then computes them, and then filters them by having after calculation. It can be seen that to filter a condition to play a correct role, you must first understand when the condition should take effect, and then decide to put it there.

(12) Reduce table queries: in SQL statements containing subqueries, pay special attention to reducing table queries. Example:

 SELECT TAB_NAME FROM TABLES WHERE (TAB_NAME,DB_VER) = ( SELECT TAB_NAME,DB_VER FROM TAB_COLUMNS WHERE VERSION = 604)

(13) Improve SQL efficiency through internal functions. complex SQL statements tend to sacrifice execution efficiency. It is very meaningful to grasp the above method for solving problems using functions.

(14) use the table Alias (Alias): when connecting multiple tables in an SQL statement, use the table Alias and prefix the Alias on each Column. in this way, the parsing time can be reduced and the syntax errors caused by Column ambiguity can be reduced.

(15) Replace IN with EXISTS and not exists to replace not in: IN many basic table-based queries, to satisfy one condition, it is often necessary to join another table. in this case, using EXISTS (or not exists) usually improves the query efficiency. IN a subquery, the not in Clause executes an internal sorting and merging. IN either case, not in is the most inefficient (because it executes a full table traversal for the table IN the subquery ). to avoid the use of not in, we can rewrite it into an Outer join (Outer Joins) or not exists. example:

(Efficient) SELECT * from emp (basic table) where empno> 0 and exists (SELECT 'x' from dept where dept. DEPTNO = EMP. deptno and loc = 'melb') (inefficient) SELECT * from emp (basic table) where empno> 0 and deptno in (select deptno from dept where loc = 'melb ')

(16) Identifying 'inefficient execution' SQL statements: Although SQLseo/'target = '_ blank'> A variety of optimized graphical tools are emerging, however, writing your own SQL tools is always the best way to solve the problem:

SELECT EXECUTIONS , DISK_READS, BUFFER_GETS, ROUND((BUFFER_GETS-DISK_READS)/BUFFER_GETS,2) Hit_radio, ROUND(DISK_READS/EXECUTIONS,2) Reads_per_run, SQL_TEXT FROM V$SQLAREA WHERE EXECUTIONS>0 AND BUFFER_GETS > 0 AND (BUFFER_GETS-DISK_READS)/BUFFER_GETS < 0.8 ORDER BY 4 DESC;

(17) using indexes to improve efficiency: indexes are a conceptual part of a table to improve data retrieval efficiency. ORACLE uses a complex self-balancing B-tree structure. data Query by index is usually faster than full table scan. when ORACLE finds the optimal path for executing the query and Update statements, ORACLEseo/'target = '_ blank'> optimizer uses the index. using indexes when joining multiple tables can also improve efficiency. another advantage of using an index is that it provides uniqueness verification for the primary key .. For those LONG or long raw data types, You Can index almost all columns. generally, using indexes in large tables is particularly effective. of course, you will also find that using indexes to scan small tables can also improve efficiency. although the index can improve the query efficiency, we must pay attention to its cost. the index requires space for storage and regular maintenance. The index itself is also modified whenever a record is increased or decreased in the table or the index column is modified. this means that the INSERT, DELETE, and UPDATE operations for each record will pay four or five more disk I/O. because indexes require additional storage space and processing, unnecessary indexes will slow the query response time .. Regular index reconstruction is necessary .:

 ALTER INDEX REBUILD

(18) replace DISTINCT with EXISTS: when you submit a query that contains one-to-many table information (such as the Department table and employee table), avoid using DISTINCT in the SELECT clause. in general, you can consider replacing it with EXIST, and EXISTS makes the query more rapid, because the RDBMS core module will return the result immediately after the subquery conditions are met. example: (inefficient ):

Select distinct DEPT_NO, DEPT_NAME from dept d, emp e where d. DEPT_NO = E. DEPT_NO (efficient): SELECT DEPT_NO, DEPT_NAME from dept d where exists (SELECT 'x' from emp e where e. DEPT_NO = D. DEPT_NO );

(19) server/'target = '_ blank'> SQL statements are capitalized, because oracle always first parses server/'target = '_ blank'> SQL statements, convert lowercase letters into uppercase letters and then execute

(20) try to use the connector "+" to connect strings in java code!

(21) Avoid using NOT in the index column. We should avoid using NOT in the index column, NOT will have the same impact as using the function in the index column. when ORACLE Encounters "NOT", it stops using indexes and performs full table scanning.

(22) Avoid using computation on index columns. in the WHERE clause, if the index column is part of the function. seo/'target = '_ blank'> the optimizer uses full table scanning instead of using indexes.

Example: inefficiency:

 SELECT … FROM DEPT WHERE SAL * 12 > 25000; 

Efficient:

SELECT … FROM DEPT WHERE SAL > 25000/12;

(23) Replace with> => efficiency: SELECT * from emp where deptno> = 4 inefficiency: SELECT * from emp where deptno> 3. The difference between the two is, the former DBMS will directly jump to the first record whose DEPT is equal to 4, while the latter will first locate the record whose DEPTNO is = 3 and scan forward to the record whose first DEPT is greater than 3.

(24) replacing OR with UNION (applicable to index columns) usually, replacing OR with UNION in the WHERE clause will produce better results. using OR for index columns will scan the entire table. note that the preceding rules are only valid for multiple index columns. if a column is not indexed, the query efficiency may be reduced because you did not select OR. in the following example, both LOC_ID and REGION have indexes. efficient: SELECT LOC_ID, LOC_DESC, region from location where LOC_ID = 10 union select LOC_ID, LOC_DESC, region from location where region = "MELBOURNE" inefficient: SELECT LOC_ID, LOC_DESC, region from location where LOC_ID = 10 or region = "MELBOURNE" if you insist on using OR, you need to write at the beginning of the index column with the least records.

(25) using IN to replace OR is a simple and easy-to-remember rule, but the actual execution results must be tested. IN ORACLE8i, the execution paths of the two seem to be the same.

Inefficiency:

SELECT…. FROM LOCATION WHERE LOC_ID = 10 OR LOC_ID = 20 OR LOC_ID = 30 

Efficient

SELECT… FROM LOCATION WHERE LOC_IN IN (10,20,30);

(26) Avoid using is null and is not null in the index column to avoid using any columns that can be empty in the index. ORACLE will NOT be able to use this index. this record does not exist in the index if the column contains a null value. for a composite index, if each column is empty, this record does not exist in the index. if at least one column is not empty, the record is stored in the index. for example, if the unique index is created in column A and column B of the table, and the and B values of A record exist in the table are (123, null ), ORACLE will not accept the next record with the same A, B value (123, null) (insert ). however, if all index columns are empty, ORACLE considers the entire key value to be null, but null is not equal to null. therefore, you can insert 1000 records with the same key value. Of course, they are empty! Because the null value does not exist in the index column, the null value of the index column in The WHERE clause will make ORACLE disable the index. Inefficiency: (index failure)

SELECT... From department where DEPT_CODE is not null; efficient: (index valid) SELECT... From department where DEPT_CODE> = 0;

(27) always use the first column of the index: If the index is created on multiple columns, only when its first column (leading column) is referenced by the where clause, seo/'target = '_ blank'> the optimizer selects to use this index. this is also a simple and important rule. When only the second column of the index is referenced, the seo/'target = '_ blank'> optimizer uses a full table scan and ignores the index.

(28) replace UNION with UNION-ALL (if possible): When an SQL statement needs to UNION two query result sets, the two result sets are merged in the UNION-ALL method and sorted before the final result is output. if union all is used to replace UNION, sorting is unnecessary. the efficiency will be improved accordingly. note that union all will repeatedly output the same records in the two result sets. therefore, you still need to analyze the feasibility of using union all from the business needs. UNION sorts the result set. This operation uses SORT_AREA_SIZE memory. seo/'target = '_ blank'> Optimization for this memory is also very important. the following SQL can be used to query the consumption of sorting

Inefficiency:

SELECT ACCT_NUM, BALANCE_AMT FROM DEBIT_TRANSACTIONS WHERE TRAN_DATE = '31-DEC-95' UNION SELECT ACCT_NUM, BALANCE_AMT FROM DEBIT_TRANSACTIONS WHERE TRAN_DATE = '31-DEC-95' 

Efficient:

 SELECT ACCT_NUM, BALANCE_AMT FROM DEBIT_TRANSACTIONS WHERE TRAN_DATE = '31-DEC-95' UNION ALL SELECT ACCT_NUM, BALANCE_AMT FROM DEBIT_TRANSACTIONS WHERE TRAN_DATE = '31-DEC-95'

(29) replace order by with WHERE: The order by clause uses indexes only under two strict conditions. all columns in order by must be included in the same index and maintained in the ORDER of the index. all columns in order by must be defined as non-empty. the index used BY the WHERE clause and the index used in the order by clause cannot be tied together.

For example:

The DEPT table contains the following columns:

DEPT_CODE PK NOT NULL DEPT_DESC NOT NULL DEPT_TYPE NULL

Inefficiency: (indexes are not used)

 SELECT DEPT_CODE FROM DEPT ORDER BY DEPT_TYPE

Efficient: (using indexes)

 SELECT DEPT_CODE FROM DEPT WHERE DEPT_TYPE > 0

(30) Avoid changing the index column type. When comparing data of different data types, ORACLE automatically converts the column type. Suppose EMPNO is a numeric index column.

SELECT … FROM EMP WHERE EMPNO = ‘123' 

In fact, after ORACLE type conversion, the statement is converted:

SELECT … FROM EMP WHERE EMPNO = TO_NUMBER(‘123') 

Fortunately, the type conversion does not occur on the index column, and the purpose of the index is not changed. Now, assume that EMP_TYPE is a character-type index column.

SELECT … FROM EMP WHERE EMP_TYPE = 123 

This statement is converted:

SELECT … FROM EMP WHERETO_NUMBER(EMP_TYPE)=123 

This index will not be used because of internal type conversion! To avoid implicit type conversion for your SQL statements, it is best to explicitly convert the type conversion. Note that when comparing the character and value, ORACLE will first convert the value type to the character type.

(31) WHERE clause to be careful: The WHERE clause in some SELECT statements does not use indexes. Here are some examples. In the following example, (1 )'! = 'No index is used. remember, indexes only tell you what exists in the table, but not what does not exist in the table. (2) '|' is a character concatenation function. as with other functions, indexes are disabled. (3) '+' is a mathematical function. as with other mathematical functions, indexes are disabled. (4) The same index Columns cannot be compared with each other, which enables full table scan.

(32). if the number of records in a table with more than 30% data records is retrieved. using indexes will not significantly improve the efficiency. b. in certain cases, using indexes may be slower than full table scanning, but this is an order of magnitude difference. in general, using an index is several times or even several thousand times more than a full table scan!

(33) operations to avoid resource consumption:

SQL statement of DISTINCT, UNION, MINUS, INTERSECT, and ORDER

The SQL engine is started to execute the resource-consuming sorting (SORT) function. DISTINCT requires a sorting operation, while other operations require at least two sorting operations. generally, SQL statements with UNION, MINUS, and INTERSECT can be rewritten in other ways. if your get = '_ blank'> database SORT_AREA_SIZE is well configured, you can also consider using UNION, MINUS, and INTERSECT. After all, they are highly readable.

(34) seo/'target = '_ blank'> optimize group by: to improve the efficiency of group by statements, you can filter out unnecessary records before group. the following two queries return the same results, but the second one is much faster.

Inefficiency:

SELECT JOB , AVG(SAL) FROM EMP GROUP by JOB HAVING JOB = ‘PRESIDENT' OR JOB = ‘MANAGER' 

Efficient:

 SELECT JOB , AVG(SAL) FROM EMP WHERE JOB = ‘PRESIDENT' OR JOB = ‘MANAGER' GROUP by JOB

Articles you may be interested in:
  • Mysql database query optimizes mysql Efficiency
  • Optimization of my. ini in mysql database 2 GB memory for multi-site and multi-compression settings
  • Enable database cache dependency to optimize website performance
  • Experience in optimizing SQL Server databases with High Performance
  • Optimize the MySQL joint index and Where clause to improve Database Operation Efficiency
  • MySQL performance optimization tips help your database
  • Six tips for MySQL database performance optimization
  • Experience in optimizing mysql Databases
  • Mysql database optimization Summary (experiences)
  • Oracle SQL tuning database optimization steps (Graphic tutorial)
  • Summary of oracle Database SQL Optimization
  • SQL optimization solution for millions of databases
  • Eight methods to optimize the Mysql database
  • Mysql database optimization and restoration tool mysqlcheck details
  • Optimize Query statements in the MySQL database
  • MySQL database Optimization

Related Article

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.