语句

   

不走索引而是表扫描的字句 

  • "Is null"
  • "<>", "!=", "!>", "!<", 
  • "Not", "Not exist", "Not in", "Not like"
  • "Like '%500'" (字符串前面有%的)

       

  • NOT IN会多次扫描表,使用EXISTS、NOT EXISTS ,IN , LEFT OUTER JOIN 来替代,特别是左连接,而Exists比IN更快,最慢的是NOT操作.如果列的值含有空,以前它的索引不起作用,现在2000的优化器能够处理了。相同的是IS NULL,"NOT", "NOT EXISTS", "NOT IN"能优化它,而"<>"等还是不能优化,用不到索引。

       

    优化的一个思路: 根据业务规则优化

  • 例如:

  • 从业务规则角度尽量避免使用distinct

  account_table中记录用户的不同状态, 想要列出table1中所有的用户name(可能重复)。

select distinct name from account_table

选择 所有name都存在且不会重复的一个状态(比如注册行为)来达到同样效果

select name from account_table where status = 1

 

  Union all + 变换条件 代替 Union

  根据业务规则,尝试把union中不互逆的条件改为互逆的条件

 

 

From 小表驱动大表
from
子句中写在最后的表(基础表 driving table)将被最先处理。
from
子句中包含多个表的情况下,你必须选择记录条数最少的表作为基础表。

如果有3个以上的表连接查询, 那就需要选择交叉表(intersection table)作为基础表,交叉表是指那个被其他表所引用的表。

   

不要在Where字句中的列名加函数

如Convert,substring等,如果必须用函数的时候,1. 创建计算列再创建索引来替代. 2. 变通写法:WHERE SUBSTRING(firstname,1,1) = 'm'改为WHERE firstname like 'm%'(索引扫描)。

将函数和列名分开,并且索引不能建得太多和太大。   

Where子句中的连接顺序

Oracle采用自下而上的顺序解析where子句,根据这个原理,表之间的连接必须写在其它where条件之前,那些可以过滤掉最大数量记录的条件必须写在where子句的末尾。 

  • like模糊查询

    select * from contact where username like %yue%'
    关键词%yue%,由于yue前面用到了"%",因此该查询必然走全表扫描,除非必要,否则不要在关键词前加%

       

    and 替代 between

    不同数据库中可能对between的处理不同,分拆成and两个条件能保证正确

  • 例如: where x_date between '2018-01-01' and  '2018-02-05' 

  •   ->    where x_date >=  '2018-01-01' and x_date <= '2018-02-05' 

  • In字句中 出现率高优先顺序

    IN后面值的列表中,将出现最频繁的值放在最前面,出现得最少的放在最后面,减少判断的次数 eg: id in (2008, 2006, 2007, 2009)

       

    or 替代 in

    a=1 or a=2 如果a=1, 那么a=2将不会被计算和处理

    a in(1,2) 如果编译器没有做优化则会先分解再判断时间会相对长. 如果编译器做了优化处理, 效率与or相当

       

    between 替代 in

    类似id 为int型或只包含整数值的情况

    select fields from table where id in (1,2,3,4)

    ==>

    select fields from table where id between 1 and 4

       

    union/union all 替代 or

    两个条件不互逆使用union,互逆使用union all

       

    -- union all 替代 or

       

    select fields from table where flag=4 or flag=9

    ==>

    select fields from table where flag=4

    union all

    select fields from table where flag=9

       

    -- union 替代 or

       

    select fields from table where category = 'new' or date = '2018-01-26'

    ==>

    select fields from table where category = 'new'

    union

    select fields from table where date = '2018-01-26'

     

       

       

     

    函数

    Count(*)

    count(1) count(*) count(列名)

    --  https://www.cnblogs.com/Caucasian/p/7041061.html


  • Select COUNT(*)
    的效率较低,尽量变通写法,而EXISTS.同时请注意区别: select count(Field of null) from Table select count(Field of NOT null) from Table 的返回值是不同的! 

    语句 1:

    where stt.date_of_settlement <= @ddt

    and stt.date_of_settlement >= khb.date_of_settlement

    and stt.date_of_settlement >= crm.date_of_begin

    and stt.date_of_settlement < crm.date_of_end

    and stt.date_of_settlement >= scrm.date_of_begin

    and stt.date_of_settlement < scrm.date_of_end

    改进 2

    where stt.date_of_settlement

    BETWEEN greast(khb.date_of_settlement, crm.date_of_begin, scrm.date_of_begin) and @ddt

    and stt.date_of_settlement < least(crm.date_of_begin, scrm.date_of_end)

       

内容来源于网络如有侵权请私信删除
你还没有登录,请先登录注册
  • 还没有人评论,欢迎说说您的想法!