sql - Best way to handle multiple table query linked to "main" table -
i tracking visitor sessions , have "master" table contains session start, end , visitor info.
create table "sessions" ( session_id bigserial primary key, session_start timestamp not null, session_end timestamp not null user_ip text not null, user_agent text not null )
for each session have 3 metrics - pageviews, downloads , video plays. each table follows formula
create table "sessions_<x>" ( session_id bigint references sessions (session_id), data_1 text not null, data_2 text not null, ... )
the 3 tables not identical, data not important - thing share session_id
reference sessions
table.
data inserted so, in single transaction, relevant pageview/download/video play record added , session_start
/ session_end
updated.
for code, wish extract each session associated pageviews, downloads , video plays. in effort minimize number of queries figured i'd following:
- do query on
sessions
table identifying information +session_id
each session. - for each of 3 metric tables,
select * table session_id in (<sessions>)
- in code, "collect" data each result set #2 , associate base session information.
i wondering if best approach. have worries large data sets might bad idea (lots of in-memory data @ time), there alternative can in 1 query session information + associated metrics (for 3 tables) easily? or @ least in way can iterate through in program 1 session @ time?
this not user-facing web service, super-fast, sub-second replies not goal, performance factor. service being built in python if matters , using postgresql 9.2.
try:
select * session s left outer join session_<x> sx on s.session_id = sx.session_id left outer join session_<y> sy on s.session_id = sy.session_id;
Comments
Post a Comment