PostgreSQL中内部选择的SQL性能问题,用于列表报告
技术问答
257 人阅读
|
0 人回复
|
2023-09-12
|
使用PostgreSQL数据库:, g7 R5 c1 z8 ], J' I
我有一个调查应用程序,用户可以输入活动并回答他们的活动。调查本身被称为RECALLS_T,输入事件是EVENTS_T,答案是ANSWERS_T。答案是提供的. v/ m1 h9 @# V0 s U" S& i. c. y
活动问题的 ,答案存储在中间ACTIVITY_QUESTIONS_T,由Lookup(LOOKUP_T)映射。0 @/ y; M% u" _3 T' o
然后,我需要运行一个基于事件的报告,其中每一行都是EVENTS_T每次 召回 事件(所有事件组合为所有召回)。然而,在报告中 某些列
2 e4 R# N, T4 e6 G% M# {9 m有必要指示某些答案的值,否则这些单元格为NULL。所以,这是 列表报告 。, i9 r, `$ |: E! x$ r- y
示例(首先是简单的扁平内容,然后是复杂的列表内容):
. U8 r' V& f5 m* ~+ |RecallID | RecallDate | Event |..| WalkAlone | WalkWithPartner |..| ExerciseAtGym256 | 10-01-19 | Exrcs |..| NULL | NULL |..| yes256 | 10-01-19 | Walk |..| yes | NULL |..| NULL256 | 10-01-19 | Eat |..| NULL | NULL |..| NULL257 | 10-01-19 | Exrcs |..| NULL | NULL |..| yes我的SQL在基于表格的答案列中有内部选择,看起来像这样:
# h7 k( k9 K$ G7 ?- W2 h1 C1 z; kselect -- Easy flat stuff firstr.id as recallid,r.recall_date as recalldate,... ,-- Example of Tabulated Columnsselect l.description from answers_t ans,activity_questions_t aq,lookup_t l where l.id=aq.answer_choice_id and aq.question_id=13 and aq.id=ans.activity_question_id and aq.activity_id=27 and ans.event_id=e.id) as transportationotherintensity,(select l.description from answers_t ans,activity_questions_t aq,lookup_t lwhere l.id=66 and l.id=aq.answer_choice_id and aq.question_id=14and aq.id=ans.activity_question_id and ans.event_id=e.id) as commutework,(select l.description from answers_t ans,activity_questions_t aq,lookup_t lwhere l.id=67 and l.id=aq.answer_choice_id and aq.question_id=14 and aq.id=ans.activity_question_id and ans.event_id=e.id) as commuteschool,(select l.description from answers_t ans,activity_questions_t aq,lookup_t lwhere l.id=95 and l.id=aq.answer_choice_id and aq.question_id=14 and aq.id=ans.activity_question_id and ans.event_id=e.id) as dropoffpickup,SQL能正常工作,并报表 得以 呈现,但是性能很差
4 i8 }) a$ _- B: M" [) D& m。我验证了它的质量是成比例的:没有魔术子弹能解决它的特定项目。每个内部选择都会导致性能下降。1.000行的结果集需要15秒,但不超过2秒-3秒。
. Y& ]. ~4 j( M1 N请注意,这些 索引 已经存在
2 R) |3 c; e3 s2 L1 i+ tANSWERS_T:在ACTIVITY_QUESTION_ID,EVENT_ID1 [% Y: p6 d: `6 \- e. c
EVENTS_T: 在 RECALL_ID( }( k( U$ m" C& ~
ACTIVITY_QUESTIONS_T:对ACTIVITY_ID,QUESTION_ID,ANSWER_CHOICE_ID我做错了这些内部选择吗?6 n8 v4 F1 K& X2 P& V; `
# p4 O/ p9 C. J7 F e) e 解决方案: |
|
|
|
|
|