度量快速开发平台-专业、快速的软件定制快开平台

 找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
热搜: 部件 流程 SQL
查看: 2543|回复: 8

[分享] SQL 优化

[复制链接]

235

主题

2547

帖子

5834

积分

论坛元老

Rank: 8Rank: 8

积分
5834
发表于 2020-3-19 18:07:33 | 显示全部楼层 |阅读模式
语句

不走索引而是表扫描的字句
  • "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(列名)


  • 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)


回复

使用道具 举报

235

主题

2547

帖子

5834

积分

论坛元老

Rank: 8Rank: 8

积分
5834
 楼主| 发表于 2020-3-20 15:04:06 | 显示全部楼层
回复 支持 反对

使用道具 举报

235

主题

2547

帖子

5834

积分

论坛元老

Rank: 8Rank: 8

积分
5834
 楼主| 发表于 2020-3-20 15:04:43 | 显示全部楼层
回复 支持 反对

使用道具 举报

235

主题

2547

帖子

5834

积分

论坛元老

Rank: 8Rank: 8

积分
5834
 楼主| 发表于 2020-3-20 15:08:21 | 显示全部楼层
回复 支持 反对

使用道具 举报

235

主题

2547

帖子

5834

积分

论坛元老

Rank: 8Rank: 8

积分
5834
 楼主| 发表于 2020-3-20 15:08:53 | 显示全部楼层
回复 支持 反对

使用道具 举报

235

主题

2547

帖子

5834

积分

论坛元老

Rank: 8Rank: 8

积分
5834
 楼主| 发表于 2020-3-20 15:09:13 | 显示全部楼层
回复 支持 反对

使用道具 举报

235

主题

2547

帖子

5834

积分

论坛元老

Rank: 8Rank: 8

积分
5834
 楼主| 发表于 2020-3-20 15:09:50 | 显示全部楼层
回复 支持 反对

使用道具 举报

235

主题

2547

帖子

5834

积分

论坛元老

Rank: 8Rank: 8

积分
5834
 楼主| 发表于 2020-3-23 17:49:02 | 显示全部楼层
回复 支持 反对

使用道具 举报

235

主题

2547

帖子

5834

积分

论坛元老

Rank: 8Rank: 8

积分
5834
 楼主| 发表于 2020-3-23 17:49:44 | 显示全部楼层
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|重庆度量科技  本站关键词:快速开发平台

GMT+8, 2024-3-29 16:24 , Processed in 0.143535 second(s), 25 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表