optimize postgres query

This commit is contained in:
Brad Rydzewski 2018-11-08 22:42:32 -08:00
parent 4f5c53f9ee
commit 360708e93d
4 changed files with 14 additions and 82 deletions

View file

@ -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 package mysql
// Lookup returns the named statement. // Lookup returns the named statement.

View file

@ -1,34 +1,14 @@
-- name: feed-latest-build -- name: feed-latest-build
SELECT SELECT repo_owner, repo_name, repo_full_name, b.*
repo_owner FROM repos LEFT OUTER JOIN (
,repo_name 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
,repo_full_name FROM builds
,build_number ORDER BY build_repo_id, build_id DESC
,build_event ) b ON b.build_repo_id = repos.repo_id
,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
)
INNER JOIN perms ON perms.perm_repo_id = repos.repo_id INNER JOIN perms ON perms.perm_repo_id = repos.repo_id
WHERE perms.perm_user_id = $1 WHERE perms.perm_user_id = $1
AND repos.repo_active = true AND repos.repo_active = TRUE
ORDER BY repo_full_name ASC; ORDER BY repo_full_name ASC;
-- name: feed -- name: feed

View file

@ -98,35 +98,15 @@ SELECT currval('builds_build_id_seq');
` `
var feedLatestBuild = ` var feedLatestBuild = `
SELECT SELECT repo_owner, repo_name, repo_full_name, b.*
repo_owner FROM repos LEFT OUTER JOIN (
,repo_name 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
,repo_full_name FROM builds
,build_number ORDER BY build_repo_id, build_id DESC
,build_event ) b ON b.build_repo_id = repos.repo_id
,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
)
INNER JOIN perms ON perms.perm_repo_id = repos.repo_id INNER JOIN perms ON perms.perm_repo_id = repos.repo_id
WHERE perms.perm_user_id = $1 WHERE perms.perm_user_id = $1
AND repos.repo_active = true AND repos.repo_active = TRUE
ORDER BY repo_full_name ASC; ORDER BY repo_full_name ASC;
` `

View file

@ -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 package sqlite
// Lookup returns the named statement. // Lookup returns the named statement.