【问题标题】:copy subset of rows from one table to another, filtering on two columns将行的子集从一个表复制到另一个表,过滤两列
【发布时间】:2011-09-28 22:25:04
【问题描述】:

我有以下 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 技能有限,所以我的问题(最后)是 - 这是最有效的方法吗?

【问题讨论】:

    标签: mysql sql performance


    【解决方案1】:

    不,这不是最有效的。 Source

    这就是为什么在 MySQL 中搜索缺失值的最佳方法是使用 LEFT JOIN / IS NULL 或 NOT IN 而不是 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 ... IS 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 行,因此您的原始查询可能足够快。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-05
      • 2012-08-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多