创建、删除用户:
create user lgehr identified by lgehr default tablespace users quota 1000M on users;
grant create session, create table, create view to lgehr;
grant dba to lgehr;
drop user lgehr cascade;
创建、删除表空间:
CREATE TABLESPACE "LGEHR"
LOGGING
DATAFILE 'F:\TABLESPACE\LGEHR.dbf' SIZE 2000M
EXTENT MANAGEMENT LOCAL
SEGMENT SPACE MANAGEMENT AUTO;
drop tablespace "TB42" including contents and datafiles;
导出数据:exp tb/[email protected] file=data.dmp
导入数据:
imp user2/user2 file='E:\xxx\data.dmp' tablespaces=TBS fromuser=user1 touser=user2 ignore=y log='E:\TABLESPACE\a1.txt'
创建多记录测试表:
create table t3 as select rownum as id ,rownum+1 as id2,rpad('*',1000,'*') as contents from dual connect by level<=100;
trace报告设置:
SET AUTOTRACE OFF ---------------- 不生成AUTOTRACE 报告,这是缺省模式
SET AUTOTRACE ON EXPLAIN ------ AUTOTRACE只显示优化器执行路径报告
SET AUTOTRACE ON STATISTICS -- 只显示执行统计信息
SET AUTOTRACE ON ----------------- 包含执行计划和统计信息
SET AUTOTRACE TRACEONLY ------ 同set autotrace on,但是不显示查询输出
显示执行耗时:
set timing on
索引:
create table t as select * from dba_objects;
update t set object_id=rownum;
commit;
create index idx1_object_id on t(object_id);
set autotrace on
select count(*) from t; t表含有索引列object_id,但object_id列没有设置not null,查询不走索引 【因为索引不能存储空值】
select count(*) from t where object_id is not null; 语句中用了is not null过滤,查询走索引
select count(*) from t; 设置为非空列,查询走索引,alter table t modify OBJECT_ID not null;
行迁移:update的时候,如果当前块装不下update后的数据,就会把数据放到能够装得下的块,在原有块保留指向新块的地址
行链接:update的时候,如果找不到任何块装得下update后数据,就会把数据存放到由多个块组成的block chain中
行链接、行迁移弊端:insert、update性能降低,查询时增加IO次数
热点块:数据库的热点块,从简单了讲,就是极短的时间内对少量数据块进行了过于频繁的访问。
查询正在执行的SQL:
SELECT b.sid oracleID,
b.username 登录Oracle用户名,
b.serial#,
spid 操作系统ID,
paddr,
sql_text 正在执行的SQL,
b.machine 计算机名 ,
--alter system kill session '136,635'
'alter system kill session '''||b.sid||','||b.serial#||''';',
SQL_FULLTEXT
FROM v$process a, v$session b, v$sqlarea c
WHERE a.addr = b.paddr
AND b.sql_hash_value = c.hash_value
and sql_text like '%with TB_TT%'
查看执行计划:
explain plan for
SELECT * FROM (SELECT A.*, ROWNUM RN FROM (select * from DWI_MDM_PPU_RESIDENT_EXT a where a.end_time = date '4999-12-31' order by DW_UPDATE_TIME desc,card_no desc) A WHERE ROWNUM < 61) DVIEW WHERE RN >= 46;
select * from table(dbms_xplan.display());
表统计信息:
exec dbms_stats.gather_table_stats(ownname => 'lhdw',tabname => 'DWI_MDM_PPU_RESIDENT_EXT',estimate_percent => 10,method_opt=> 'for all indexed columns') ;
查看索引状态:
select * from user_indexes
where table_name = 'DWI_MDM_PPU_RESIDENT_EXT'
修改用户密码:
alter user sys identified by oracle;
设置linux环境变量:
export ORACLE_HOME=/u01/app/oracle/product/11.2.0
export ORACLE_SID=ORCL
export PATH=$PATH:${ORACLE_HOME}/bin
立即执行sql语句:
EXECUTE IMMEDIATE V_SQLSTR;
常用语句:
通过id关联,判断有无,有就更新,无则插入,效率比分别更新插入高:
merge into t_test t1
using t_test2 t2
on (t1.c_id = t2.c_id)
when matched then
update set
t1.c_name = t2.c_name,
t1.c_code = t2.c_name
when not matched then
insert values(t2.c_id, t2.c_name, t2.c_code)
得到分组内部排序:
select t1.c_id, t1.c_name, t1.c_code,
row_number() over(partition by t1.c_code order by t1.c_id desc)
from t_test t1
查看表空间及对应的数据文件信息
– select file_name,tablespace_name from dba_data_files;
– select file_name,tablespace_name from dba_temp_files;