程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 數據庫知識 >> MYSQL數據庫 >> MySQL綜合教程 >> mysql刪除超大表中的部分數據

mysql刪除超大表中的部分數據

編輯:MySQL綜合教程

mysql中刪除一般表數據我們會使用delete 或者truncate來清空表數據,但是如果碰到超大表時你會發現此方法有點困難了,下面我以一個mysql刪除超大表中的部分數據為示例給各位同學介紹介紹。

mysql普通刪除表

delete 語句的定義:

經常和數據庫打交道的孩子們,刪除數據的時候用的大多都是 delete 語句。現在讓我們來看一下 delete語句的定義。


DELETE [LOW_PRIORITY] [QUICK] [IGNORE] FROM tbl_name

[WHERE where_definition]

[ORDER BY ...]

[LIMIT row_count]


delete from friends where user_name = ‘simaopig’;

truncate 語句 TRUNCATE [TABLE] tbl_name

這裡簡單的給出個示例,我想刪除 friends 表中所有的記錄,bKjia.c0m可以使用如下語句:

truncate table friends;

但在我在刪除超大表時發現 delete是不行了,加索引也別想。mysql上delete加low_priorty,quick,ignore估計也幫助不大

看到mysql文檔有一種解決方案:http://dev.mysql.com/doc/refman/5.0/en/delete.html


If you are deleting many rows from a large table, you may www.bKjia.c0m exceed the lock table size for an InnoDB table. To avoid this problem, or simply to minimize the time that the table remains locked, the following strategy (which does not use Delete at all) might be helpful:

Select the rows not to be deleted into an empty table that has the same structure as the original table:

Insert INTO t_copy Select * FROM t Where ... ;
Use RENAME TABLE to atomically move the original table out of the way and rename the copy to the original name:

RENAME TABLE t TO t_old, t_copy TO t;
Drop the original table:

Drop TABLE t_old;

E文不好,簡單的翻譯下:

刪除達標上的多行數據時,innodb會超出lock table size的限制,最小化的減少鎖表的時間的方案是:

1選擇不需要刪除的數據,並把它們存在一張相同結構的空表裡

2重命名原始表,並給新表命名為原始表的原始表名

3刪掉原始表

總結一下就是,當時刪除大表的一部分數據時可以使用 見新表,拷貝數據,刪除舊表,重命名的方法。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved