[英]In SQL DB2, how would I check if a list of values in one table is available in another table?
I have two tables.我有两张桌子。 One is a comparatively small table with 20000 records with columns with id(Unique key),name, zipcode.一个是一个相对较小的表,有 20000 条记录,其列具有 id(唯一键)、名称、邮政编码。 And the other is a huge table with nearly 1 billion+ records with columns id(unique key), name, age, address & active status(boolean).另一个是一个巨大的表,包含近 10 亿条记录,列 id(唯一键)、名称、年龄、地址和活动状态(布尔值)。 I want to have the records which are not active in the second table and check if those inactive records are available in the first table.我想要在第二个表中不活动的记录,并检查这些不活动的记录是否在第一个表中可用。 I don't know how to loop the records in the first table as a single query.我不知道如何将第一个表中的记录作为单个查询循环。 How can I do that in Db2?我怎么能在 Db2 中做到这一点?
You may use EXISTS
logic here:您可以在这里使用EXISTS
逻辑:
SELECT t1.*
FROM Table1 t1
WHERE EXISTS (SELECT 1 FROM Table2 t2 WHERE t2.id = t1.id AND t2.status = false);
Note that the above query might benefit from the following index on the second table:请注意,上述查询可能会受益于第二个表上的以下索引:
CREATE INDEX idx2 ON Table2 (id, status);
This might let the lookup proceed much faster.这可能会让查找进行得更快。 Note that we chose to express your logic by scanning the first table, and looking up in the second, as the first table is much smaller than the second.请注意,我们选择通过扫描第一个表并在第二个表中查找来表达您的逻辑,因为第一个表比第二个小得多。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.