为什么80%的码农都做不了架构师?>>>

PL/SQL is great, but like any programming language it is capable of being misused. This article highlights the common performance mistakes made when developing in PL/SQL, turning what should be an elegant solution into a resource hog. This is very much an overview, but each section includes links to the relevant articles on this site that discuss the topic in greater depth, including example code, so think of this more like a check-list of things to avoid, that will help you get the best performance from your PL/SQL.

  • Stop using PL/SQL when you could use SQL
  • Stop avoiding bulk binds
  • Stop using pass-by-value (NOCOPY)
  • Stop using the wrong data types
  • Quick Points

Stop using PL/SQL when you could use SQL

The first sentence in the first chapter of the PL/SQL documentation states the following.

"PL/SQL, the Oracle procedural extension of SQL, is a portable, high-performance transaction-processing language."

So PL/SQL is an extension to SQL, not a replacement for it. In the majority of cases, a pure SQL solution will perform better than one made up of a combination of SQL and PL/SQL. Remember, databases are designed to work with sets of data. As soon as you start to process data in a row-by-row (or slow-by-slow) manner, you are stopping the database from doing what it does best. With that in mind, a PL/SQL programmer should aim to be an expert in SQL that knows a bit of PL/SQL, rather than an expert in PL/SQL that knows a little bit of SQL.

SQL has evolved greatly over the last 20 years. The introduction of features like analytic functions and SQL/XML mean you can perform very complex tasks directly from SQL. The following points describe some of the common situations where people use PL/SQL when SQL would be more appropriate.

  • Stop using UTL_FILE to read text files if you can external tables. Using the UTL_FILE package to read data from flat files is very inefficient. Since Oracle 7 people have been using SQL*Loader to improve performance, but since Oracle 9i the recommended way to read data from flat files is to use external tables. Not only is is more efficient by default, but it is easy to read the data in parallel and allows preprocessor commands to do tasks like unzipping files on the fly before reading them. In many cases, your PL/SQL load process can be replaced by a single INSERT ... SELECT statement with the data sourced from an external table.

  • Stop writing PL/SQL merges if you can use the MERGE statement. Merging, or upserting, large amounts of data using PL/SQL is a terrible waste of resources. Instead you should use the MERGE statement to perform the action in a single DML statement. Not only is it quicker, but it looks simpler and is easily made to run in parallel.

  • Stop coding multitable insert manually. Why send multiple DML statements to the server when an action can be performed in a single multitable insert? Since Oracle 9i multitable inserts have provided a flexible way of reducing round-trips to the server.

  • Stop using bulk binds (FORALL) when you can use DML error logging (DBMS_ERRLOG) to trap failures in DML. By default, if a single row in a DML statement raises an exception, all the work done by that DML statement is rolled back. In the past this meant operations that were logically a single INSERT ... SELECT, UPDATE or DELETE statement affecting multiple rows had to be coded as a PL/SQL bulk operation using the FORALL ... SAVE EXCEPTIONS construct, for fear that a single exception would trash the whole process. Oracle 10g Release 2 introduced DML error logging, allowing us to revert back to using a single DML statement to replace the unnecessary bulk bind operation.

The thing to remember about all these points is they replace PL/SQL with DML. In addition to them being more efficient, provided the server has enough resources to cope with it, it is very easy to make them even faster on large operations by running them in parallel. Making PL/SQL run in parallel is considerably more difficult in comparison (see parallel-enabled pipelined table functions and DBMS_PARALLEL_EXECUTE).

Stop avoiding bulk binds

Having just told you to avoid bulk binds in favor of single DML statements, I'm now going to tell you to stop avoiding bulk binds where they are appropriate. If you are in a situation where a single DML statement is not possible and you need to process many rows individually, you should use bulk binds as they can often provide an order of magnitude performance improvement over conventional row-by-row processing in PL/SQL.

Bulk binds have been available since Oracle 8i, but it was the inclusion of record processing in bulk bind operations in Oracle 9i Release 2 that made them significantly easier to work with.

The BULK COLLECT clause allows you to pull multiple rows back into a collection. The FORALL construct allows you to bind all the data in a collection into a DML statement. In both cases, the performance improvements are achieved by reducing the number of context switches between PL/SQL and SQL that are associated with row-by-row processing.

Stop using pass-by-value (NOCOPY)

As the Oracle database and PL/SQL have matured it has become increasingly common to work with large objects (LOBs), collections and complex object types, such as XMLTYPE. When these large and complicated types are passed as OUT and IN OUT parameters to procedures and functions, the default pass-by-value processing of these parameters can represent a significant performance overhead.

The NOCOPY hint allows you to switch from the default pass-by-value to pass-by-reference, eliminating this overhead. In many cases, this can represent a significant performance improvement with virtually no effort.

Stop using the wrong data types

When you use the wrong data types, Oracle is forced to do an implicit conversion during assignments and comparisons, which represents an unnecessary overhead. In some cases this can lead to unexpected and dramatic issues, like preventing the optimizer from using an index or resulting in incorrect date conversions.

Oracle provide a variety of data types, many of which have dramatically difference performance characteristics. Nowhere is this more evident than with the performance of numeric data types.

Make sure you pick the appropriate data type for the job you are doing!

