2011-03-14 328 views
2
ALTER TABLE customers ADD split INT(1);

10 mil。记录...我执行了这个命令1小时,仍然加载..有什么办法让它完成得更快?mysql缓慢查询

+0

你目前在这个服务器上有什么负载? – wlk 2011-03-14 18:00:52

+0

另外,桌上有多少个索引......如果你有很多索引,它也必须重建所有的索引页面......一千万没有什么可以打喷嚏来首先改变表格,然后重新索引影响。 – DRapp 2011-03-14 18:17:00

+0

您正在使用哪种存储引擎? MyISAM数据? InnoDB的?您可以更改一些设置以使其更快,但它们取决于存储引擎。 – 2011-03-14 18:41:42

回答

1

可能不是代码..
关闭所有其他应用程序,确保没有其他任何东西占用你的CPU使用率,没有恶意软件?获得更快的电脑?
如果您可以告诉我们您正在使用的环境设置等,这将有所帮助。它可能是一些涉及你的网络,你的服务器等等的东西。

+1

有些东西告诉我,如果它是1000万行,它是具有大量流量的高容量服务器,或者是数据服务器的一部分一个非常便宜的公司,没有其他的事情发生。这看起来更像是一个案例“我需要更多的权力,Scotty!”。 – 2011-03-14 18:08:48

+0

哈哈!是的,也许,thx指出了:) – 2011-03-14 18:21:29

6

以下内容非常快,需要6分钟以上的1000万行,但示例表中的字段和索引数量少于生产表,因此如果您决定使用它,则需要花费更长的时间!

注意:该示例是在Windows操作系统上完成的,因此您必须更改路径名和\ r \ n以符合linux标准!

这里是我现有的表(InnoDB引擎OFC):

drop table if exists customers; 
create table customers 
(
customer_id int unsigned not null auto_increment primary key, 
name varchar(255) not null, 
country_id tinyint unsigned not null default 0, 
key (country_id) 
) 
engine=innodb; 

mysql> select count(*) from customers; 
+----------+ 
| count(*) | 
+----------+ 
| 10000000 | 
+----------+ 
1 row in set (1.78 sec) 

创建,其中包括您所需要的新的字段的表的新版本:

drop table if exists customers_new; 
create table customers_new 
(
customer_id int unsigned not null auto_increment primary key, 
name varchar(255) not null, 
country_id tinyint unsigned not null default 0, 
split tinyint not null default 0, 
key (country_id) 
) 
engine=innodb; 

出口数据从旧客户表中按PK订单转换为csv格式:

select * into outfile 'c:\\customers.dat' 
fields terminated by '|' optionally enclosed by '"' 
lines terminated by '\r\n' 
from customers order by customer_id; 

Query OK, 10000000 rows affected (17.39 sec) 

负荷customer.dat文件到新的客户表:

truncate table customers_new; 

set autocommit = 0; 

load data infile 'c:\\customers.dat' 
into table customers_new 
fields terminated by '|' optionally enclosed by '"' 
lines terminated by '\r\n' 
(
customer_id, 
name, 
country_id, 
@dummy -- represents the new split field 
) 
set 
name = nullif(name,''); 

commit; 

Query OK, 10000000 rows affected (6 min 0.14 sec) 

检查,看是否一切正常

select * from customers_new order by customer_id desc limit 1; 
+-------------+-------------------+------------+-------+ 
| customer_id | name    | country_id | split | 
+-------------+-------------------+------------+-------+ 
| 10000000 | customer 10000000 |  218 |  0 | 
+-------------+-------------------+------------+-------+ 
1 row in set (0.00 sec) 

insert into customers_new (name, country_id, split) values ('f00',1,1); 
Query OK, 1 row affected (0.07 sec) 

select * from customers_new order by customer_id desc limit 1; 
+-------------+------+------------+-------+ 
| customer_id | name | country_id | split | 
+-------------+------+------------+-------+ 
| 10000001 | f00 |   1 |  1 | 
+-------------+------+------------+-------+ 
1 row in set (0.00 sec) 

删除旧表,并重新命名新:

drop table customers; 
Query OK, 0 rows affected (0.18 sec) 

rename table customers_new to customers; 
Query OK, 0 rows affected (0.05 sec) 

select * from customers order by customer_id desc limit 1; 
+-------------+------+------------+-------+ 
| customer_id | name | country_id | split | 
+-------------+------+------------+-------+ 
| 10000001 | f00 |   1 |  1 | 
+-------------+------+------------+-------+ 
1 row in set (0.00 sec) 

这就是所有人!