【问题标题】:How do make faster query from pandas to postgresql如何更快地从 pandas 查询到 postgresql
【发布时间】:2021-06-01 12:57:19
【问题描述】:

我有一个 CSV 文件,我必须搜索数据库中有哪些行。 例如,从我的 CSV 中,我必须使用姓名、姓氏和出生日期才能在 DB 中查找大学名称。 例如:

从这个图像示例中,我应该找到大学 1 的 XXX YYY 学习,大学 2 的 AAA BBB,TTT YYY 没有结果。

我的解决方案很慢。 CSV 文件有 50k 行和 DB 40M。

我使用 python pandas,并读取 CSV 文件,然后我创建了一个包含姓名、姓氏和出生日期的新列。来自新组合列的示例数据:“XXX+YYYY+29-05-1953”

然后我从新的 combine 列中获取所有可能数据的列表。 可以说列表是:combine_list = data[new_column].tolist()

现在我的惊人查询:))

query = Select concat(name ,'+',surname,'+',birthdate) as new_column, university
        from db_table where name is not NULL and surname is not NULL and birthdate is not NULL
        and concat(name ,'+',surname,'+',birthdate) in {tuple(combine_list)}"

您能否给我一些建议以更快地找到它们?

【问题讨论】:

    标签: python sql pandas postgresql


    【解决方案1】:

    您可以将列查询为元组:

    Select concat(name ,'+',surname,'+',birthdate) as new_column, university
    from db_table
    where (name, surname, birthdate) IN (('XXX', 'YYY', '29-05-53'),
                                         ('AAA', 'BBB', '01-01-1997'), ...)
    

    这应该比查询连接的值更快,尤其是在WHERE 子句中的列上有索引的情况下。

    【讨论】:

      猜你喜欢
      • 2022-08-06
      • 1970-01-01
      • 2015-09-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-07
      • 1970-01-01
      相关资源
      最近更新 更多