我正在尝试使用 Dedupe
包将一个小的杂乱数据合并到一个规范表中。由于规范表非常大(1.22 亿行),我无法将其全部加载到内存中。
我目前使用的方法基于 this需要一整天的时间来处理测试数据:一个存储在 dict 中的 300k 行的杂乱数据表,以及一个存储在 mysql 中的 600k 行的规范数据表。如果我在内存中完成所有操作(以字典形式读取规范表),则只需半小时。
有没有办法让它更有效率?
blocked_pairs = block_data(messy_data, canonical_db_cursor, gazetteer)
clustered_dupes = gazetteer.matchBlocks(blocked_pairs, 0)
def block_data(messy_data, c, gazetteer):
block_groups = itertools.groupby(gazetteer.blocker(messy_data.viewitems()),
lambda x: x[1])
for (record_id, block_keys) in block_groups:
a = [(record_id, messy_data[record_id], set())]
c.execute("""SELECT *
FROM canonical_table
WHERE record_id IN
(SELECT DISTINCT record_id
FROM blocking_map
WHERE block_key IN %s)""",
(tuple(block_key for block_key, _ in block_keys),))
b = [(row[self.key], row, set()) for row in c]
if b:
yield (a, b)
最佳答案
通过将查询拆分为两个查询来显着加快速度。我正在使用 mysql
并且示例中使用的所有列都已编入索引...
def block_data(messy_data, c, gazetteer):
block_groups = itertools.groupby(gazetteer.blocker(messy_data.viewitems()),
lambda x: x[1])
for (record_id, block_keys) in block_groups:
a = [(record_id, messy_data[record_id], set())]
c.execute("""SELECT DISTINCT record_id
FROM blocking_map
WHERE block_key IN %s""",
(tuple(block_key for block_key, _ in block_keys),))
values = tuple(row['record_id'] for row in c)
if values:
c.execute("""SELECT *
FROM canonical_table
WHERE record_id IN %s""",
(values,))
b = [(row['record_id'], row, set())
for row in c]
if b:
yield (a, b)
关于python - 如何使用 python Dedupe 有效地将记录链接到大表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31437946/