search
HomeDatabaseMysql Tutorialoracle数据库sql的优化总结

oracle数据库sql的优化总结

Jun 07, 2016 pm 04:19 PM
oracleoptimizationSummarizedatabase

自己对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
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
Explain the InnoDB Buffer Pool and its importance for performance.Explain the InnoDB Buffer Pool and its importance for performance.Apr 19, 2025 am 12:24 AM

InnoDBBufferPool reduces disk I/O by caching data and indexing pages, improving database performance. Its working principle includes: 1. Data reading: Read data from BufferPool; 2. Data writing: After modifying the data, write to BufferPool and refresh it to disk regularly; 3. Cache management: Use the LRU algorithm to manage cache pages; 4. Reading mechanism: Load adjacent data pages in advance. By sizing the BufferPool and using multiple instances, database performance can be optimized.

MySQL vs. Other Programming Languages: A ComparisonMySQL vs. Other Programming Languages: A ComparisonApr 19, 2025 am 12:22 AM

Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages ​​such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages ​​have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

Learning MySQL: A Step-by-Step Guide for New UsersLearning MySQL: A Step-by-Step Guide for New UsersApr 19, 2025 am 12:19 AM

MySQL is worth learning because it is a powerful open source database management system suitable for data storage, management and analysis. 1) MySQL is a relational database that uses SQL to operate data and is suitable for structured data management. 2) The SQL language is the key to interacting with MySQL and supports CRUD operations. 3) The working principle of MySQL includes client/server architecture, storage engine and query optimizer. 4) Basic usage includes creating databases and tables, and advanced usage involves joining tables using JOIN. 5) Common errors include syntax errors and permission issues, and debugging skills include checking syntax and using EXPLAIN commands. 6) Performance optimization involves the use of indexes, optimization of SQL statements and regular maintenance of databases.

MySQL: Essential Skills for Beginners to MasterMySQL: Essential Skills for Beginners to MasterApr 18, 2025 am 12:24 AM

MySQL is suitable for beginners to learn database skills. 1. Install MySQL server and client tools. 2. Understand basic SQL queries, such as SELECT. 3. Master data operations: create tables, insert, update, and delete data. 4. Learn advanced skills: subquery and window functions. 5. Debugging and optimization: Check syntax, use indexes, avoid SELECT*, and use LIMIT.

MySQL: Structured Data and Relational DatabasesMySQL: Structured Data and Relational DatabasesApr 18, 2025 am 12:22 AM

MySQL efficiently manages structured data through table structure and SQL query, and implements inter-table relationships through foreign keys. 1. Define the data format and type when creating a table. 2. Use foreign keys to establish relationships between tables. 3. Improve performance through indexing and query optimization. 4. Regularly backup and monitor databases to ensure data security and performance optimization.

MySQL: Key Features and Capabilities ExplainedMySQL: Key Features and Capabilities ExplainedApr 18, 2025 am 12:17 AM

MySQL is an open source relational database management system that is widely used in Web development. Its key features include: 1. Supports multiple storage engines, such as InnoDB and MyISAM, suitable for different scenarios; 2. Provides master-slave replication functions to facilitate load balancing and data backup; 3. Improve query efficiency through query optimization and index use.

The Purpose of SQL: Interacting with MySQL DatabasesThe Purpose of SQL: Interacting with MySQL DatabasesApr 18, 2025 am 12:12 AM

SQL is used to interact with MySQL database to realize data addition, deletion, modification, inspection and database design. 1) SQL performs data operations through SELECT, INSERT, UPDATE, DELETE statements; 2) Use CREATE, ALTER, DROP statements for database design and management; 3) Complex queries and data analysis are implemented through SQL to improve business decision-making efficiency.

MySQL for Beginners: Getting Started with Database ManagementMySQL for Beginners: Getting Started with Database ManagementApr 18, 2025 am 12:10 AM

The basic operations of MySQL include creating databases, tables, and using SQL to perform CRUD operations on data. 1. Create a database: CREATEDATABASEmy_first_db; 2. Create a table: CREATETABLEbooks(idINTAUTO_INCREMENTPRIMARYKEY, titleVARCHAR(100)NOTNULL, authorVARCHAR(100)NOTNULL, published_yearINT); 3. Insert data: INSERTINTObooks(title, author, published_year)VA

See all articles

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor