Skip to content

Commit

Permalink
feat(commit-graph): optimize query
Browse files Browse the repository at this point in the history
Optimize commit graph with a lot of CTEs
  • Loading branch information
WilsonNet committed Sep 6, 2024
1 parent dd5ea81 commit 931a503
Showing 1 changed file with 34 additions and 18 deletions.
52 changes: 34 additions & 18 deletions backend/kernelCI_app/views/treeCommitsHistory.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,38 @@ def get(self, request, commit_hash):
)

query = """
WITH earliest_commits AS (
WITH
relevant_checkouts AS (
SELECT
id,
git_commit_hash,
git_commit_name,
MIN(start_time) AS earliest_start_time
git_repository_branch,
git_repository_url,
origin,
start_time
FROM
checkouts
WHERE
git_repository_branch = %(git_branch_param)s
AND git_repository_url = %(git_url_param)s
AND origin = %(origin_param)s
ORDER BY
start_time DESC
),
earliest_commits AS (
SELECT
git_commit_hash,
git_commit_name,
MAX(start_time) AS earliest_start_time
FROM
relevant_checkouts
GROUP BY
git_commit_hash,
git_commit_name
ORDER BY
earliest_start_time DESC
LIMIT 6
),
build_counts AS (
SELECT
Expand All @@ -50,7 +68,7 @@ def get(self, request, commit_hash):
SUM(CASE WHEN b.valid = false THEN 1 ELSE 0 END) AS invalid_builds,
SUM(CASE WHEN b.valid IS NULL THEN 1 ELSE 0 END) AS null_builds
FROM
checkouts AS c
relevant_checkouts AS c
INNER JOIN
builds AS b
ON c.id = b.checkout_id
Expand Down Expand Up @@ -83,21 +101,28 @@ def get(self, request, commit_hash):
SUM(CASE WHEN t.status = 'SKIP' AND t.path NOT LIKE 'boot%%' THEN 1 ELSE 0 END)
AS non_boots_skip_count
FROM
checkouts AS c
relevant_checkouts AS c
INNER JOIN
builds AS b
on
ON
c.id = b.checkout_id
LEFT JOIN
tests AS t
ON b.id = t.build_id
WHERE
c.git_repository_branch = %(git_branch_param)s
AND c.git_repository_url = %(git_url_param)s
AND c.origin = %(origin_param)s
b.start_time >= (
SELECT
MIN(earliest_start_time)
FROM earliest_commits
)
AND t.start_time >= (
SELECT
MIN(earliest_start_time)
FROM earliest_commits
)
GROUP BY
c.git_commit_hash
)
)
SELECT
ec.git_commit_hash,
ec.git_commit_name,
Expand Down Expand Up @@ -125,15 +150,6 @@ def get(self, request, commit_hash):
INNER JOIN
test_counts AS tc
ON ec.git_commit_hash = tc.git_commit_hash
WHERE
ec.earliest_start_time <= (
SELECT
earliest_start_time
FROM
earliest_commits
WHERE
git_commit_hash = %(commit_hash)s
)
ORDER BY
ec.earliest_start_time DESC
LIMIT 6;
Expand Down

0 comments on commit 931a503

Please sign in to comment.