python - 我的 Postgres 查询如何执行得更快?我可以使用 Python 来提供更快的迭代吗?

标签 python postgresql pandas

这是一个由两部分组成的问题。如果您正在查看此内容,感谢您抽出宝贵时间!

  1. 有没有办法加快我的查询速度?

    我之前问过一个问题 here ,并最终能够自己解决问题。

    但是,当我针对包含 40,000 多条记录的数据库运行时,我设计的用于生成所需结果的查询非常慢(超过 25 分钟)。

    查询达到了它的目的,但我希望你们中的一位聪明人能向我指出如何使查询以更理想的速度执行。

    我的查询:

    with dupe as (
        select
             json_document->'Firstname'->0->'Content' as first_name,
             json_document->'Lastname'->0->'Content' as last_name,
             identifiers->'RecordID' as record_id
        from (
            select *,  
                   jsonb_array_elements(json_document->'Identifiers') as identifiers
            from staging
        ) sub
        group by record_id, json_document
        order by last_name
    ) 
    
    select * from dupe da where (
      select count(*) from dupe db 
      where db.record_id = da.record_id
    ) > 1;
    

    同样,一些示例数据:

    第 1 行:

    {
            "Firstname": "Bobb",
            "Lastname": "Smith",
            "Identifiers": [
                {
                    "Content": "123",
                    "RecordID": "123",
                    "SystemID": "Test",
                    "LastUpdated": "2017-09-12T02:23:30.817Z"
                },
                {
                    "Content": "abc",
                    "RecordID": "abc",
                    "SystemID": "Test",
                    "LastUpdated": "2017-09-13T10:10:21.598Z"
                },
                {
                    "Content": "def",
                    "RecordID": "def",
                    "SystemID": "Test",
                    "LastUpdated": "2017-09-13T10:10:21.598Z"
                }
            ]
    }
    

    第 2 行:

    {
            "Firstname": "Bob",
            "Lastname": "Smith",
            "Identifiers": [
                {
                    "Content": "abc",
                    "RecordID": "abc",
                    "SystemID": "Test",
                    "LastUpdated": "2017-09-13T10:10:26.020Z"
                }
            ]
    }
    
  2. 如果我要将我的查询结果或部分结果引入可以使用 Pandas 对其进行操作的 Python 环境,我该如何迭代我的查询结果(或子-查询)以获得与我的原始查询相同的最终结果?

    有没有更简单的方法,使用 Python,以与 Postgres 相同的方式遍历我的非嵌套 json 数组?

    例如,执行此查询后:

    select
        json_document->'Firstname'->0->'Content' as first_name,
        json_document->'Lastname'->0->'Content' as last_name,
        identifiers->'RecordID' as record_id
    from (
           select *,  
                  jsonb_array_elements(json_document->'Identifiers') as identifiers
           from staging
         ) sub
    order by last_name;
    

    我如何使用 Python/Pandas 获取该查询的结果并执行如下操作:

    da = datasets[query_results]  # to equal my dupe da query
    db = datasets[query_results]  # to equal my dupe db query
    

    然后执行相当于

    select * from dupe da where (
        select count(*) from dupe db 
        where db.record_id = da.record_id
    ) > 1;
    

    在 Python 中?

如果我没有在这里提供足够的信息,我深表歉意。我是一个 Python 新手。非常感谢任何帮助!谢谢!!

最佳答案

尝试以下操作,它会消除您的计数 (*),而是使用存在。

 with dupe as ( 
   select id, 
     json_document->'Firstname'->0->'Content' as first_name, 
     json_document->'Lastname'->0->'Content' as last_name, 
     identifiers->'RecordID' as record_id 
   from 
     (select 
       *, 
       jsonb_array_elements(json_document->'Identifiers') as identifiers 
      from staging ) sub 
      group by
        id,
        record_id, 
        json_document 
      order by last_name ) 
 select * from dupe da 
   where exists 
     (select * 
       from dupe db 
       where 
         db.record_id = da.record_id 
         and db.id != da.id
     )

关于python - 我的 Postgres 查询如何执行得更快?我可以使用 Python 来提供更快的迭代吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46242442/

相关文章:

python - 有什么方法可以告诉 selenium 在某个时候不要执行 js?

python - 缺少所需的参数状态Python社交身份验证电子邮件验证

postgresql - 将数据库记录写入 postgres 时运行 shell 脚本

python - 根据查找更改 pandas 中的行值

python - 为什么 Python ggplot 返回名称 'aes' 未定义?

python - 如何在 Scrapy 中暂停爬虫

python - 带有 Pandas read_json 的列数据类型

python - psycopg2 映射 Python : "list of dicts" to Postgres : "array of composite type" for an INSERT statement

sql - PostgreSQL 使用触发器更新表

python - 按时间间隔+聚合函数对 Pandas 进行分组