我的查询需要花费数小时才能运行,它在PgAdmin 4中的Postgres 9.3中超过9M行。由于它的格式,数据全部是字符串,我不得不使用子查询将其分成列。因此,为了使聚合视图变得更好,我投射了我需要的每个元素。因为有数百万行,所以有些数据元素在表中的某个点有空值,所以我需要where语句。我对where子句中的每个项目都有所不同。所有基本的btree索引。示例:在" gnma2"上创建索引loan_age_idx使用btree(" Loan_Age")。由于强制转换函数而需要的where语句导致返回零行。我该怎么做才能删除那些但保留演员陈述?
create table "t1" as
Select
gnma2."Issuer_ID",
gnma2."As_of_Date",
gnma2."Agency",
gnma2."Loan_Purpose",
gnma2."State",
gnma2."Months_Delinquent",
count(gnma2."Disclosure_Sequence_Number") as "Total_Loan_Count",
avg(cast(gnma2."Loan_Interest_Rate" as double precision))/1000 as "avg_int_rate",
avg(cast(gnma2."Original_Principal_Balance" as real))/100 as "avg_OUPB",
avg(cast(gnma2."Unpaid_Principal_Balance" as real))/100 as "avg_UPB",
avg(cast(gnma2."Loan_Age" as real)) as "avg_loan_age",
avg(cast(gnma2."Loan_To_Value" as real))/100 as "avg_LTV",
avg(cast(gnma2."Total_Debt_Expense_Ratio_Percent" as real))/100 as "avg_DTI",
avg(cast(gnma2."Credit_Score" as real)) as "avg_credit_score",
left(gnma2."First_Payment_Date",4) as "Origination_Yr"
From public."gnma2"
where
"Loan_Age" >= '1' and
"Loan_To_Value" >= '0' and
"Total_Debt_Expense_Ratio_Percent" >= '0' and
"Credit_Score" >= '0' and
"Loan_Interest_Rate" >= '0' and
"Original_Principal_Balance" >= '1' and
"Unpaid_Principal_Balance" >= '0'
Group By
gnma2."Issuer_ID",
gnma2."Agency",
gnma2."Loan_Purpose",
gnma2."State",
gnma2."As_of_Date",
gnma2."Months_Delinquent",
gnma2."First_Payment_Date";
QUERY PLAN
HashAggregate (cost=3496556.07..3496556.65 rows=13 width=91) (actual
time=124214.207..124214.207 rows=0 loops=1)
-> Bitmap Heap Scan on gnma2
(cost=166750.75..3496546.65 rows=130 width=91) (actual time=124214.202..124214.20
2 rows=0 loops=1)
Recheck Cond: ("Loan_Age" >= '1'::bpchar)
Rows Removed by Index Recheck: 4785233
Filter: (("Loan_To_Value" >= '0'::bpchar) AND ("Total_Debt_Expense_Ratio_Percent" >= '0'::bpchar) AND ("Cr
edit_Score" >= '0'::bpchar) AND ("Loan_Interest_Rate" >= '0'::bpchar) AND ("Original_Principal_Balance" >= '1'::bpc
har) AND ("Unpaid_Principal_Balance" >= '0'::bpchar))
Rows Removed by Filter: 9311198
-> Bitmap Index Scan on loan_age_indx (cost=0.00..166750.72 rows=9029087 width=0) (actual time=5015.865.
.5015.865 rows=9311198 loops=1)
Index Cond: ("Loan_Age" >= '1'::bpchar)
Total runtime: 124214.524 ms