0

我遇到了一种情况,我想从一个数据库表中获取不在其他数据库表中的数据。为此,我使用 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"
                           );

提前致谢

4

2 回答 2

1

LEFT JOIN像这样使用:

SELECT distinct ts.task_code t1
from TASKS as TS 
LEFT JOIN `project_tasks` pt ON ts.task_code = pt.task_code
LEFT JOIN `dept_project_tasks` dpt ON pt.task_code = dpt.task_code 
WHERE dpt.task_code IS NULL;

更新的 SQL Fiddle 演示

于 2012-11-20T11:00:22.153 回答
0

首先,你不需要带入project_tasks,只要有一个强制的外键

SELECT distinct task_code 
from TASKS as TS 
where TS.dept_code="dept1" 
AND    TS.task_code NOT IN (
    SELECT `dept_project_tasks`.task_code 
    FROM `dept_project_tasks` WHERE `dept_project_tasks`.dept_code = "dept1"
);

其次,您需要一个dept_project_tasks.dept_code包含 task_code 的索引。

于 2012-11-20T11:33:50.580 回答