• 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取得是会话的当前日期和时间

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

    按天循环插入表中。

  • 相关阅读:
    Overloaded的方法是否可以改变返回值的类型
    parseXXX的用法
    java的类型转换问题。int a = 123456;short b = (short)a;System.out.println(b);为什么结果是-7616?
    UVA 10405 Longest Common Subsequence(简单DP)
    POJ 1001 Exponentiation(大数处理)
    POJ 2318 TOYS(计算几何)(二分)
    POJ 1265 Area (计算几何)(Pick定理)
    POJ 3371 Flesch Reading Ease (模拟题)
    POJ 3687 Labeling Balls(拓扑序列)
    POJ 1094 Sorting It All Out(拓扑序列)
  • 原文地址:https://www.cnblogs.com/zigewb/p/5108653.html
Copyright © 2020-2023  润新知