数据库:Aurora数据库(MySQL)
架构:
Table 1 : Department (d_id, d_name)
Table 2 : Student_Department (s_id, d_id)
在存储过程中,我们首先插入部门详细信息,然后使用该ID,在循环中的 Student_Department 表中插入多个记录。
存储过程:
WHILE index < numOfStudents DO
...
INSERT INTO department (d_name)
VALUES (new_dep_name)
ON DUPLICATE KEY UPDATE d_name = new_dep_name;
SELECT d_id INTO dept_id FROM department WHERE d_name = new_dep_name;
INSERT INTO Student_Department (s_id, d_id)
VALUES (student_id, dept_id);
...
SET index = index + 1;
END WHILE;
有什么方法可以优化存储过程中的多个插入?