PostgreSQLデータベースの使用:
ユーザーがアクティビティを入力し、アクティビティに関する質問に回答する調査アプリケーションがあります。調査自体はと呼ばれRECALLS_T
、入力されたイベントはEVENTS_T
、、回答はANSWERS_T
です。回答は、提供されたアクティビティの質問に対するものであり、に保存されACTIVITY_QUESTIONS_T
、ルックアップ(LOOKUP_T
)によってマップされます。
次に、すべての行がEVENTS_T
すべてのリコールのイベントであるイベントベースのレポートを実行する必要があります(すべてのイベントがすべてのリコールに対して結合されます)。ただし、そのレポートの一部の列は、特定の回答の値を示す必要があります。そうでない場合、それらのセルはNULLです。したがって、これは表形式のレポートです。
例(最初は簡単なフラットなもの、次に複雑な表形式のもの):
RecallID | RecallDate | Event |..| WalkAlone | WalkWithPartner |..| ExerciseAtGym
256 | 10-01-19 | Exrcs |..| NULL | NULL |..| yes
256 | 10-01-19 | Walk |..| yes | NULL |..| NULL
256 | 10-01-19 | Eat |..| NULL | NULL |..| NULL
257 | 10-01-19 | Exrcs |..| NULL | NULL |..| yes
私のSQLには、表形式の回答ベースの列の内部選択があり、次のようになります。
select
-- Easy flat stuff first
r.id as recallid, r.recall_date as recalldate, ... ,
-- Example of Tabulated Columns:
(select 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 l
where l.id=66 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 commutework,
(select l.description from answers_t ans, activity_questions_t aq, lookup_t l
where 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 l
where 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は機能し、レポートはレンダリングされますが、パフォーマンスは良くありません。私はそれが比例して悪いことを確認しました:それを修正する特定のアイテムのための魔法の弾丸はありません。すべての内部選択は、パフォーマンスの低下につながります。1,000行の結果セットには15秒かかりますが、2〜3秒以内で完了します。
これらのインデックスはすでに存在することに注意してください。
ANSWERS_T
:オンACTIVITY_QUESTION_ID
、EVENT_ID
EVENTS_T
: オン RECALL_ID
ACTIVITY_QUESTIONS_T
:上ACTIVITY_ID
、QUESTION_ID
、ANSWER_CHOICE_ID
これらの内部選択で私が間違っていることはありますか?
質問を要約すると、条件付き集計を使用します。PostgreSQLでは、次を使用できます。
select ans.event_id,
max(l.description) filter (where aq.question_id = 13 and aq.activity_id = 27) as transportationotherintensity
max(l.description) filter (where l.id = 66 and aq.question_id = 14 and aq.activity_id = 67) as commutework,
. . .
from activity_questions_t aq join
lookup_t l
on l.id = aq.answer_choice_id join
answers_t ans
on aq.id = ans.activity_question_id
group by ans.event_id
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加