From 360708e93de1ee1c7aff829c6b832b9e2b5a5f5f Mon Sep 17 00:00:00 2001 From: Brad Rydzewski Date: Thu, 8 Nov 2018 22:42:32 -0800 Subject: [PATCH] optimize postgres query --- store/datastore/sql/mysql/sql_gen.go | 14 --------- store/datastore/sql/postgres/files/feed.sql | 34 +++++---------------- store/datastore/sql/postgres/sql_gen.go | 34 +++++---------------- store/datastore/sql/sqlite/sql_gen.go | 14 --------- 4 files changed, 14 insertions(+), 82 deletions(-) diff --git a/store/datastore/sql/mysql/sql_gen.go b/store/datastore/sql/mysql/sql_gen.go index a93ea22cb..313a53eb4 100644 --- a/store/datastore/sql/mysql/sql_gen.go +++ b/store/datastore/sql/mysql/sql_gen.go @@ -1,17 +1,3 @@ -// Copyright 2018 Drone.IO Inc. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - package mysql // Lookup returns the named statement. diff --git a/store/datastore/sql/postgres/files/feed.sql b/store/datastore/sql/postgres/files/feed.sql index 67cf802ca..93da38619 100644 --- a/store/datastore/sql/postgres/files/feed.sql +++ b/store/datastore/sql/postgres/files/feed.sql @@ -1,34 +1,14 @@ -- name: feed-latest-build -SELECT - repo_owner -,repo_name -,repo_full_name -,build_number -,build_event -,build_status -,build_created -,build_started -,build_finished -,build_commit -,build_branch -,build_ref -,build_refspec -,build_remote -,build_title -,build_message -,build_author -,build_email -,build_avatar -FROM repos LEFT OUTER JOIN builds ON build_id = ( - SELECT build_id FROM builds - WHERE builds.build_repo_id = repos.repo_id - ORDER BY build_id DESC - LIMIT 1 -) +SELECT repo_owner, repo_name, repo_full_name, b.* +FROM repos LEFT OUTER JOIN ( + SELECT DISTINCT ON (build_repo_id) build_repo_id, build_id, build_number, build_event, build_status, build_created, build_started, build_finished, build_commit, build_branch, build_ref, build_refspec, build_remote, build_title, build_message, build_author, build_email, build_avatar + FROM builds + ORDER BY build_repo_id, build_id DESC +) b ON b.build_repo_id = repos.repo_id INNER JOIN perms ON perms.perm_repo_id = repos.repo_id WHERE perms.perm_user_id = $1 - AND repos.repo_active = true + AND repos.repo_active = TRUE ORDER BY repo_full_name ASC; -- name: feed diff --git a/store/datastore/sql/postgres/sql_gen.go b/store/datastore/sql/postgres/sql_gen.go index 5cb3576b1..7cac189c0 100644 --- a/store/datastore/sql/postgres/sql_gen.go +++ b/store/datastore/sql/postgres/sql_gen.go @@ -98,35 +98,15 @@ SELECT currval('builds_build_id_seq'); ` var feedLatestBuild = ` -SELECT - repo_owner -,repo_name -,repo_full_name -,build_number -,build_event -,build_status -,build_created -,build_started -,build_finished -,build_commit -,build_branch -,build_ref -,build_refspec -,build_remote -,build_title -,build_message -,build_author -,build_email -,build_avatar -FROM repos LEFT OUTER JOIN builds ON build_id = ( - SELECT build_id FROM builds - WHERE builds.build_repo_id = repos.repo_id - ORDER BY build_id DESC - LIMIT 1 -) +SELECT repo_owner, repo_name, repo_full_name, b.* +FROM repos LEFT OUTER JOIN ( + SELECT DISTINCT ON (build_repo_id) build_repo_id, build_id, build_number, build_event, build_status, build_created, build_started, build_finished, build_commit, build_branch, build_ref, build_refspec, build_remote, build_title, build_message, build_author, build_email, build_avatar + FROM builds + ORDER BY build_repo_id, build_id DESC +) b ON b.build_repo_id = repos.repo_id INNER JOIN perms ON perms.perm_repo_id = repos.repo_id WHERE perms.perm_user_id = $1 - AND repos.repo_active = true + AND repos.repo_active = TRUE ORDER BY repo_full_name ASC; ` diff --git a/store/datastore/sql/sqlite/sql_gen.go b/store/datastore/sql/sqlite/sql_gen.go index a46421788..e98d2e36e 100644 --- a/store/datastore/sql/sqlite/sql_gen.go +++ b/store/datastore/sql/sqlite/sql_gen.go @@ -1,17 +1,3 @@ -// Copyright 2018 Drone.IO Inc. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - package sqlite // Lookup returns the named statement.