1. select an appropriate Oracle optimizer
There are three optimizer types in Oracle:
A. RULE (based on Rules)
B. COST (COST-based)
C. CHOOSE (optional)
Set the default optimizer. You can declare the OPTIMIZER_MODE parameter in the init. ora file, as shown in figureRULE, COST, CHOOSE, ALL_ROWS, FIRST_ROWS. Of course, you also overwrite SQL statements or sessions.
To use the Cost-Based Optimizer (CBO, Cost-Based Optimizer), you must run the analyze command frequently to increase the accuracy of object statistics in the database.
If the optimizer mode of the database is set to CHOOSE, the actual optimizer mode is related to whether the analyze command has been run. If the table has been analyze, the optimizer mode will automatically become CBO. Otherwise, the database will adopt the RULE form optimizer.
By default, Oracle uses the CHOOSE optimizer. To avoid unnecessary full table scan, you must avoid using the CHOOSE optimizer, instead, we directly use a rule-based or cost-based optimizer.
2. Access Table: Oracle uses two methods to access Table records:
A. Full table Scan
Full table scan accesses each record in the table sequentially. Oracle optimizes full table scanning by reading multiple data blocks at a time.
B. Access the table through ROWID
You can use ROWID-based access to improve the efficiency of table access. ROWID contains the physical location information recorded in the table ...... Oracle uses indexes to connect data with the physical location where data is stored (ROWID. Generally, indexes provide a quick way to access ROWID. Therefore, queries based on index columns can improve the performance.
3. Share SQL statements
To avoid repeated parsing of the same SQL statement, Oracle stores the SQL statement in memory after the first parsing. The memory in the shared buffer pool of the SGA (system global area) can be shared by all database users. Therefore, when you execute an SQL statement (sometimes called a cursor), if it is exactly the same as the previously executed statement, oracle can quickly obtain parsed statements and the best execution path. This function greatly improves SQL Execution performance and saves memory usage.
Unfortunately, Oracle only provides cache buffering for simple tables. This function is not applicable to multi-table join queries.
The database administrator must set the appropriate parameters for this region in init. ora. When the memory area is larger, more statements can be reserved. Of course, the possibility of sharing is higher.
When you submit an SQL statement to Oracle, Oracle will first find the same statement in the memory.
It should be noted that Oracle adopts a strict match between the two. To achieve sharing, the SQL statements must be identical (including spaces and line breaks ).
The shared statement must meet three conditions:
A. Character-level comparison:
The statements currently executed must be the same as those in the shared pool.
For example:
SELECT * from emp;
Different from each of the following
SELECT * from EMP;Select * From Emp;SELECT * FROM EMP; |
B. The objects referred to by the two statements must be identical:
For example:
How to access user object names
Jack sal_limit private synonymWork_city public synonymPlant_detail public synonymJill sal_limit private synonymWork_city public synonymPlant_detail table owner |
Consider whether the following SQL statements can be shared between the two users.
Can SQL be shared?
Select max (sal_cap) from sal_limit;Each user cannot havePrivate synonym-sal_limit,They are different objects.
Select count (* 0 from work_city where sdesc like 'new % ';Two users can access the same objectPublic synonym-work_city
Select a. sdesc, B. location from work_city a, plant_detail B where a. city_id = B. city_idYou cannot use jack to access plant_detail through private synonym, and jill is the table owner with different objects.
C. bind variables must be used in the two SQL statements)
For example, the two SQL statements in the first group are the same (which can be shared), while the two statements in the second group are different (even when running, different bind variables are assigned the same value)
a. select pin ,name from people where pin = :blk1.pin;select pin ,name from people where pin = :blk1.pin; b.select pin ,name from people where pin = :blk1.ot_ind;select pin ,name from people where pin = :blk1.ov_ind; |
4. Select the most efficient table name sequence (only valid in the rule-based Optimizer)
The Oracle parser processes the table names in the FROM clause in the order FROM right to left. Therefore, the table written in the FROM clause (basic table driving table) is first processed. When the FROM clause contains multiple tables, You must select the table with the least number of records as the base table. When Oracle processes multiple tables, it uses sorting and merging to connect them. First, scan the first table (the last table in the FROM clause) and sort the records, and then scan the second table (the last second table in the FROM clause ), finally, all records retrieved from the second table are merged with the appropriate records in the first table.
For example:
Table TAB1 16,384 records
Table TAB2 1 record
Select TAB2 as the base table (the best method)
Select count (*) from tab1, tab2 execution time 0.96 seconds
Select TAB2 as the base table (poor method)
Select count (*) from tab2, tab1 execution time 26.09 seconds
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.
For example, the EMP table describes the intersection between the LOCATION table and the CATEGORY table.
SELECT *FROM LOCATION L ,CATEGORY C,EMP EWHERE E.EMP_NO BETWEEN 1000 AND 2000AND E.CAT_NO = C.CAT_NOAND E.LOCN = L.LOCN |
It will be more efficient than the following SQL statements
SELECT *FROM EMP E ,LOCATION L ,CATEGORY CWHERE E.CAT_NO = C.CAT_NOAND E.LOCN = L.LOCNAND E.EMP_NO BETWEEN 1000 AND 2000 |