-- 语法 MERGEINTO [target-table] A USING [source-tablesql] B ON([conditional expression] and [...]...) WHEN MATCHED THEN [UPDATEsql] WHENNOT MATCHED THEN [INSERTsql]
时间函数
1 2 3 4 5
--获取系统时间 select sysdate from dual;--2019/5/14 12:39:23 select to_char(sysdate,'yyyy-MM-dd HH24:mi:ss') from dual;--2019-05-14 12:39:24 --hh为12小时制,如果时间超过12会报错 SELECT to_date('2009-12-25 14:23:31','yyyy-mm-dd,hh:mi:ss') FROM dual;--报错
with as: 提取出重复数据,优化查询,with 必须紧跟引用的 select 语句,且创建的临时表必须被引用,否则报错
1 2 3 4 5 6 7
-- 语法 WITH <subquery_name> AS (<the aggregation SQL statement>) SELECT <query naming subquery_name>;
用于区分相同分组标准的分组统计结果。可通过 having group_id() < 1 来剔除重复的分组。
6. DBA 函数
1. 锁表与解锁
1. 查询被锁表
1 2 3 4 5 6 7 8 9 10 11
-- 查询锁表 select object_name ,machine ,sql_exec_start ,s.sid ,s.serial# from v$locked_object l ,dba_objects o ,v$session s where l.object_id = o.object_id and l.session_id=s.sid;
select table_name,tablespace_name,partitioned from user_tables whereuser='hello'; select*from all_tables where table_name='t_tmp_d';
2. 表大小
1 2 3 4 5 6 7 8 9 10 11 12 13 14
select t1.table_name,tablespace_name,partitioned,"Size(G)",MMX,MMI,cnt from( select table_name,tablespace_name,partitioned from user_tables whereuser='hello' ) t1 leftjoin ( select Owner,Segment_name, sum(bytes) /1024/1024/1024as "Size(G)" From Dba_segments Where tablespace_name like'hello_data' groupby Owner,Segment_name ) t2 on t1.table_name=t2.Segment_name LEFTJOIN(select table_name,max(partition_name) AS MMX,min(partition_name) AS MMI,count(1) AS CNT from user_tab_partitions where table_name IN ( select table_name from user_tables whereuser='hello'and partitioned='YES'and partition_name notin ('P19000101','P99999999','P999999','P190001')) groupby table_name orderby table_name) t3 on t1.table_name=t3.table_name orderby table_name;
3. 查看表状态
1
select*from user_tab_partitions where tablespace_name='BOARD_DATA'and table_name='FACT_KB_FM_ANL_D';
4. 查看表最大,最小分区
1 2 3 4
select table_name,max(partition_name),min(partition_name),count(1) from user_tab_partitions where table_name IN ( select table_name from user_tables whereuser='BOARD'and partitioned='YES'and partition_name notin ('P19000101','P99999999','P999999','P190001')) groupby table_name orderby table_name;
5. 查询分区
1
select*from user_tab_partitions where table_name='TB_ALO_LOG_VISIT'orderby partition_name;
6. 表空间
1 2 3 4 5 6 7 8 9 10 11
select total.tablespace_name, round(total.GB,2) as Total_GB, round(total.GB-free.GB, 2) as Used_GB, round(free.GB, 2) as free_GB, round((1-free.GB/total.GB)*100, 2) as Used_Pct from (select tablespace_name, sum(bytes)/1024/1024/1024as GB from dba_free_space groupby tablespace_name) free, (select tablespace_name, sum(bytes)/1024/1024/1024as GB from dba_data_files groupby tablespace_name) total where free.tablespace_name=total.tablespace_name;
3. 表的操作记录
1. 首先查找表的操作记录
1
select*from v$sqlarea a where a.SQL_TEXT like'%TABLE_NAME%'orderby last_load_time desc;
2. 从上面的记录中找到 update 语句对应的 sql_id
1
select*from v$sqltext a,v$sqlarea b where a.SQL_ID=b.SQL_ID and b.SQL_ID in('cq53826tk4u3c','afftnrfhu5utk') orderby b.LAST_ACTIVE_TIME desc;
3. 从上面的记录中找到最新的sql操作记录,然后找到用户名和主机
1
select*from sys.v_$session l,sys.v_$sql s where s.SQL_ID='cq53826tk4u3c'and l.USERNAME isnotnull;
NAME TYPE VALUE ------------------------------------ ----------- ------------------------------ buffer_pool_recycle string db_recycle_cache_size big integer0 recyclebin string on
2. 查找回收站中表的 object_name
1
select object_name,original_name,partition_name,type,ts_name,createtime,droptime from recyclebin;
3. 撤回drop 操作
1 2
# 命令窗口,利用表的 object_name 恢复表 SQL> flashback table "BIN$qBqFu9FDVG3gU47CAgrjvA==$0" to before drop;