zoukankan      html  css  js  c++  java
  • oracle查询优化,存储过程select表循环插入另一个表,以及索引重建

    查询语句pl/sql中用F5优化语句

    ORACLE的explain plan工具的作用只有一个,获取语句的执行计划
    1.语句本身并不执行,ORACLE根据优化器产生理论上的执行计划
    2.语句的分析结果存放在表PLAN TABLE中

    select * from TABLE
    where NOWTIME >=to_date('20160101','yyyy-mm-dd') and NOWTIME < to_date('20160102','yyyy-mm-dd')

    通过截图显示select语句是走索引的“INDEXRANGE SCAN”后边是索引名称,cost显示成本,走索引成本是很低的。

    如果没有“INDEXRANGE SCAN”,表之前是建有索引,说明索引失效,我们无需删掉再建立索引,只需要用以下语句重建索引,速度很快,即使表里有上千万数据。

    alter index index_name rebuild tablespace tablespace_name 加入表空间名,会将指定的索引移动到指定的表空间当中。

    索引在重建时,查询仍然可以使用旧索引。实际上,oracle在rebuild时,在创建新索引过程中,并不会删除旧索引,直到新索引rebuild成功。
      从这点可以知道rebuild比删除重建的一个好处是不会影响原有的SQL查询,但也正由于此,用rebuild方式建立索引需要相应表空间的空闲空间是删除重建方式的2倍。
    重建索引有多种方式

    存储过程:

    //////////////////////////////////////////

    create or replace procedure loop_while
    (
    start_date in date,
    end_date in date
    )
    is
    current_date_str date := start_date;

    current_date_str_end date;
    begin
    while current_date_str <=end_date
    loop
    current_date_str_end:=current_date_str+1;
    insert into TABLE1
    select * from TABLE
    where NOWTIME >=current_date_str and NOWTIME < current_date_str+1 ;
    commit;
    current_date_str:=current_date_str+1;
    end loop;
    end loop_while;

    /////////////////////////////////////////////////////////////////

    current_date是关键字,只会获取当前日期

    sysdate取的是服务器(主机)的当前日期和时间
    current_date取得是会话的当前日期和时间

    注意:一般情况下,二者相同。但如果修改了当前会话的时区,则会不同。

    按天循环插入表中。

  • 相关阅读:
    让你少奋斗10年的工作经验
    POJ Exponentiation解题
    数据结构树和二叉树
    语句摘录
    ACM解题报告格式
    编程规范
    数据结构图
    Java学习之二Java反射机制
    使用Python正则表达式提取搜索结果中的站点
    toj 1702 A Knight's Journey
  • 原文地址:https://www.cnblogs.com/zigewb/p/5108653.html
Copyright © 2011-2022 走看看