Home Database Mysql Tutorial oracle中怎么确定性能差的SQL语句

oracle中怎么确定性能差的SQL语句

Jun 07, 2016 pm 05:46 PM
oracle performance

oracle中怎么确定性能差的SQL语句,当涉及 SQL 时,性能不佳有两方面:CPU 密集型语句(CPU-intensive statements)和 I/O 密集型语句(I/O-intensive statements)。

oracle中怎么确定性能差的SQL语句,当涉及 SQL 时,性能不佳有两方面:CPU 密集型语句(CPU-intensive statements)和 I/O 密集型语句(I/O-intensive statements)。

前者很容易定位。所有的操作系统都可以让我们查看 CPU 密集型任务。这些任务可以追溯到一个特定用户,一个特定应用程序模块。 CPU 密集型模块一般都是由较差的代码和/或结构造成,而不是性能差的 SQL。一旦确定模块,你必须试图使之更有效率。一个可能的解决方案是将把某些处理移除程序,让数据库处理(高明点的 SQL,存储对象,内联函数,数组处理等)。

第二个是 I/O 密集型的 SQL 语句。这些语句会导致大量的数据库 I/O(全表扫描,排序,更新等),并以很高代价运行几个小时。从 Oracle 7 开始,解决了 SQL 识别问题。通过查询数据库共享池区域,我们可以很容易确定大多数 I/O 密集型 SQL 语句。

下面 SQL 语句演示了如何确定 I/O 命中率低于 80%的 SQL 语句。这个命中率是,自从 SQL 语句第一次被解析到共享池,通过所有执行的语句反应整体 I/O。下面可能是最近几分钟或几天的结果:

 代码如下 复制代码

sql> SELECT executions,

   2        disk_reads,

   3        buffer_gets,

   4        ROUND((buffer_gets - disk_reads) / buffer_gets, 2) hit_ratio,

   5        sql_text

   6     FROM   v$sqlarea

   7    WHERE  executions  > 0

   8     AND    buffer_gets > 0

   9     AND    (buffer_gets - disk_reads) / buffer_gets

   10   order by 4 desc ;

 

EXECUTIONS DISK_READS BUFFER_GETS  HIT_RATIO SQL_TEXT

---------- ---------- ----------- ---------- -----------------------------------------------------------------------

        16        180         369        .51 SELECT SKU,PREPACK_IND,CASE_ID,TRANSFER_QTY,UNIT_COST,UNIT_RETAIL,ROWID

                                             FROM TSF_DETAIL WHERE transfer = :1  order by sku

        16        30          63         .52 SELECT TRANSFER,TO_STORE,TO_WH FROM TSFHEAD  WHERE TRANSFER = :b1  AND

                                             TRANSFER_STATUS = 'A'

        2         3           7          .57 SELECT SKU   FROM UPC_EAN  WHERE UPC = :b1

        12        14          35         .60 SELECT SUBSTR(DESC_UP,1,30),DEPT,SYSTEM_IND   FROM DESC_LOOK  WHERE

                                             SKU = :b1

        14        13          35         .63 SELECT UNIT_COST,UNIT_RETAIL,SUBCLASS FROM WIN_SKUS WHERE SKU = :b1

事实上,我们发现对特定的 SQL,上面的数据有些误导,其实语句没有问题。考虑下面 v$sqlarea 输出:

Executions Disk_Reads Buffer_Gets Hit_Ratio Sql_Text

---------- ---------- ----------- --------- --------------------

    2          6          19         0.68   SELECT A.EMP_NO, ...

该语句的命中率很低,但事实上它很有效。因为,SQL 是通过 UNIQUE 索引操作的,物理磁盘读取的数量几乎与逻辑读取一样。UNIQUE 索引显著减少了整体的物理和逻辑磁盘 I/O 数量,导致了一个令人误解的低命中率。

下面例子,命中率很好。但是真的很好吗?

 代码如下 复制代码

Executions Disk_Reads Buffer_Gets Hit_Ratio Sql_Text

---------- ---------- ----------- --------- --------------------

    2         3625       178777      0.98   SELECT A.EMP_NO, ...

