When the data in the table is not required, delete the data and release the occupied space. To delete the data in the table, use the delete statement or the truncate statement.
I. Delete statements
(1) conditional Deletion
Syntax format:Delete [from] table_name [Where condition];
For example, delete data whose userid is '001' in the users table: delete from users where userid = '001 ';
(2) unconditionally Delete the entire table data
Syntax format:Delete table_name;
For example, delete all data in the User table: delete users;
Ii. truncate statement
The truncate statement is used to delete all records in the table.
Syntax format:Truncate [Table] table_name;
(1) DeleteSpace occupied by records not retained
Truncate [Table] table_name [drop Storage];
For example, deleting all data in the users table does not save space: truncate table users drop storage; because the drop storage keyword is used by default, the drop storage can be omitted;
(2) DeleteSpace occupied by reserved records
Truncate [Table] table_name [reuse Storage];
For example, delete all data in the users table and save the occupied space: truncate table users reuse storage;
Iii. Comparison of the two deletion statements
The delete statement deletes records one by one, while the truncate statement does not generate rollback information when deleting data; therefore, if you need to delete a large amount of data, using Delete will occupy a large amount of system resources, while using truncate will be much faster.
The following is an example:
1. Create a user table first:
Create Table users (userid varchar2 (20), username varchar2 (30), userpass varchar2 (30 ));
CopyCode
2. Insert a data record.
Insert into users values ('001', 'gavindream', '20180101 ');
3. Insert tens of thousands of data records using the copy and insert method.
Insert into users (userid, username, userpass) Select * from users;
I have inserted 4194304 pieces of data. It takes 90.964 seconds to delete the data using delete, and then twice as long as the data is inserted. However, it takes only 2.215 seconds to use truncate, as shown in:
From online: http://www.cr173.com/html/15011_1.html