Quick Points

  • Stop doing index scans when you can use ROWIDs.
  • Stop using explicit cursors.
  • Stop putting your code in the wrong order. Take advantage of performance gains associated with short-circuit evaluation and logic/branch ordering in PL/SQL.
  • Stop doing intensive processing immediately if it is more appropriate to decouple it.
  • Stop calling PL/SQL functions in your SQL statements. If you must do it, make sure you use the most efficient manner possible.
  • Stop avoiding code instrumentation (DBMS_APPLICATION_INFO and DBMS_SESSION). It's a very quick way to identify problems.
  • Stop avoiding PL/SQL native compilation.
  • Stop avoiding conditional compilation where it is appropriate. The easiest way to improve the speed of doing something is to avoid doing it in the first place.
  • Stop reinventing the wheel. Oracle has many built-in packages, procedures and functions that will probably do the job much more efficiently than you will, so learn them and use them. You can also save time by using other people's code, like the Alexandria PL/SQL Utility Library.

Hope this helps. Regards Tim...

转载于:https://my.oschina.net/xwinie/blog/108242

PL/SQL: Stop Making the Same Performance Mistakes相关推荐

  1. PL/SQL异常处理(原创)

    Exception概述 Exception是一种PL/SQL标识符,当运行的PL/SQL块出现错误或警告,则会触发异常处理.为了提高程序的健壮性,可以在PL/SQL块中引入异常处理部分,进行捕捉异常, ...

  2. PL/SQL 基础( 上 )

    预备 ( PL/SQL 好处 ) Integration : 集成度高 ( server 和 application 中都有 , 例如 : Oracle Developer ) Improved pe ...

  3. PL/SQL详细介绍

    PL/SQL笔记 PL/SQL块中只能直接嵌入SELECT,DML(INSERT,UPDATE,DELETE)以及事务控制语句(COMMIT,ROLLBACK,SAVEPOINT),而不能直接嵌入DD ...

  4. oracle pl/sql 基础

    PL/SQL笔记 PL/SQL块中只能直接嵌入SELECT,DML(INSERT,UPDATE,DELETE)以及事务控制语句(COMMIT,ROLLBACK,SAVEPOINT),而不能直接嵌入DD ...

  5. 成都笔试——PL/SQL准备

    1. 英文邮件写作 先需要发一封邮件给客户,就一些需求的讨论确定一个会议时间,我们这边在周三下午和周五早上有空,讯问客户的空闲时间. Dear all, Good day! Thank you for ...

  6. PL/SQL基础(1):语法

    本篇是 Oracle基础小结 系列之一. 本篇目录 1.什么是PL/SQL? 2.PL/SQL基本结构 3.PL/SQL符号定义 4.PL/SQL数据类型 5.PL/SQL条件句法 6.PL/SQL循 ...

  7. PL/SQL Developer(解压版)连接64位的Oracle11g

    PL/SQL Developer(解压版)连接64位的Oracle11g 在Windows 64位系统上安装64位的Oracle数据库,但是没有对应的64位PL/SQL Developer,此时要用P ...

  8. oracle pl/sql 程序设计 历史笔记整理

    20131016 周三 oracle pl/sql 程序设计 第2章 创建并运行pl/sql代码 sqlplus yjkhecc/yjkhecc@10.85.23.92:1521/orcl 在java ...

  9. PL/SQL ——分页编程

    通过PL/SQL编程,编写分页存储过程.代码如下所示: 1 --PL/SQL开发编写分页代码 2 --创建包 3 create or replace package Page as 4 type te ...

最新文章

  1. NGLView 安装与配置-交互式分子结构和轨迹查看
  2. 基于SSM的餐饮工业化管理系统-计算机毕业设计
  3. 凡事多找找自己的原因_布袋除尘器灰斗积粉过多、堵灰该咋办?别急,从这8个方面找原因...
  4. php家检乘除,php通用检测函数集(转)_php
  5. 检索召回 技术综述!
  6. Core Data 学习笔记(二)被管理对象模型
  7. [Beta阶段]第十次Scrum Meeting
  8. 一篇文章搞懂BIM技术的要点和前景
  9. 我是如何纯靠技术在大学月入上万,收获人生第一个10W
  10. 2022强网杯web方向wp
  11. java实现图片压缩
  12. JQuery解析二维码
  13. 《HelloGitHub》第 61 期
  14. WES分析7-VCF
  15. 为什么顺丰快递就要比别的快递快,别的快递难道做不到吗?
  16. java json设置编码_java-JSON字符编码
  17. word不能保存, 不能另存, 保存按钮 另存菜单不可用 的解决方法
  18. 【小程序】中WXML的语法详解
  19. 计算机应用基础的操作,计算机应用基础操作(1)
  20. android 视频大小压缩,Android视频压缩(亲测有效)方便简单易用

热门文章

  1. ZooKeeper安装过程
  2. Spring中调用远程EJB的配置
  3. java poi 导出excel 数字有问题
  4. 说说你对http、https、http2.0的理解【前端每日一题-25】
  5. iOS学习笔记11-多线程入门
  6. 【海洋女神原创】谈谈静默安装
  7. java中对象的序列化和反序列化
  8. Qt中QtTableWidget的使用
  9. VB6 二维数组去重实现
  10. 【算法题1】上台阶问题