这个 SQL 语句看上去很有效。但是, 当我们仔细看时,事情就不是那么回事了。命中率并没有透露出,该语句存在五个表连接,并且每次执行进行了超过 3600 个物理磁盘读取。这是否太多了?是否有效?若不进一步研究,无法回答这两个问题。事实上,这个实例中,五个表的中其一个错误地执行了全表扫描。通过重新构造 SQL,我们可以减少物理磁盘 I/O 到小于 50,同时,也显著减少逻辑磁盘 I/O。巧合的是,命中率也下降到不到 70%。

我们首选 V$SQLAREA 查询是每个语句执行的物理磁盘 I/O 的真实报告。命中率是信息性的,但有时会产生误导。逻辑 I/O 相关的很少。如果语句执行 1,000,000 个逻辑 I/O,但只用了不到十分之一秒,这就没人在乎了。这是总的物理 I/O,几乎消耗了所有的时间,和确定潜在不正确的 SQL。例如:

 代码如下 复制代码

sql> SELECT sql_text, executions,

             ROUND(disk_reads / executions, 2) reads_per_run,

             disk_reads, buffer_gets,

             ROUND((buffer_gets - disk_reads)

                  / buffer_gets, 2) hit_ratio,

             sql_text

      FROM   v$sqlarea

      WHERE  executions  > 0

      AND    buffer_gets > 0

      AND    (buffer_gets - disk_reads) / buffer_gets

      ORDER by 3 desc ;

 

前两个语句会报告更具启发性的结果:

 代码如下 复制代码

Executions Reads_Per_Run Disk_Reads Buffer_Gets Hit_Ratio Sql_Text

---------- ------------- ---------- ----------- --------- ------------

    2           3            6          19        0.68    SELECT ...

    2         1812.5       3625       178777      0.98    SELECT ...

从视图 V$SQLAREA 中,我们可以立即隔离所有具有高物理读取的语句。这些语句可能并不一定低效或写得不好,但恰恰是它们需要进一步调查或调整。

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

Video Face Swap

Video Face Swap

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

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1269
29
C# Tutorial
1248
24
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 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.

What steps are required to configure CentOS in HDFS What steps are required to configure CentOS in HDFS Apr 14, 2025 pm 06:42 PM

Building a Hadoop Distributed File System (HDFS) on a CentOS system requires multiple steps. This article provides a brief configuration guide. 1. Prepare to install JDK in the early stage: Install JavaDevelopmentKit (JDK) on all nodes, and the version must be compatible with Hadoop. The installation package can be downloaded from the Oracle official website. Environment variable configuration: Edit /etc/profile file, set Java and Hadoop environment variables, so that the system can find the installation path of JDK and Hadoop. 2. Security configuration: SSH password-free login to generate SSH key: Use the ssh-keygen command on each node

What to do if the oracle log is full What to do if the oracle log is full Apr 12, 2025 am 06:09 AM

When Oracle log files are full, the following solutions can be adopted: 1) Clean old log files; 2) Increase the log file size; 3) Increase the log file group; 4) Set up automatic log management; 5) Reinitialize the database. Before implementing any solution, it is recommended to back up the database to prevent data loss.

Oracle's Role in the Business World Oracle's Role in the Business World Apr 23, 2025 am 12:01 AM

Oracle is not only a database company, but also a leader in cloud computing and ERP systems. 1. Oracle provides comprehensive solutions from database to cloud services and ERP systems. 2. OracleCloud challenges AWS and Azure, providing IaaS, PaaS and SaaS services. 3. Oracle's ERP systems such as E-BusinessSuite and FusionApplications help enterprises optimize operations.

How to export oracle view How to export oracle view Apr 12, 2025 am 06:15 AM

Oracle views can be exported through the EXP utility: Log in to the Oracle database. Start the EXP utility, specifying the view name and export directory. Enter export parameters, including target mode, file format, and tablespace. Start exporting. Verify the export using the impdp utility.

How to stop oracle database How to stop oracle database Apr 12, 2025 am 06:12 AM

To stop an Oracle database, perform the following steps: 1. Connect to the database; 2. Shutdown immediately; 3. Shutdown abort completely.

See all articles