Home Database Mysql Tutorial Oracle中的SQL语句性能调整原则

Oracle中的SQL语句性能调整原则

Jun 07, 2016 pm 03:03 PM
oracle sql in principle performance statement Adjustment

欢迎进入Oracle社区论坛,与200万技术人员互动交流 >>进入 一、问题的提出 在应用系统开发初期,由于开发数据库数据比较少,对于查询SQL语句,复杂视图的的编写等体会不出SQL语句各种写法的性能优劣,但是如果将应用系统提交实际应用后,随着数据库中数据的

欢迎进入Oracle社区论坛,与200万技术人员互动交流 >>进入

    一、问题的提出
    在应用系统开发初期,由于开发数据库数据比较少,对于查询SQL语句,复杂视图的的编写等体会不出SQL语句各种写法的性能优劣,但是如果将应用系统提交实际应用后,随着数据库中数据的增加,系统的响应速度就成为目前系统需要解决的最主要的问题之一。系统优化中一个很重要的方面就是SQL语句的优化。对于海量数据,劣质SQL语句和优质SQL语句之间的速度差别可以达到上百倍,可见对于一个系统不是简单地能实现其功能就可,而是要写出高质量的SQL语句,提高系统的可用性。
   
    在多数情况下,Oracle使用索引来更快地遍历表,优化器主要根据定义的索引来提高性能。但是,如果在SQL语句的where子句中写的SQL代码不合理,就会造成优化器删去索引而使用全表扫描,一般就这种SQL语句就是所谓的劣质SQL语句。在编写SQL语句时我们应清楚优化器根据何种原则来删除索引,这有助于写出高性能的SQL语句。
   
    二、SQL语句编写注意问题
    下面就某些SQL语句的where子句编写中需要注意的问题作详细介绍。在这些where子句中,即使某些列存在索引,但是由于编写了劣质的SQL,系统在运行该SQL语句时也不能使用该索引,而同样使用全表扫描,这就造成了响应速度的极大降低。
   
    1. IS NULL 与 IS NOT NULL
    不能用null作索引,任何包含null值的列都将不会被包含在索引中。即使索引有多列这样的情况下,只要这些列中有一列含有null,该列就会从索引中排除。也就是说如果某列存在空值,即使对该列建索引也不会提高性能。
   
    任何在where子句中使用is null或is not null的语句优化器是不允许使用索引的。
   
    2. 联接列
   
    对于有联接的列,即使最后的联接值为一个静态值,优化器是不会使用索引的。我们一起来看一个例子,假定有一个职工表(employee),对于一个职工的姓和名分成两列存放(FIRST_NAME和LAST_NAME),现在要查询一个叫比尔.克林顿(Bill Cliton)的职工。
   
    下面是一个采用联接查询的SQL语句, 
    

<p></p><p>
<span>    </span><span>select</span><span> </span><span>*</span><span> </span><span>from</span><span> employss    </span><span>where</span><span>    first_name</span><span>||</span><span>''</span><span>||</span><span>last_name </span><span>=</span><span>'</span><span>Beill Cliton</span><span>'</span><span>; </span></p>
Copy after login

     
    上面这条语句完全可以查询出是否有Bill Cliton这个员工,但是这里需要注意,系统优化器对基于last_name创建的索引没有使用。
   
    当采用下面这种SQL语句的编写,Oracle系统就可以采用基于last_name创建的索引。    

<p></p><p>
<span>    </span><span>Select</span><span> </span><span>*</span><span> </span><span>from</span><span> employee    </span><span>where</span><span>    first_name </span><span>=</span><span>'</span><span>Beill</span><span>'</span><span> </span><span>and</span><span> last_name </span><span>=</span><span>'</span><span>Cliton</span><span>'</span><span>; </span></p>
Copy after login

  
    遇到下面这种情况又如何处理呢?如果一个变量(name)中存放着Bill Cliton这个员工的姓名,对于这种情况我们又如何避免全程遍历,使用索引呢?可以使用一个函数,将变量name中的姓和名分开就可以了,但是有一点需要注意,这个函数是不能作用在索引列上。下面是SQL查询脚本: 

