我有以下 MySql 表,其中包含我的原始事件数据(大约 150 万行)
userId | pathId | other stuff....
我在 userId, pathId
上有一个索引(大约 50,000 个唯一组合)
在我的处理过程中,我确定了 30,000 个我不需要的 userId, pathId
值,但我确实想保留原始原始表。所以我想将所有行复制到已处理的事件表中,除了与这 30,000 个 userId, pathId
值匹配的行。
我正在考虑的一种方法是将我不想要的行的 30,000 个 userId,PathId
值写入一个 temp_table,然后执行如下操作:
[create table processed_table ...]
insert into processed_table
select * from raw_table r
where not exists (
select * from temp_table t where r.userId=t.userid and r.pathId=t.pathId
)
关于信息,processed_table
通常最终是 raw_table
大小的一半。
无论如何,这似乎可行,但我的 SQL 技能有限,所以我的问题(最后)是 - 这是最有效的方法吗?
最佳答案
不,这不是最有效的。 Source
That’s why the best way to search for missing values in MySQL is using a LEFT JOIN / IS NULL or NOT IN rather than NOT EXISTS.
这是一个 NOT IN
的例子:
INSERT INTO processed_table
SELECT *
FROM raw_table
WHERE (userId, pathId) NOT IN (
SELECT userId, pathId FROM temp_table
)
LEFT JOIN ... 为 NULL
:
INSERT INTO processed_table
SELECT *
FROM raw_table r
LEFT JOIN temp_table t
ON r.userId = t.userid AND r.pathId = t.pathId
WHERE t.userId IS NULL
但是,由于您的表非常小且只有 50,000 行,因此您的原始查询可能足够快。
关于mysql - 将行的子集从一个表复制到另一个表,过滤两列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7590387/