zoukankan      html  css  js  c++  java
  • Oracle学习笔记12:oracle优化

    当数据库中的数据有很多时,查询的效率就会很低,因此为了提高查询效率,就会有个种的方法,这里主要写:

      1.索引

      2.表分区

      3.优化sql命令

    1.索引

    什么是索引?

    将数据库看作一本书的话,索引就是书的目录,通过索引来查询数据库,可以提高查询效率

    关于索引:https://www.cnblogs.com/fnng/archive/2012/10/10/2719221.html

    索引分类:

    1.默认索引:B树索引(二叉树索引)

    2.位图索引:针对列值特定的列(如性别只有男和女)、

    3.函数索引:在函数上创建索引

    创建索引:

    -- 创建索引
    create [unique|bitmap] index 索引名 on 表名(列名1,列名2..);

    举例:

    --创建唯一索引
    create unique index idx1 on emp(ename);
    --创建复合索引
    create index idx2 on emp(EMPNO,job,DEPTNO);
    --创建位图索引
    create bitmap index idx3 on emp(DEPTNO);
    --创建函数索引
    create index idx4 on EMP(upper(ENAME));
    -- 默认主键为索引
    -- 执行查询操作时,会默认使用条件中的列或group by中的列对应的索引

    索引就像是图书的目录,因此要现有内容,才能根据内容建立目录,因此索引创建也是一样,要表中有数据,才能根据数据建立索引,建立索引后, 查询时如果有索引会默认使用索引,执行查询效率会变高,但是相应的,执行更新操作后,索引也需要进行更新,就像在书中修改了内容,目录也要随着更改,因此使用索引会导致更新数据变慢,索引的更新,oracle数据库会自动进行更新。

    索引的使用原则:

    1.数据量少的表不适合建立索引

    2.数据量大的表,如果查询的数据量小于10%,建立索引

    3.为大部分不重复的列值建立索引

    4.重复少的建立B树索引,重复多的,建立位图索引

    5.包含空值,但是要经常进行非空查询的,建立索引

    6.不适合在CLOB和BLOB等大数据对象类型上建立索引

    7.经常只读操作的适合建立索引

    8.大部分需要更新操作的,少建立一些索引,提高更新效率

    2.表分区

    为什么使用表分区?

    使用分区功能能够将表、索引进一步细分为段,这些数据库对象的段叫做分区。

    使用分区的优点

    改善查询性能

    增强可用性

    维护方便

    均衡io

    如何分区

    1.范围分区

    2.列表分区

    3.散列分区

    4.索引分区

    -- 创建三个表空间
    create tablespace stu_table1
      datafile 'e:/orcl/stu_table1.dbf'
      size 20 m;
    create tablespace stu_table2
      datafile 'e:/orcl/stu_table2.dbf'
      size 20 m;
    create tablespace stu_table3
      datafile 'e:/orcl/stu_table3.dbf'
      size 20 m;
    -- 1.范围分区(range)
    create table student
    (
      id    number primary key,
      sname varchar2(20) not null,
      sex   number
    ) partition by range (id)
    (
      --   分区,分到不同的表空间
      partition part1 values less than (10000) tablespace stu_table1,
      partition part2 values less than (20000) tablespace stu_table2,
      partition part3 values less than (maxvalue ) tablespace stu_table3
    );
    -- 执行查询操作
    select * from student partition (part1) where id<5000;
    
    -- 2.列表分区(list)
    create table student1(
      id number(7) primary key ,
      sname varchar2(20) not null,
      city varchar2(20)
    )partition by list (city)(
    --   list 按列表分区
      partition part1 values('郑州'),
      partition part2 values('上海')
    );
    -- 查询上海的学生
    select * from student1 partition (part1);
    
    -- 3.散列分区
    create table student2(
      id number primary key ,
      sname varchar2(20) not null ,
      city varchar2(20)
    )partition by hash(sname) (
    --     分区
             partition part1,
             partition part2
      )
    
    --   4.索引分区,给分区创建索引
    -- 4.1本地索引分区
    create table student3
    (
      id    number ,
      sname varchar2(20) not null,
      sex   number
    ) partition by range (id)
    (
      --   分区,分到不同的表空间
      partition part1 values less than (10000) tablespace stu_table1,
      partition part2 values less than (20000) tablespace stu_table2,
      partition part3 values less than (maxvalue ) tablespace stu_table3
    );
    create index idx1 on student3(id)local (
      partition part1 tablespace stu_table1,
      partition part2 tablespace stu_table2,
      partition part3 tablespace stu_table3
    );
    -- 4.2全局索引分区
    -- 只适用于范围分区
    create table student4
    (
      id    number ,
      sname varchar2(20) not null,
      sex   number
    ) partition by range (id)
    (
      --   分区,分到不同的表空间
      partition part1 values less than (10000) tablespace stu_table1,
      partition part2 values less than (20000) tablespace stu_table2,
      partition part3 values less than (maxvalue ) tablespace stu_table3
    );
    -- 为范围分区创建全局索引
    create index idx2 on student4(id) global partition by range (id)(
      partition g1 values less than (10000),
      partition g2 values less than (20000),
      partition g3 values less than (maxvalue)
      );

    优化sql命令

    1.避免全表扫面

    2.避免or查询:尽量使用union all来串联结果集

    3.避免is null 判断:尽量为可以为空的列设置默认值

    4.避免使用<>或!=

    5.慎用in 和not in ,可以使用between and 来代替

    6.避免在where条件中使用函数查询,可以建立函数索引。

    7.建少模糊查询比如:like '%abc%'

    8.避免在where 条件中对字段使用表达式操作

    9.尽量使用数字类型字段

    10.任何时候都应该避免select * from table_name;

  • 相关阅读:
    Eclipse正确导入第三方project
    面试的基础_01字符串反向操作
    一个简单的实现了智能虚拟女友—图灵机器人
    Notepad++去除代码行号的几种方法
    fastjson将bean转成字符串时首字母变小写问题
    2015第34周二能收发邮件但不能打开网页解决方法
    2015第34周一
    2015第33周日
    2015第33周六
    构建自己的顾问团
  • 原文地址:https://www.cnblogs.com/Zs-book1/p/11240759.html
Copyright © 2011-2022 走看看