mysql INSERT表现糟糕

时间:2014-09-03 15:20:27

标签: mysql sql

我正在迭代我从csv文件获得的大约14,000条记录。 对于每个用户,我这样做:

1)我在users表中创建了一个用户记录 2)我将该用户的~21个属性插入users_attributes

最初每个用户需要大约2秒才能插入,但是一段时间后插入时间会增加到大约50秒......所以脚本需要永远...

为什么这么慢?

示例插入声明:

执行一次:

INSERT INTO users (uname, email, pass, passreminder, activated, approved_date, approved_by, user_regdate, lastlogin, theme, ublockon, ublock, tz, locale) VALUES ('someid_example.com', 'someid@example.com', 'test', 'User-generated', -32768, '2014-09-03 14:41:50', 2, '2014-09-03 14:41:50', '1970-01-01 00:00:00', '', 0, '', '', '');

重复21次:

INSERT INTO users_attributes (name, value, user_id) VALUES ('user_information_address', 'some address', 9862);

架构:

CREATE TABLE `users` (
    `uid` int(11) NOT NULL AUTO_INCREMENT,
    `uname` varchar(25) COLLATE utf8_unicode_ci NOT NULL,
    `email` varchar(60) COLLATE utf8_unicode_ci NOT NULL,
    `pass` varchar(138) COLLATE utf8_unicode_ci NOT NULL,
    `passreminder` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
    `activated` smallint(6) NOT NULL,
    `approved_date` datetime NOT NULL,
    `approved_by` int(11) NOT NULL,
    `user_regdate` datetime NOT NULL,
    `lastlogin` datetime NOT NULL,
    `theme` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
    `ublockon` smallint(6) NOT NULL,
    `ublock` longtext COLLATE utf8_unicode_ci NOT NULL,
    `tz` varchar(30) COLLATE utf8_unicode_ci NOT NULL,
    `locale` varchar(5) COLLATE utf8_unicode_ci NOT NULL,
    PRIMARY KEY (`uid`),user
    KEY `uname` (`uname`),
    KEY `email` (`email`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci

CREATE TABLE `users_attributes` (
    `user_id` INT(11) NOT NULL,
    `name` VARCHAR(80) COLLATE utf8_unicode_ci NOT NULL,
    `value` longtext COLLATE utf8_unicode_ci NOT NULL,
    PRIMARY KEY (`user_id`,`name`),
    KEY `IDX_SOMENUM` (`user_id`),
    CONSTRAINT `IDX_SOMENUM` FOREIGN KEY (`user_id`) REFERENCES `users` (`uid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci

0 个答案:

没有答案