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) deleting all records does not reserve the space occupied by the records
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) Delete space occupied by reserved records of all 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)
); Copy the code
2. Insert a data record.
Insert into users values ('001', 'gavindream', '000000'); 3. insert tens of thousands of data records using the copy insertion method.
Insert into users (userid, username, userpass) select * from users; I have inserted 4194304 pieces of data. It takes 90.964 seconds to delete data using delete, the data is inserted twice, but the time spent using truncate is only 2.215 seconds, as shown in: