减少 500000 行的 POStgreSQL table 到 Pandas 的执行时间的替代方法?

Alternate ways to reduce execution time for bringing a POstgreSQL table to Pandas with 500000 rows?

我有一个包含 70 个 table 的 PostgreSQL 数据库,我希望访问其中一个名为 "hub_psm_log_inter" 的特定 table。我希望将它带到 pandas 并对其应用一些操作。我正在访问的 table 的形状为 (500000, 23),将来可能会增加。 执行 psql.read_sql_query 大约需要 3 分钟。 我希望减少它所花费的时间。 对我来说重要的行是 (cust_hub_id = 358 & status_switch = 1)。 df_on 的形状只有 10000 行。

import numpy as np  
import pandas as pd  

import psycopg2 as pg  
import pandas.io.sql as psql

conn = pg.connect(
    database = '', 
    user = '', 
    password = '', 
    host = '', 
    port = '' 
)

df2 = psql.read_sql_query("SELECT * FROM hub_psm_log_inter", conn)


df4 = df2[df2.cust_hub_id == 358]
df4['status_switch'] = pd.to_numeric(df4['status_switch'], errors='coerce')
df_on = df4[df4.status_switch == 1]

在您的 SQL 查询中使用 WHERE 子句:

SELECT * FROM hub_psm_log_inter WHERE cust_hub_id = 358 AND status_switch = 1

从您的代码来看,status_switch 可能作为字符串存储在您的 table 中,因此您可能需要引用它,即

SELECT * FROM hub_psm_log_inter WHERE cust_hub_id = 358 AND status_switch = '1'