我遇到了一种情况,我想从一个数据库表中获取不在其他数据库表中的数据。为此,我使用 NOT IN 子句。这适用于少量数据。当表有大量数据时,会花费大量时间。我检查了 2000 行,这需要 8 多分钟。我认为巨大的时间是因为 NOT IN 子句。请任何人建议尽快执行此操作的替代方法。
编辑:架构有很小的变化。在 dept_project_tasks 我有关联表的 ID。(我正在使用带有默认 ID 的 Rails)
DB和查询详细信息如下:(Sqlfiddle链接相同:点击这里。请不要在同一个sqlfiddle页面修改)
数据库 1:
create table TASKS(task_code VARCHAR(20), task_name VARCHAR(20), project_name VARCHAR(20),dept_code VARCHAR(20));
insert into TASKS(task_code, task_name, project_name, dept_code) VALUES("task1", "task1", "project1", "dept1");
insert into TASKS(task_code, task_name, project_name, dept_code) VALUES("task1", "task1", "project1", "dept2");
insert into TASKS(task_code, task_name, project_name, dept_code) VALUES("task2", "task2", "project2", "dept1");
insert into TASKS(task_code, task_name, project_name, dept_code) VALUES("task2", "task2", "project2", "dept3");
insert into TASKS(task_code, task_name, project_name, dept_code) VALUES("task3", "task3", "project3", "dept2");
insert into TASKS(task_code, task_name, project_name, dept_code) VALUES("task3", "task3", "project3", "dept1");
insert into TASKS(task_code, task_name, project_name, dept_code) VALUES("task4", "task4", "project4", "dept1");
insert into TASKS(task_code, task_name, project_name, dept_code) VALUES("task4", "task4", "project4", "dept3");
数据库 2:
create table depts(dept_code VARCHAR(20), dept_name VARCHAR(20));
create table project_tasks(task_code VARCHAR(20), task_name VARCHAR(20));
create table dept_project_tasks(dept_code VARCHAR(20), task_code VARCHAR(20));
insert into depts(dept_code, dept_name) values("dept1", "dept_one");
insert into depts(dept_code, dept_name) values("dept2", "dept_two");
insert into project_tasks(task_code, task_name) values("task1", "task1");
insert into project_tasks(task_code, task_name) values("task2", "task2");
insert into project_tasks(task_code, task_name) values("task3", "task3");
insert into dept_project_tasks(dept_code, task_code) values("dept1", "task1");
insert into dept_project_tasks(dept_code, task_code) values("dept2", "task1");
insert into dept_project_tasks(dept_code, task_code) values("dept1", "task2");
insert into dept_project_tasks(dept_code, task_code) values("dept3", "task2");
查询是:
SELECT distinct task_code
from TASKS as TS
where TS.dept_code="dept1"
AND TS.task_code NOT IN (SELECT `project_tasks`.task_code
FROM `project_tasks`
INNER JOIN `dept_project_tasks` ON `project_tasks`.task_code = `dept_project_tasks`.task_code
WHERE `dept_project_tasks`.dept_code = "dept1"
);
提前致谢