CREATEDATABASEIFNOTEXISTS testdb;USE testdb;CREATETABLEbooks( id int auto_increment primary key, title varchar(255) not null)ENGINE=InnoDB;
接下来,使用以下存储过程填充books表的数据:
DELIMITER $$CREATEPROCEDURE load_book_data(IN num int(10))BEGINDECLAREcounterint(10) default0;DECLARE book_title varchar(255) default'';WHILEcounter< num DOSET book_title =concat('Book title #',counter);SETcounter=counter+1;INSERT INTO books(title) VALUES(book_title);ENDWHILE;END$$DELIMITER ;-- DROP PROCEDURE load_book_data;
然后,将10,000行数据插入到books表,执行上面语句将需要一段时间。
CALL load_book_data(10000);
执行上面语句之后,查看books表中的数据:
SELECT*FROM books;
最后,使用TRUNCATE TABLE语句来与DELETE语句相比执行的速度。
selectnow() as start_time;TRUNCATETABLE books;selectnow() as end_time;
上面语句执行结果,如下所示 -
mysql>selectnow() as start_time;TRUNCATETABLE books;selectnow() as end_time;+---------------------+| start_time |+---------------------+| 2017-07-2421:31:07 |+---------------------+1rowinsetQuery OK, 0rows affected+---------------------+| end_time |+---------------------+| 2017-07-2421:31:08 |+---------------------+1rowinset