Home Database Mysql Tutorial oracle数据库sql的优化总结

oracle数据库sql的优化总结

Jun 07, 2016 pm 04:19 PM
oracle optimization Summarize database

自己对oracle sql的一些优化总结,自己也记录下来,也希望对大家有帮助: 一:使用where少使用having; 二:查两张以上表时,把记录少的放在右边; 三:减少对表的访问次数; 四:有where子查询时,子查询放在最前; 五:select语句中尽量避免使用*(执行时会把*

   自己对oracle sql的一些优化总结,自己也记录下来,也希望对大家有帮助:

  一:使用where少使用having;

  二:查两张以上表时,把记录少的放在右边;

  三:减少对表的访问次数;

  四:有where子查询时,子查询放在最前;

  五:select语句中尽量避免使用*(执行时会把*依次转换为列名);

  六:尽量多的使用commit;

  七:Decode可以避免重复扫描相同的记录或重复连接相同的表;

  八:通过内部函数也可提高sql效率;

  九:连接多个表时,使用别名并把别名前缀于每个字段上;

  十:用exists代替in

  十一:not exists代替 not in(not in 字句将执行一个内部的排序和合并,任何情况下,not in是最低效的,子查询中全表扫描了。为了避免使用not in,可以改写成outer joins或not exists);

  十二:表连接比exists更高效;

  十三:用exists替换distinct

  例:

  低:                                                                                                                                                         高:

  select distinct dept_no, dept_name                                                                                    select dept_no, dept_name

  from dept d, emp e                                                                                                                  from dept d

  where d.dept_no = e.dept_no;                                                                                               where exists (select 1 from emp e where e.dept_no = d.dept_no);

  十四:使用TKPROF工具来查询sql性能状态;

  十五:用索引提高效率(代价是:索引需要空间,而且定期重构索引很有必要:ALTER INDEX REBUILD

  先介绍下索引的原理,方便接下来对索引的优化的理解:

  通过索引找到rowid,然后通过rowid访问表。但如果查询的列包括在index中,将不在执行第二部操作,因为检索数据保存在索引中,单单访问索引就可以完全满足查询要求。

  前提提要:在十六例中,LODGING列有唯一索引;MANAGER列上有非唯一性索引。

  十六:索引范围查询(INDEX RANGE SACEN):

  适用于两种情况:

  1)基于一个范围的查询:

  SELECT LODGING FROM LODGING WHERE LODGING LIKE 'M%'

  (where字句条件包括一系列的值,oracle将通过索引范围查询方式查询LODGING_PK)

  2) 基于非唯一性索引的检索:

  SELECT LODGING FROM LODGING WHERE MANAGER = 'LI';

  (此查询分两步:LODGING$MANAGER的索引范围查询得到所有符合条件记录的rowid,然后通过rowid访问表得到LODGING列的值。该索引为非唯一性索引,数据库不能对它执行索引唯一扫描)

  where字句中,如果索引列所对应的值的第一个字符由通配符开始,索引将不被采用,而会全表扫描,如 SELECT..... WHERE MANAGER LIKE '%LI'

  十七:基础表的选择:

  基础表:最先访问的表(通常以全表扫描的方式被访问)。

  根据优化器的不同,SQL语句中基础表的选择是不一样的:

  如果使用CBO,,优化器会检查SQL语句中的每个表的物理大小,索引的状态,然后选用话费最低的路径。

  如果使用RBO,并且所有的连接条件都有索引对应,这种情况下基础表就是FROM字句中列在最后的表

  例:

  SELECT A.NAME, B.MANAGER FROM WOKER A, LODGING B WHERE A.LODGING = B.LODGING;

  由于LODGING列上有一个索引,而且WORKER表中没有相比较的索引,WORKER表将被作为查询基础表。

  十八:多个平等的索引:

  当SQL语句的执行路径可以使用分布在多个表上的多个索引时,oracle会同事使用多个索引并在运行时对它们的记录合并,检索仅对全部索引有效的记录。

  oracle选择执行路径是,唯一索引等级高于非唯一索引,只有当where字句中索引列和常量比较才有效。如果索引列和其它表的索引列相比较,这种字句在优化器中等级非常低;

  如果不同表中两个相同等级的索引将被引用,根据FROM字句中表的顺序决定哪个先被使用。FROM字句中最后的表索引优先级高。如果相同表中两个相同等级的索引将被引用,where字句中最先被引用的索引将有最高的优先级。

  例:DEPTNO上有非唯一性索引,EMP_CAT也有非唯一性索引

  SELECT ENAME FROM EMP WHERE DEPT_NO = 20 AND EMP_CAT = 'A';

  DEPTNO索引将被先检索,然后同EMP_CAT索引检索出的结果合并,执行路径如下:

  TABLE ACCESS BY ROWID ON EMP

  AND _EQUAL

  INDEX RANGE SCAN ON DEPT_IDX

  INDEX RANGE SCAN ON CAT_IDX

  十九:等式比较与范围比较:

  先上例子:

  SELECT ENAME FROM EMP WHERE DEPT_NO > 20 AND EMP_CAT = 'A';

  (在两个非唯一性索引前提下)此时范围索引不被使用,通过EMP_CAT索引查询出记录再与DEPT_NO条件进行比较

  注意:唯一性所以做范围比较时,等级要比非唯一性索引的等式比较低;

  二十:强制索引失效:

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

MySQL: An Introduction to the World's Most Popular Database MySQL: An Introduction to the World's Most Popular Database Apr 12, 2025 am 12:18 AM

MySQL is an open source relational database management system, mainly used to store and retrieve data quickly and reliably. Its working principle includes client requests, query resolution, execution of queries and return results. Examples of usage include creating tables, inserting and querying data, and advanced features such as JOIN operations. Common errors involve SQL syntax, data types, and permissions, and optimization suggestions include the use of indexes, optimized queries, and partitioning of tables.

Why Use MySQL? Benefits and Advantages Why Use MySQL? Benefits and Advantages Apr 12, 2025 am 12:17 AM

MySQL is chosen for its performance, reliability, ease of use, and community support. 1.MySQL provides efficient data storage and retrieval functions, supporting multiple data types and advanced query operations. 2. Adopt client-server architecture and multiple storage engines to support transaction and query optimization. 3. Easy to use, supports a variety of operating systems and programming languages. 4. Have strong community support and provide rich resources and solutions.

How to solve the problem of closing oracle cursor How to solve the problem of closing oracle cursor Apr 11, 2025 pm 10:18 PM

The method to solve the Oracle cursor closure problem includes: explicitly closing the cursor using the CLOSE statement. Declare the cursor in the FOR UPDATE clause so that it automatically closes after the scope is ended. Declare the cursor in the USING clause so that it automatically closes when the associated PL/SQL variable is closed. Use exception handling to ensure that the cursor is closed in any exception situation. Use the connection pool to automatically close the cursor. Disable automatic submission and delay cursor closing.

How to create cursors in oracle loop How to create cursors in oracle loop Apr 12, 2025 am 06:18 AM

In Oracle, the FOR LOOP loop can create cursors dynamically. The steps are: 1. Define the cursor type; 2. Create the loop; 3. Create the cursor dynamically; 4. Execute the cursor; 5. Close the cursor. Example: A cursor can be created cycle-by-circuit to display the names and salaries of the top 10 employees.

How to create oracle dynamic sql How to create oracle dynamic sql Apr 12, 2025 am 06:06 AM

SQL statements can be created and executed based on runtime input by using Oracle's dynamic SQL. The steps include: preparing an empty string variable to store dynamically generated SQL statements. Use the EXECUTE IMMEDIATE or PREPARE statement to compile and execute dynamic SQL statements. Use bind variable to pass user input or other dynamic values ​​to dynamic SQL. Use EXECUTE IMMEDIATE or EXECUTE to execute dynamic SQL statements.

What to do if the oracle can't be opened What to do if the oracle can't be opened Apr 11, 2025 pm 10:06 PM

Solutions to Oracle cannot be opened include: 1. Start the database service; 2. Start the listener; 3. Check port conflicts; 4. Set environment variables correctly; 5. Make sure the firewall or antivirus software does not block the connection; 6. Check whether the server is closed; 7. Use RMAN to recover corrupt files; 8. Check whether the TNS service name is correct; 9. Check network connection; 10. Reinstall Oracle software.

How to read the oracle awr report How to read the oracle awr report Apr 11, 2025 pm 09:45 PM

An AWR report is a report that displays database performance and activity snapshots. The interpretation steps include: identifying the date and time of the activity snapshot. View an overview of activities and resource consumption. Analyze session activities to find session types, resource consumption, and waiting events. Find potential performance bottlenecks such as slow SQL statements, resource contention, and I/O issues. View waiting events, identify and resolve them for performance. Analyze latch and memory usage patterns to identify memory issues that are causing performance issues.

How to use triggers for oracle How to use triggers for oracle Apr 11, 2025 pm 11:57 PM

Triggers in Oracle are stored procedures used to automatically perform operations after a specific event (insert, update, or delete). They are used in a variety of scenarios, including data verification, auditing, and data maintenance. When creating a trigger, you need to specify the trigger name, association table, trigger event, and trigger time. There are two types of triggers: the BEFORE trigger is fired before the operation, and the AFTER trigger is fired after the operation. For example, the BEFORE INSERT trigger ensures that the age column of the inserted row is not negative.

See all articles