Effective query for listing flags in use by messages in a folder - Mailing list pgsql-sql

From Andreas Joseph Krogh
Subject Effective query for listing flags in use by messages in a folder
Date
Msg-id VisenaEmail.12.442a4269e3e66e11.14c26b11f46@tc7-visena
Whole thread Raw
Responses Re: Effective query for listing flags in use by messages in a folder
List pgsql-sql
Hi all.
 
On PG-9.3 (no JSONB),
 
For an IMAP-like system; I'm trying to figure out an effective way to query for "what flags are in use in a folder". A flag is considered used when one or more messages in that folder has the value=true.
 
The schema is like this:
create table message(    folder_id integer not NULL,    is_seen boolean NOT NULL default false,    is_replied boolean not null default false,    is_forwarded boolean not null default false,    is_deleted boolean not null default false,    is_draft boolean not null default false,    is_flagged boolean not null default false
);
 
I need the "flags" to be in the message-table for other queries to be as efficient as possible (no JOIN'ing), the system contains millions of messages.
 
create index message_folder_id_deleted_idx ON message(folder_id) where is_deleted = TRUE;
create index message_folder_id_forwarded_idx ON message(folder_id) where is_forwarded = TRUE;
create index message_folder_id_replied_idx ON message(folder_id) where is_replied = TRUE;
create index message_folder_id_seen_idx ON message(folder_id) where is_seen = TRUE;
create index message_folder_id_flagged_idx ON message(folder_id) where is_flagged = TRUE;
create index message_folder_id_draft_idx ON message(folder_id) where is_draft = TRUE;

select 'is_deleted' from (select * from message where folder_id = 3 AND is_deleted limit 1) as q
UNION
select 'is_forwarded' from (select * from message where folder_id = 3 AND is_forwarded limit 1) as q
UNION
select 'isreplied' from (select * from message where folder_id = 3 AND is_replied limit 1) as q
UNION
select 'is_seen' from (select * from message where folder_id = 3 AND is_seen limit 1) as q
UNION
select 'is_flagged' from (select * from message where folder_id = 3 AND is_flagged limit 1) as q
UNION
select 'is_draft' from (select * from message where folder_id = 3 AND is_draft limit 1) as q
;
 
Are there better ways to do this?
 
Thanks.
 
--
Andreas Joseph Krogh
CTO / Partner - Visena AS
Mobile: +47 909 56 963
Attachment

pgsql-sql by date:

Previous
From: Pavel Stehule
Date:
Subject: Re: Perfomance 9.1.9 vs 9.4.1
Next
From: Hector Vass
Date:
Subject: Re: Effective query for listing flags in use by messages in a folder