1

我有两张像亲子一样的桌子。在 Parent 表中,有 7211 条记录。在 Child 中,有 169498 条记录。这两个表之间没有关系(表示没有设置外键,而是使用了parentID)。在 Child 表中,有多余的记录和缺少的记录。

作为一个样本,

Parent Table - tblParent
PID     PName
A001    John
B002    Mary
...     ...

Child Table  - tblChild
PID   TID   Desc
A001  056   Book
Y004  087   Pen
...   ...   ...

我的查询如下所示,

SELECT PID
FROM tblParent
WHERE PID NOT IN
(
SELECT PID
FROM tblChild
)

以实际编号运行它。记录中,MS Access 2000 突然停止。如果我用 10 条记录测试它,它可以正常工作。主要原因是什么?没有记录?

我尝试另一种方式。

SELECT C.PID, P.PID
FROM tblChild C, tblParent P
WHERE C.PID <> P.PID

这时,乘法结果出来了。(我的意思是一个带有所有 P.PID 的 C.PID,然后等等......)

在 Access 2000 中,如何在最短的执行时间内获得额外和缺失的记录?

4

2 回答 2

1

Firstly, you do have an index on the PID column? It should be the primary key in tblParent and a non-unique index in tblChild. Setting the foreign key relationship would have created these indexes for you (I believe).

You could reduce the number of records that you need to match on. If you were to run this SQL:

SELECT DISTINCT PID
FROM tblChild
WHERE PID IN (
    SELECT PID
    FROM tblParent
)

You would only get the parent PID's that should be in the tblParent table. Then combining it like

SELECT PID
FROM tblParent
WHERE PID NOT IN
(
    SELECT DISTINCT PID
    FROM tblChild
    WHERE PID IN (
        SELECT PID
        FROM tblParent
    )
)

Should help speed up the query to find all parents with no children.

To find the extra children records, you could do

SELECT DISTINCT PID
FROM tblChild
WHERE PID NOT IN (
    SELECT PID
    FROM tblParent
)

Unfortunately, I don't have Access to test this out. I hope it helps

于 2011-03-03T10:48:14.310 回答
0

这是因为您在子查询中选择了 169 498 条记录:

SELECT PID
FROM tblChild

That's a lot. Whole tblChild table is possibly scanned, read from disk, PID column is separated and stored somewhere in the memory, all 169 498 values. Then, you read all 7211 records and check for each if it's not in the huge 169 498-sized dataset of child PIDs. That's a lot of work for Access.

于 2011-03-03T08:58:26.247 回答