init: Create database indexes during schema creation

We now automatically create the database indexes during schema
creation by parsing our own index creation script(s) and doing
their raw SQL commands one at a time on the underlying database.

To simplify setup we now only perform this work inside of init,
and no longer do it implicitly when the WAR is loaded inside of
a servlet container.

Bug: issue 343
Change-Id: I0b37d7e75d75402a3854bb3751c642dec7897940
Signed-off-by: Shawn O. Pearce <sop@google.com>
This commit is contained in:
Shawn O. Pearce
2009-12-16 20:30:00 -08:00
parent 3bccd773c2
commit 2fdaabd50e
13 changed files with 547 additions and 298 deletions

View File

@@ -1,171 +0,0 @@
-- Gerrit 2 : Generic
--
-- Indexes to support @Query
--
-- *********************************************************************
-- AccountAccess
-- covers: byPreferredEmail, suggestByPreferredEmail
CREATE INDEX accounts_byPreferredEmail
ON accounts (preferred_email);
-- covers: suggestByFullName
CREATE INDEX accounts_byFullName
ON accounts (full_name);
-- *********************************************************************
-- AccountAgreementAccess
-- @PrimaryKey covers: byAccount
-- *********************************************************************
-- AccountExternalIdAccess
-- covers: byAccount
CREATE INDEX account_external_ids_byAccount
ON account_external_ids (account_id);
-- covers: byEmailAddress, suggestByEmailAddress
CREATE INDEX account_external_ids_byEmail
ON account_external_ids (email_address);
-- *********************************************************************
-- AccountGroupAccess
-- @SecondaryKey("name") covers: all, suggestByName
CREATE INDEX account_groups_ownedByGroup
ON account_groups (owner_group_id);
-- *********************************************************************
-- AccountGroupMemberAccess
-- @PrimaryKey covers: byAccount
CREATE INDEX account_group_members_byGroup
ON account_group_members (group_id);
-- *********************************************************************
-- AccountProjectWatchAccess
-- @PrimaryKey covers: byAccount
-- covers: notifyNewChanges
CREATE INDEX account_project_watches_ntNew
ON account_project_watches (notify_new_changes, project_name);
-- covers: notifyAllComments
CREATE INDEX account_project_watches_ntCmt
ON account_project_watches (notify_all_comments, project_name);
-- covers: notifySubmittedChanges
CREATE INDEX account_project_watches_ntSub
ON account_project_watches (notify_submitted_changes, project_name);
-- *********************************************************************
-- AccountSshKeyAccess
-- @PrimaryKey covers: byAccount, valid
-- *********************************************************************
-- ApprovalCategoryAccess
-- too small to bother indexing
-- *********************************************************************
-- ApprovalCategoryValueAccess
-- @PrimaryKey covers: byCategory
-- *********************************************************************
-- BranchAccess
-- @PrimaryKey covers: byProject
-- *********************************************************************
-- ChangeAccess
-- covers: byOwnerOpen
CREATE INDEX changes_byOwnerOpen
ON changes (open, owner_account_id, created_on, change_id);
-- covers: byOwnerClosed
CREATE INDEX changes_byOwnerClosed
ON changes (open, owner_account_id, last_updated_on);
-- covers: submitted, allSubmitted
CREATE INDEX changes_submitted
ON changes (status, dest_project_name, dest_branch_name, last_updated_on);
-- covers: allOpenPrev, allOpenNext
CREATE INDEX changes_allOpen
ON changes (open, sort_key);
-- covers: byProjectOpenPrev, byProjectOpenNext
CREATE INDEX changes_byProjectOpen
ON changes (open, dest_project_name, sort_key);
-- covers: allClosedPrev, allClosedNext
CREATE INDEX changes_allClosed
ON changes (open, status, sort_key);
CREATE INDEX changes_key
ON changes (change_key);
-- *********************************************************************
-- PatchSetApprovalAccess
-- @PrimaryKey covers: byPatchSet, byPatchSetUser
-- covers: openByUser
CREATE INDEX patch_set_approvals_openByUser
ON patch_set_approvals (change_open, account_id);
-- covers: closedByUser
CREATE INDEX patch_set_approvals_closedByUser
ON patch_set_approvals (change_open, account_id, change_sort_key);
-- *********************************************************************
-- ChangeMessageAccess
-- @PrimaryKey covers: byChange
-- *********************************************************************
-- ContributorAgreementAccess
-- covers: active
CREATE INDEX contributor_agreements_active
ON contributor_agreements (active, short_name);
-- *********************************************************************
-- PatchLineCommentAccess
-- @PrimaryKey covers: published, draft
CREATE INDEX patch_comment_drafts
ON patch_comments (status, author_id);
-- *********************************************************************
-- PatchSetAncestorAccess
-- @PrimaryKey covers: ancestorsOf
-- covers: descendantsOf
CREATE INDEX patch_set_ancestors_desc
ON patch_set_ancestors (ancestor_revision);
-- *********************************************************************
-- ProjectAccess
-- @PrimaryKey covers: all, suggestByName
-- *********************************************************************
-- ProjectRightAccess
-- @PrimaryKey covers: byProject
-- covers: byCategoryGroup
CREATE INDEX project_rights_byCatGroup
ON project_rights (category_id, group_id);
-- *********************************************************************
-- StarredChangeAccess
-- @PrimaryKey covers: byAccount
CREATE INDEX starred_changes_byChange
ON starred_changes (change_id);

View File

@@ -1,219 +0,0 @@
-- Gerrit 2 : PostgreSQL
--
-- Cluster hot tables by their primary method of access
--
ALTER TABLE patch_sets CLUSTER ON patch_sets_pkey;
ALTER TABLE change_messages CLUSTER ON change_messages_pkey;
ALTER TABLE patch_comments CLUSTER ON patch_comments_pkey;
ALTER TABLE patch_set_approvals CLUSTER ON patch_set_approvals_pkey;
ALTER TABLE account_group_members CLUSTER ON account_group_members_pkey;
ALTER TABLE starred_changes CLUSTER ON starred_changes_pkey;
CLUSTER;
-- Define our schema upgrade support function.
--
CREATE LANGUAGE plpgsql;
CREATE OR REPLACE FUNCTION
check_schema_version (exp INT)
RETURNS VARCHAR(255)
AS $$
DECLARE
l_act INT;
BEGIN
SELECT version_nbr INTO l_act
FROM schema_version;
IF l_act <> exp
THEN
RAISE EXCEPTION 'expected schema %, found %', exp, l_act;
END IF;
RETURN 'OK';
END;
$$ LANGUAGE plpgsql;
-- Indexes to support @Query
--
-- *********************************************************************
-- AccountAccess
-- covers: byPreferredEmail, suggestByPreferredEmail
CREATE INDEX accounts_byPreferredEmail
ON accounts (preferred_email);
-- covers: suggestByFullName
CREATE INDEX accounts_byFullName
ON accounts (full_name);
-- *********************************************************************
-- AccountAgreementAccess
-- @PrimaryKey covers: byAccount
-- *********************************************************************
-- AccountExternalIdAccess
-- covers: byAccount
CREATE INDEX account_external_ids_byAccount
ON account_external_ids (account_id);
-- covers: byEmailAddress, suggestByEmailAddress
CREATE INDEX account_external_ids_byEmail
ON account_external_ids (email_address);
-- *********************************************************************
-- AccountGroupAccess
-- @SecondaryKey("name") covers: all, suggestByName
CREATE INDEX account_groups_ownedByGroup
ON account_groups (owner_group_id);
-- *********************************************************************
-- AccountGroupMemberAccess
-- @PrimaryKey covers: byAccount
CREATE INDEX account_group_members_byGroup
ON account_group_members (group_id);
-- *********************************************************************
-- AccountProjectWatchAccess
-- @PrimaryKey covers: byAccount
-- covers: notifyNewChanges
CREATE INDEX account_project_watches_ntNew
ON account_project_watches (project_name)
WHERE notify_new_changes = 'Y';
-- covers: notifyAllComments
CREATE INDEX account_project_watches_ntCmt
ON account_project_watches (project_name)
WHERE notify_all_comments = 'Y';
-- covers: notifySubmittedChanges
CREATE INDEX account_project_watches_ntSub
ON account_project_watches (project_name)
WHERE notify_submitted_changes = 'Y';
-- *********************************************************************
-- AccountSshKeyAccess
-- @PrimaryKey covers: byAccount, valid
-- *********************************************************************
-- ApprovalCategoryAccess
-- too small to bother indexing
-- *********************************************************************
-- ApprovalCategoryValueAccess
-- @PrimaryKey covers: byCategory
-- *********************************************************************
-- BranchAccess
-- @PrimaryKey covers: byProject
-- *********************************************************************
-- ChangeAccess
-- covers: byOwnerOpen
CREATE INDEX changes_byOwnerOpen
ON changes (owner_account_id, created_on, change_id)
WHERE open = 'Y';
-- covers: byOwnerClosed
CREATE INDEX changes_byOwnerClosed
ON changes (owner_account_id, last_updated_on)
WHERE open = 'N';
-- covers: submitted, allSubmitted
CREATE INDEX changes_submitted
ON changes (dest_project_name, dest_branch_name, last_updated_on)
WHERE status = 's';
-- covers: allOpenPrev, allOpenNext
CREATE INDEX changes_allOpen
ON changes (sort_key)
WHERE open = 'Y';
-- covers: byProjectOpenPrev, byProjectOpenNext
CREATE INDEX changes_byProjectOpen
ON changes (dest_project_name, sort_key)
WHERE open = 'Y';
-- covers: allClosedPrev, allClosedNext
CREATE INDEX changes_allClosed
ON changes (status, sort_key)
WHERE open = 'N';
CREATE INDEX changes_key
ON changes (change_key);
-- *********************************************************************
-- PatchSetApprovalAccess
-- @PrimaryKey covers: byPatchSet, byPatchSetUser
-- covers: openByUser
CREATE INDEX patch_set_approvals_openByUser
ON patch_set_approvals (account_id)
WHERE change_open = 'Y';
-- covers: closedByUser
CREATE INDEX patch_set_approvals_closedByUser
ON patch_set_approvals (account_id, change_sort_key)
WHERE change_open = 'N';
-- *********************************************************************
-- ChangeMessageAccess
-- @PrimaryKey covers: byChange
-- *********************************************************************
-- ContributorAgreementAccess
-- covers: active
CREATE INDEX contributor_agreements_active
ON contributor_agreements (active, short_name);
-- *********************************************************************
-- PatchLineCommentAccess
-- @PrimaryKey covers: published, draft
CREATE INDEX patch_comment_drafts
ON patch_comments (author_id)
WHERE status = 'd';
-- *********************************************************************
-- PatchSetAncestorAccess
-- @PrimaryKey covers: ancestorsOf
-- covers: descendantsOf
CREATE INDEX patch_set_ancestors_desc
ON patch_set_ancestors (ancestor_revision);
-- *********************************************************************
-- ProjectAccess
-- @PrimaryKey covers: all, suggestByName
-- covers: ownedByGroup
-- *********************************************************************
-- ProjectRightAccess
-- @PrimaryKey covers: byProject
-- covers: byCategoryGroup
CREATE INDEX project_rights_byCatGroup
ON project_rights (category_id, group_id);
-- *********************************************************************
-- StarredChangeAccess
-- @PrimaryKey covers: byAccount
CREATE INDEX starred_changes_byChange
ON starred_changes (change_id);

View File

@@ -1,27 +0,0 @@
-- Gerrit 2 : MySQL
--
delimiter //
CREATE FUNCTION nextval_project_id ()
RETURNS BIGINT
LANGUAGE SQL
NOT DETERMINISTIC
MODIFIES SQL DATA
BEGIN
INSERT INTO project_id (s) VALUES (NULL);
DELETE FROM project_id WHERE s = LAST_INSERT_ID();
RETURN LAST_INSERT_ID();
END;
//
CREATE FUNCTION nextval_account_id ()
RETURNS BIGINT
LANGUAGE SQL
NOT DETERMINISTIC
MODIFIES SQL DATA
BEGIN
INSERT INTO account_id (s) VALUES (NULL);
DELETE FROM account_id WHERE s = LAST_INSERT_ID();
RETURN LAST_INSERT_ID();
END;
//