【发布时间】:2021-08-23 07:32:28
【问题描述】:
excel大数据导入MySQL数据库耗时较长,如何提高性能?
Excel 数据如下:
sheet_student
| id | name | status | course_id |
|---|---|---|---|
| 1001 | alpha | 0 | C001 |
| 1002 | alpha | 1 | C002 |
| 1003 | alpha | 0 | C003 |
| ... | ... | ... | ... |
| 1501 | zip | 0 | C399 |
sheet_course
| course_id | course_code | course_qulity |
|---|---|---|
| C001 | computer science | 99 |
| C001 | computer vision | 86 |
| C001 | computer network | 87 |
| C001 | database | 91 |
| C002 | math | 92 |
| C002 | logical | 93 |
| C002 | ai | 94 |
| ... | ... | ... |
| C299 | computer vision | 94 |
MySQL 喜欢的表如下:
学生桌
| id | name | status | course_id |
|---|---|---|---|
| primary key | string | int | string |
CREATE TABLE IF NOT EXISTS `student`(
`id` INT UNSIGNED AUTO_INCREMENT,
`name` VARCHAR(100) NOT NULL,
`status` INT,
`course_id` INT NOT NULL,
PRIMARY KEY ( `id` )
)ENGINE=InnoDB DEFAULT CHARSET=utf8;
源表
| id | reference_id | course_code | course_qulity |
|---|---|---|---|
| primary key | reference for student table primary key | string | int |
CREATE TABLE IF NOT EXISTS `course`(
`id` INT UNSIGNED AUTO_INCREMENT,
`reference_id` INT UNSIGNED,
`course_code` VARCHAR(100) NOT NULL,
`course_qulity` INT NOT NULL,
PRIMARY KEY ( `id` )
)ENGINE=InnoDB DEFAULT CHARSET=utf8;
图像的浅灰色区域可能会有所改善,但我不知道如何优化它。
用inner join查询学生表和课程表是个不错的方法。但是insert和update操作不行。
当excel数据太大时,导入过程会很耗时。
更新
同一个excel多次导入时,MySQL的数据会根据excel数据进行更新。 student 表中的name 和course_id 字段确定行数据是否唯一。 course 表中的reference_id 和course_code 字段确定行数据是否唯一。
【问题讨论】:
-
提供紧凑的 Excel 示例数据(每表 3-5 行)、MySQL 初始数据(结构为 CREATE TABLE,如果导入前存在的数据可能会影响,则某些行为 INSERT INTO)和完整的最终此源数据导入后的 MySQL 表数据状态,并附有详细说明。
-
何不尝试一下,看看会发生什么?
-
您是否在 VBA 中使用循环从数据库中选择行来决定更新或插入?如果是这样,一个解决方案可能是创建两个表 student_new_data 和 course_new_data ,导入完整的 Excel 列表,然后使用 MySQL 的
INSERT ON DUPLICATE KEY UPDATE一次用于学生,一次用于课程以一步更新整个原始表。跨度> -
@ThorstenKettner 感谢您为我提供解决方案。
标签: mysql sql database-performance