Merge lp:~lifeless/launchpad/bug-421901 into lp:launchpad/db-devel

Proposed by Robert Collins
Status: Merged
Approved by: Stuart Bishop
Approved revision: no longer in the source branch.
Merged at revision: 10403
Proposed branch: lp:~lifeless/launchpad/bug-421901
Merge into: lp:launchpad/db-devel
Diff against target: 86 lines (+60/-0)
3 files modified
database/schema/comments.sql (+1/-0)
database/schema/patch-2208-60-0.sql (+15/-0)
database/schema/trusted.sql (+44/-0)
To merge this branch: bzr merge lp:~lifeless/launchpad/bug-421901
Reviewer Review Type Date Requested Status
Stuart Bishop (community) db Approve
Review via email: mp+56729@code.launchpad.net

Commit message

[r=stub][bug=421901] Schema preparing for improving who-commented-on-bug queries.

Description of the change

The commented bugs query is slow because we scan across 4 tables, even a bitmap join will only drop that to effectively three, so we end up reading all the messages from $foo, then filtering at the bugmessage step, and then onto bug. Making a copy of owner available on bugmessage and indexing appropriately (see the bug for a variety of tried indices) gives a 10x speed up on our pathological cases.

To post a comment you must log in.
Revision history for this message
Stuart Bishop (stub) wrote :

Fine

review: Approve (db)

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'database/schema/comments.sql'
2--- database/schema/comments.sql 2011-03-31 15:08:15 +0000
3+++ database/schema/comments.sql 2011-04-07 10:58:28 +0000
4@@ -1360,6 +1360,7 @@
5 COMMENT ON COLUMN BugMessage.bugwatch IS 'The external bug this bug comment was imported from.';
6 COMMENT ON COLUMN BugMessage.remote_comment_id IS 'The id this bug comment has in the external bug tracker, if it is an imported comment. If it is NULL while having a bugwatch set, this comment was added in Launchpad and needs to be pushed to the external bug tracker.';
7 COMMENT ON COLUMN BugMessage.index IS 'The index (used in urls) of the message in a particular bug.';
8+COMMENT ON COLUMN BugMessage.owner IS 'Denormalised owner from Message, used for efficient queries on commentors.';
9
10 -- Messaging subsytem
11 COMMENT ON TABLE Message IS 'This table stores a single RFC822-style message. Messages can be threaded (using the parent field). These messages can then be referenced from elsewhere in the system, such as the BugMessage table, integrating messageboard facilities with the rest of The Launchpad.';
12
13=== added file 'database/schema/patch-2208-60-0.sql'
14--- database/schema/patch-2208-60-0.sql 1970-01-01 00:00:00 +0000
15+++ database/schema/patch-2208-60-0.sql 2011-04-07 10:58:28 +0000
16@@ -0,0 +1,15 @@
17+-- Copyright 2011 Canonical Ltd. This software is licensed under the
18+-- GNU Affero General Public License version 3 (see the file LICENSE).
19+
20+SET client_min_messages=ERROR;
21+
22+-- denormalise message.owner for query efficiency.
23+ALTER TABLE bugmessage ADD COLUMN owner int;
24+-- And an index.
25+CREATE INDEX bugmessage__owner__index__idx ON bugmessage USING btree(owner, index);
26+
27+-- Triggers to maintain in both directions.
28+CREATE TRIGGER bugmessage__owner__mirror AFTER UPDATE OR INSERT ON bugmessage FOR EACH ROW EXECUTE PROCEDURE bugmessage_copy_owner_from_message();
29+CREATE TRIGGER message__owner__mirror AFTER UPDATE ON message FOR EACH ROW EXECUTE PROCEDURE message_copy_owner_to_bugmessage();
30+
31+INSERT INTO LaunchpadDatabaseRevision VALUES (2208, 60, 0);
32
33=== modified file 'database/schema/trusted.sql'
34--- database/schema/trusted.sql 2011-03-29 10:17:46 +0000
35+++ database/schema/trusted.sql 2011-04-07 10:58:28 +0000
36@@ -1954,6 +1954,50 @@
37 return int(total_heat)
38 $$;
39
40+CREATE OR REPLACE FUNCTION bugmessage_copy_owner_from_message()
41+RETURNS trigger LANGUAGE plpgsql SECURITY DEFINER SET search_path TO public AS
42+$$
43+BEGIN
44+ IF TG_OP = 'INSERT' THEN
45+ IF NEW.owner is NULL THEN
46+ UPDATE BugMessage
47+ SET owner = Message.owner FROM
48+ Message WHERE
49+ Message.id = NEW.message AND
50+ BugMessage.id = NEW.id;
51+ END IF;
52+ ELSIF NEW.message != OLD.message THEN
53+ UPDATE BugMessage
54+ SET owner = Message.owner FROM
55+ Message WHERE
56+ Message.id = NEW.message AND
57+ BugMessage.id = NEW.id;
58+ END IF;
59+ RETURN NULL; -- Ignored - this is an AFTER trigger
60+END;
61+$$;
62+
63+COMMENT ON FUNCTION bugmessage_copy_owner_from_message() IS
64+'Copies the message owner into bugmessage when bugmessage changes.';
65+
66+CREATE OR REPLACE FUNCTION message_copy_owner_to_bugmessage()
67+RETURNS trigger LANGUAGE plpgsql SECURITY DEFINER SET search_path TO public AS
68+$$
69+BEGIN
70+ IF NEW.owner != OLD.owner THEN
71+ UPDATE BugMessage
72+ SET owner = NEW.owner
73+ WHERE
74+ BugMessage.message = NEW.id;
75+ END IF;
76+ RETURN NULL; -- Ignored - this is an AFTER trigger
77+END;
78+$$;
79+
80+COMMENT ON FUNCTION message_copy_owner_to_bugmessage() IS
81+'Copies the message owner into bugmessage whenmessage changes.';
82+
83+
84 CREATE OR REPLACE FUNCTION bug_update_heat_copy_to_bugtask()
85 RETURNS trigger LANGUAGE plpgsql SECURITY DEFINER SET search_path TO public AS
86 $$

Subscribers

People subscribed via source and target branches

to status/vote changes: