oracle中的truncate与delelte
转自:http://hi.baidu.com/grace_king/blog/item/cb82930ad6249134b0351d6e.html
delete: Use the DELETE statement to remove rows from a table, a partitioned table, a view's base table, or a view's partitioned base table.
truncate:Use the TRUNCATE statement to remove all rows from a table or cluster. By default, Oracle also deallocates all space used by the removed rows except that specified by the MINEXTENTS storage parameter and sets the NEXT storage parameter to the size of the last extent removed from the segment by the truncation process.
truncate的功能可以拆分为:dropping + re-creating a table,但truncate的效率要高。因为truncate无须考虑表索引、约束、触发器等的重建。
另外truncate执行后,无法回滚(roll back)
HWM(high water mark,最高水位线):The high water mark is divides a segment into used blocks and free blocks Blocks below the high water mark (used blocks) have at least once contained data. This data might have been deleted. Since Oracle knows that blocks beyond the high water mark don't have data, it only reads blocks up to the high water mark in a full table scan(FTS).在oracle10g中增加了调整HWM的命令:alter table xxx shrink space。
以下网上达人总结的truncate与delete区别:
1、在功能上,truncate是清空一个表的内容,它相当于delete from table_name。
2、delete是dml操作,truncate是ddl操作;因此,用delete删除整个表的数据时,会产生大量的roolback,占用很多的rollback segments, 而truncate不会。
3、在内存中,用delete删除数据,表空间中其被删除数据的表占用的空间还在,便于以后的使用,另外它是“假相”的删除,相当于windows中用delete删除数据是把数据放到回收站中,还可以恢复,当然如果这个时候重新启动系统(OS或者RDBMS),它也就不能恢复了!
而用truncate清除数据,内存中表空间中其被删除数据的表占用的空间会被立即释放,相当于windows中用shift+delete删除数据,不能够恢复!
4、truncate 调整high water mark 而delete不;truncate之后,TABLE的HWM退回到 INITIAL和NEXT的位置(默认)delete 则不可以。
5、truncate 只能对TABLE,delete 可以是table,view,synonym。
6、TRU
相关文档:
ORACLE游标
游标:容器,存放当前SQL语句影响的记录
所有DML语句都会用到游标
逐行处理影响的行数
游标
静态游标:游标与SQL语句在运行前关联
&nb ......
有表A(字段A1,A2)和表B(字段B1,B2).
字段A2,B2上都有索引.
A,B 表联查
sql1 这个sql 非常快 2秒的样子
select * from A,B where A.A1=B.B1(+) and A2='值1'
sql2 这个sql 慢到让人无法忍受
select * from A,B where A.A1=B.B1(+) and B2='值1'
外联以后 表B上的索引不起作用了.
如果换成内联 速度很快.
sel ......
Oracle SQL PLUS 基本操作1
登录
c:\>sqlplus "sys/test1234 as sysdba" 以sysdba身份登录
c:\>sqlplus/nolog 以nolog身份登录
sql> connect sys/test1234 as sysdba
Connected.
启动
SQL> startup &nb ......
alter system kill session'50,492';
--以下几个为相关表
SELECT * from v$lock;
SELECT * from v$sqlarea;
SELECT * from v$session;
SELECT * from v$process ;
SELECT * from v$locked_object;
SELECT * from all_objects;
SELECT * from v$session_wait; ......