<p></p><p>
<span>    </span><span>select</span><span> </span><span>*</span><span> </span><span>from</span><span> employee    </span><span>where</span><span>    first_name </span><span>=</span><span> SUBSTR(</span><span>'</span><span>&&name</span><span>'</span><span>,</span><span>1</span><span>,INSTR(</span><br><span>'</span><span>&&name</span><span>'</span><span>,</span><span>'</span><span> </span><span>'</span><span>)</span><span>-</span><span>1</span><span>)    </span><span>and</span><span>    last_name </span><span>=</span><span> SUBSTR(</span><span>'</span><span>&&name</span><span>'</span><span>,INSTR(</span><span>'</span><span>&&name’,</span><span>'</span><span> </span><span>'</span><span>)+1) </span></p>
Copy after login

   

3. 带通配符(%)的like语句
   
    同样以上面的例子来看这种情况。目前的需求是这样的,要求在职工表中查询名字中包含cliton的人。可以采用如下的查询SQL语句:   

<p></p><p>
<span>select</span><span> </span><span>*</span><span> </span><span>from</span><span> employee </span><span>where</span><span> last_name </span><span>like</span><span> </span><span>'</span><span>%cliton%</span><span>'</span><span>; </span></p>
Copy after login


    
    这里由于通配符(%)在搜寻词首出现,所以Oracle系统不使用last_name的索引。在很多情况下可能无法避免这种情况,但是一定要心中有底,通配符如此使用会降低查询速度。然而当通配符出现在字符串其他位置时,优化器就能利用索引。在下面的查询中索引得到了使用:     

<p></p><p>
<span>select</span><span> </span><span>*</span><span> </span><span>from</span><span> employee </span><span>where</span><span> last_name </span><span>like</span><span> </span><span>'</span><span>c%</span><span>'</span><span>; </span></p>
Copy after login

    4. Order by语句
   
    ORDER BY语句决定了Oracle如何将返回的查询结果排序。Order by语句对要排序的列没有什么特别的限制,也可以将函数加入列中(象联接或者附加等)。任何在Order by语句的非索引项或者有计算表达式都将降低查询速度。
   
    仔细检查order by语句以找出非索引项或者表达式,它们会降低性能。解决这个问题的办法就是重写order by语句以使用索引,也可以为所使用的列建立另外一个索引,同时应绝对避免在order by子句中使用表达式。
   
    5. NOT
   
    我们在查询时经常在where子句使用一些逻辑表达式,如大于、小于、等于以及不等于等等,也可以使用and(与)、or(或)以及not(非)。NOT可用来对任何逻辑运算符号取反。下面是一个NOT子句的例子:
   
    ... where not (status ='VALID')
   
    如果要使用NOT,则应在取反的短语前面加上括号,并在短语前面加上NOT运算符。NOT运算符包含在另外一个逻辑运算符中,这就是不等于()运算符。换句话说,即使不在查询where子句中显式地加入NOT词,NOT仍在运算符中,见下例:
   
    ... where status 'INVALID';
   
    再看下面这个例子:
   
    select * from employee where salary3000;
   
    对这个查询,可以改写为不使用NOT:
   
    select * from employee where salary3000;
   
    虽然这两种查询的结果一样,但是第二种查询方案会比第一种查询方案更快些。第二种查询允许Oracle对salary列使用索引,而第一种查询则不能使用索引。
   
    6. IN和EXISTS
   
    有时候会将一列和一系列值相比较。最简单的办法就是在where子句中使用子查询。在where子句中可以使用两种格式的子查询。
   
    第一种格式是使用IN操作符:
   
    ... where column in(select * from ... where ...);
   
    第二种格式是使用EXIST操作符:
   
    ... where exists (select 'X' from ...where ...);
   
    我相信绝大多数人会使用第一种格式,因为它比较容易编写,而实际上第二种格式要远比第一种格式的效率高。在Oracle中可以几乎将所有的IN操作符子查询改写为使用EXISTS的子查询。
   
    第二种格式中,子查询以‘select 'X'开始。运用EXISTS子句不管子查询从表中抽取什么数据它只查看where子句。这样优化器就不必遍历整个表而仅根据索引就可完成工作(这里假定在where语句中使用的列存在索引)。相对于IN子句来说,EXISTS使用相连子查询,构造起来要比IN子查询困难一些。
   
    通过使用EXIST,Oracle系统会首先检查主查询,然后运行子查询直到它找到第一个匹配项,这就节省了时间。Oracle系统在执行IN子查询时,首先执行子查询,并将获得的结果列表存放在在一个加了索引的临时表中。在执行子查询之前,系统先将主查询挂起,待子查询执行完毕,存放在临时表中以后再执行主查询。这也就是使用EXISTS比使用IN通常查询速度快的原因。
   
    同时应尽可能使用NOT EXISTS来代替NOT IN,尽管二者都使用了NOT(不能使用索引而降低速度),NOT EXISTS要比NOT IN查询效率更高。

 

    另外一些提高表连接的查询速度的方法:

    1.在表连接查询时,常常用下列查询方法查询数据是: 

 

<p></p><p>
<span>    </span><span>SELECT</span><span> RECORDNO,NAME,AGE    </span><span>FROM</span><span> 表1    </span><span>WHERE</span><span> 表1.RECORDNO </span><span>NOT</span><span> </span><span>IN</span><br><span>    (</span><span>SELECT</span><span> RECORDNO    </span><span>FROM</span><span> 表2    </span><span>WHERE</span><span> BIRTHDAY</span><span>=</span><span>’</span><span>710618</span><span>’); </span></p>
Copy after login

 

    笔者发现,如果表1的长度为6000条记录,表2的长度为1000条记录, 则要4分钟才能出结果。原因是使用了比较运算符 NOT IN ,它的逻辑测试速度是最慢的。利用外连接替换NOT IN 运算符,查询时间则缩短为50秒。修改方法如下: 

 

<p></p><p>
<span>    </span><span>SELECT</span><span> RECORDNO,NAME,AGE    </span><span>FROM</span><span> 表1,表2    </span><span>WHERE</span><span> 表1.RECORDNO</span><span>=</span><span>表2.RECORDNO(</span><span>+</span><span>)<br>    </span><span>AND</span><span> 表2.RECORDNO </span><span>IS</span><span> </span><span>NULL</span><span>    </span><span>AND</span><span> 表2.BIRTHDAY(</span><span>+</span><span>)</span><span>=</span><span>’</span><span>710618</span><span>’;</span></p>
Copy after login

Oracle中的SQL语句性能调整原则

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 Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
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)

Performance comparison of different Java frameworks Performance comparison of different Java frameworks Jun 05, 2024 pm 07:14 PM

Performance comparison of different Java frameworks: REST API request processing: Vert.x is the best, with a request rate of 2 times SpringBoot and 3 times Dropwizard. Database query: SpringBoot's HibernateORM is better than Vert.x and Dropwizard's ORM. Caching operations: Vert.x's Hazelcast client is superior to SpringBoot and Dropwizard's caching mechanisms. Suitable framework: Choose according to application requirements. Vert.x is suitable for high-performance web services, SpringBoot is suitable for data-intensive applications, and Dropwizard is suitable for microservice architecture.

How to optimize the performance of multi-threaded programs in C++? How to optimize the performance of multi-threaded programs in C++? Jun 05, 2024 pm 02:04 PM

Effective techniques for optimizing C++ multi-threaded performance include limiting the number of threads to avoid resource contention. Use lightweight mutex locks to reduce contention. Optimize the scope of the lock and minimize the waiting time. Use lock-free data structures to improve concurrency. Avoid busy waiting and notify threads of resource availability through events.

Performance comparison of C++ with other languages Performance comparison of C++ with other languages Jun 01, 2024 pm 10:04 PM

When developing high-performance applications, C++ outperforms other languages, especially in micro-benchmarks. In macro benchmarks, the convenience and optimization mechanisms of other languages ​​such as Java and C# may perform better. In practical cases, C++ performs well in image processing, numerical calculations and game development, and its direct control of memory management and hardware access brings obvious performance advantages.

How good is the performance of random number generators in Golang? How good is the performance of random number generators in Golang? Jun 01, 2024 pm 09:15 PM

The best way to generate random numbers in Go depends on the level of security required by your application. Low security: Use the math/rand package to generate pseudo-random numbers, suitable for most applications. High security: Use the crypto/rand package to generate cryptographically secure random bytes, suitable for applications that require stronger randomness.

Performance comparison of Java frameworks Performance comparison of Java frameworks Jun 04, 2024 pm 03:56 PM

According to benchmarks, for small, high-performance applications, Quarkus (fast startup, low memory) or Micronaut (TechEmpower excellent) are ideal choices. SpringBoot is suitable for large, full-stack applications, but has slightly slower startup times and memory usage.

How to interact with JSON data using SQL in Golang? How to interact with JSON data using SQL in Golang? Jun 03, 2024 am 11:47 AM

There are the following steps for interacting with JSON data through SQL in Golang: Use the json.Unmarshal function to parse JSON data into a Go structure and convert JSON to a structure. Use the database/sql package to access and operate SQL databases and perform operations such as inserts and queries. Combining the above steps, you can build an application based on SQL and JSON in Go to implement functions such as user registration and login.

Redstone/RED currency listing price forecast and detailed explanation of token economics Redstone/RED currency listing price forecast and detailed explanation of token economics Mar 03, 2025 pm 10:42 PM

This time, the Redstone token $RED will be launched on Binance Launchpool on Binance TGE! This is also the first time Binance has launched a pre-market trading limit mechanism! The first day limit is 200%, and the ban will be lifted after 3 days to avoid "the peak will be achieved when the market opens"! Launchpool mechanism introduces the BinanceLaunchpool participating in Redstone that needs to pledge designated tokens (BNB, USDC, FDUSD) activity period is 48 hours: 08:00 UTC on February 26, 2025 to 08:00 UTC on February 28, 2025 ending this pre-market daily limit rule: 18:00 on February 28, 2025

How to do Oracle security settings on Debian How to do Oracle security settings on Debian Apr 02, 2025 am 07:48 AM

To strengthen the security of Oracle database on the Debian system, it requires many aspects to start. The following steps provide a framework for secure configuration: 1. Oracle database installation and initial configuration system preparation: Ensure that the Debian system has been updated to the latest version, the network configuration is correct, and all required software packages are installed. It is recommended to refer to official documents or reliable third-party resources for installation. Users and Groups: Create a dedicated Oracle user group (such as oinstall, dba, backupdba) and set appropriate permissions for it. 2. Security restrictions set resource restrictions: Edit /etc/security/limits.d/30-oracle.conf

See all articles