-
Notifications
You must be signed in to change notification settings - Fork 10
perf(monitor): avoid querying the same thing twice, avoid seq scans #82
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
a79ad45
perf(monitor): Replace seq/table scan w/ two index scans
smudge e4ace21
perf(monitor): avoid querying the same thing ('claimed'/'working') twice
smudge ac55b22
Appease the linter
smudge f9b904a
Adjust after rebase (new 'legacy' index explains)
smudge File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,6 +4,17 @@ SELECT COUNT(*) AS count_all, CASE WHEN priority >= 0 | |
| AND priority < 20 THEN 10 WHEN priority >= 20 | ||
| AND priority < 30 THEN 20 WHEN priority >= 30 THEN 30 END AS case_when_priority_0_and_priority_10_then_0_when_priority_10_an, \"delayed_jobs\".\"queue\" AS delayed_jobs_queue | ||
| FROM \"delayed_jobs\" | ||
| WHERE \"delayed_jobs\".\"failed_at\" IS NOT NULL | ||
| GROUP BY CASE WHEN priority >= 0 | ||
| AND priority < 10 THEN 0 WHEN priority >= 10 | ||
| AND priority < 20 THEN 10 WHEN priority >= 20 | ||
| AND priority < 30 THEN 20 WHEN priority >= 30 THEN 30 END, \"delayed_jobs\".\"queue\" | ||
| SELECT COUNT(*) AS count_all, CASE WHEN priority >= 0 | ||
| AND priority < 10 THEN 0 WHEN priority >= 10 | ||
| AND priority < 20 THEN 10 WHEN priority >= 20 | ||
| AND priority < 30 THEN 20 WHEN priority >= 30 THEN 30 END AS case_when_priority_0_and_priority_10_then_0_when_priority_10_an, \"delayed_jobs\".\"queue\" AS delayed_jobs_queue | ||
| FROM \"delayed_jobs\" | ||
| WHERE \"delayed_jobs\".\"failed_at\" IS NULL | ||
| GROUP BY CASE WHEN priority >= 0 | ||
| AND priority < 10 THEN 0 WHEN priority >= 10 | ||
| AND priority < 20 THEN 10 WHEN priority >= 20 | ||
|
|
@@ -17,7 +28,15 @@ GroupAggregate (cost=...) | |
| -> Sort (cost=...) | ||
| Output: (CASE WHEN ((priority >= 0) AND (priority < 10)) THEN 0 WHEN ((priority >= 10) AND (priority < 20)) THEN 10 WHEN ((priority >= 20) AND (priority < 30)) THEN 20 WHEN (priority >= 30) THEN 30 ELSE NULL::integer END), queue | ||
| Sort Key: (CASE WHEN ((delayed_jobs.priority >= 0) AND (delayed_jobs.priority < 10)) THEN 0 WHEN ((delayed_jobs.priority >= 10) AND (delayed_jobs.priority < 20)) THEN 10 WHEN ((delayed_jobs.priority >= 20) AND (delayed_jobs.priority < 30)) THEN 20 WHEN (delayed_jobs.priority >= 30) THEN 30 ELSE NULL::integer END), delayed_jobs.queue | ||
| -> Seq Scan on public.delayed_jobs (cost=...) | ||
| -> Index Only Scan using idx_delayed_jobs_failed on public.delayed_jobs (cost=...) | ||
| Output: CASE WHEN ((priority >= 0) AND (priority < 10)) THEN 0 WHEN ((priority >= 10) AND (priority < 20)) THEN 10 WHEN ((priority >= 20) AND (priority < 30)) THEN 20 WHEN (priority >= 30) THEN 30 ELSE NULL::integer END, queue | ||
| GroupAggregate (cost=...) | ||
| Output: count(*), (CASE WHEN ((priority >= 0) AND (priority < 10)) THEN 0 WHEN ((priority >= 10) AND (priority < 20)) THEN 10 WHEN ((priority >= 20) AND (priority < 30)) THEN 20 WHEN (priority >= 30) THEN 30 ELSE NULL::integer END), queue | ||
| Group Key: (CASE WHEN ((delayed_jobs.priority >= 0) AND (delayed_jobs.priority < 10)) THEN 0 WHEN ((delayed_jobs.priority >= 10) AND (delayed_jobs.priority < 20)) THEN 10 WHEN ((delayed_jobs.priority >= 20) AND (delayed_jobs.priority < 30)) THEN 20 WHEN (delayed_jobs.priority >= 30) THEN 30 ELSE NULL::integer END), delayed_jobs.queue | ||
| -> Sort (cost=...) | ||
| Output: (CASE WHEN ((priority >= 0) AND (priority < 10)) THEN 0 WHEN ((priority >= 10) AND (priority < 20)) THEN 10 WHEN ((priority >= 20) AND (priority < 30)) THEN 20 WHEN (priority >= 30) THEN 30 ELSE NULL::integer END), queue | ||
| Sort Key: (CASE WHEN ((delayed_jobs.priority >= 0) AND (delayed_jobs.priority < 10)) THEN 0 WHEN ((delayed_jobs.priority >= 10) AND (delayed_jobs.priority < 20)) THEN 10 WHEN ((delayed_jobs.priority >= 20) AND (delayed_jobs.priority < 30)) THEN 20 WHEN (delayed_jobs.priority >= 30) THEN 30 ELSE NULL::integer END), delayed_jobs.queue | ||
| -> Index Only Scan using idx_delayed_jobs_live on public.delayed_jobs (cost=...) | ||
| Output: CASE WHEN ((priority >= 0) AND (priority < 10)) THEN 0 WHEN ((priority >= 10) AND (priority < 20)) THEN 10 WHEN ((priority >= 20) AND (priority < 30)) THEN 20 WHEN (priority >= 30) THEN 30 ELSE NULL::integer END, queue | ||
| SNAP | ||
|
|
||
|
|
@@ -30,6 +49,16 @@ GroupAggregate (cost=...) | |
| Sort Key: (CASE WHEN ((delayed_jobs.priority >= 0) AND (delayed_jobs.priority < 10)) THEN 0 WHEN ((delayed_jobs.priority >= 10) AND (delayed_jobs.priority < 20)) THEN 10 WHEN ((delayed_jobs.priority >= 20) AND (delayed_jobs.priority < 30)) THEN 20 WHEN (delayed_jobs.priority >= 30) THEN 30 ELSE NULL::integer END), delayed_jobs.queue | ||
| -> Seq Scan on public.delayed_jobs (cost=...) | ||
| Output: CASE WHEN ((priority >= 0) AND (priority < 10)) THEN 0 WHEN ((priority >= 10) AND (priority < 20)) THEN 10 WHEN ((priority >= 20) AND (priority < 30)) THEN 20 WHEN (priority >= 30) THEN 30 ELSE NULL::integer END, queue | ||
| Filter: (delayed_jobs.failed_at IS NOT NULL) | ||
| GroupAggregate (cost=...) | ||
| Output: count(*), (CASE WHEN ((priority >= 0) AND (priority < 10)) THEN 0 WHEN ((priority >= 10) AND (priority < 20)) THEN 10 WHEN ((priority >= 20) AND (priority < 30)) THEN 20 WHEN (priority >= 30) THEN 30 ELSE NULL::integer END), queue | ||
| Group Key: (CASE WHEN ((delayed_jobs.priority >= 0) AND (delayed_jobs.priority < 10)) THEN 0 WHEN ((delayed_jobs.priority >= 10) AND (delayed_jobs.priority < 20)) THEN 10 WHEN ((delayed_jobs.priority >= 20) AND (delayed_jobs.priority < 30)) THEN 20 WHEN (delayed_jobs.priority >= 30) THEN 30 ELSE NULL::integer END), delayed_jobs.queue | ||
| -> Sort (cost=...) | ||
| Output: (CASE WHEN ((priority >= 0) AND (priority < 10)) THEN 0 WHEN ((priority >= 10) AND (priority < 20)) THEN 10 WHEN ((priority >= 20) AND (priority < 30)) THEN 20 WHEN (priority >= 30) THEN 30 ELSE NULL::integer END), queue | ||
| Sort Key: (CASE WHEN ((delayed_jobs.priority >= 0) AND (delayed_jobs.priority < 10)) THEN 0 WHEN ((delayed_jobs.priority >= 10) AND (delayed_jobs.priority < 20)) THEN 10 WHEN ((delayed_jobs.priority >= 20) AND (delayed_jobs.priority < 30)) THEN 20 WHEN (delayed_jobs.priority >= 30) THEN 30 ELSE NULL::integer END), delayed_jobs.queue | ||
| -> Seq Scan on public.delayed_jobs (cost=...) | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
| Output: CASE WHEN ((priority >= 0) AND (priority < 10)) THEN 0 WHEN ((priority >= 10) AND (priority < 20)) THEN 10 WHEN ((priority >= 20) AND (priority < 30)) THEN 20 WHEN (priority >= 30) THEN 30 ELSE NULL::integer END, queue | ||
| Filter: (delayed_jobs.failed_at IS NULL) | ||
| SNAP | ||
|
|
||
| snapshots["runs the expected postgresql query for future_count 1"] = <<-SNAP | ||
|
|
@@ -400,20 +429,35 @@ SELECT COUNT(*) AS count_all, CASE WHEN priority >= 0 | |
| AND priority < 20 THEN 10 WHEN priority >= 20 | ||
| AND priority < 30 THEN 20 WHEN priority >= 30 THEN 30 END AS case_when_priority_0_and_priority_10_then_0_when_priority_10_an, \"delayed_jobs\".\"queue\" AS delayed_jobs_queue | ||
| FROM \"delayed_jobs\" | ||
| WHERE \"delayed_jobs\".\"failed_at\" IS NOT NULL | ||
| GROUP BY CASE WHEN priority >= 0 | ||
| AND priority < 10 THEN 0 WHEN priority >= 10 | ||
| AND priority < 20 THEN 10 WHEN priority >= 20 | ||
| AND priority < 30 THEN 20 WHEN priority >= 30 THEN 30 END, \"delayed_jobs\".\"queue\" | ||
| SELECT COUNT(*) AS count_all, CASE WHEN priority >= 0 | ||
| AND priority < 10 THEN 0 WHEN priority >= 10 | ||
| AND priority < 20 THEN 10 WHEN priority >= 20 | ||
| AND priority < 30 THEN 20 WHEN priority >= 30 THEN 30 END AS case_when_priority_0_and_priority_10_then_0_when_priority_10_an, \"delayed_jobs\".\"queue\" AS delayed_jobs_queue | ||
| FROM \"delayed_jobs\" | ||
| WHERE \"delayed_jobs\".\"failed_at\" IS NULL | ||
| GROUP BY CASE WHEN priority >= 0 | ||
| AND priority < 10 THEN 0 WHEN priority >= 10 | ||
| AND priority < 20 THEN 10 WHEN priority >= 20 | ||
| AND priority < 30 THEN 20 WHEN priority >= 30 THEN 30 END, \"delayed_jobs\".\"queue\" | ||
| SNAP | ||
|
|
||
| snapshots["produces the expected sqlite3 query plan for count 1"] = <<-SNAP | ||
| SCAN delayed_jobs | ||
| SCAN delayed_jobs USING INDEX idx_delayed_jobs_failed | ||
| USE TEMP B-TREE FOR GROUP BY | ||
| SCAN delayed_jobs USING INDEX idx_delayed_jobs_live | ||
| USE TEMP B-TREE FOR GROUP BY | ||
| SNAP | ||
|
|
||
| snapshots["[legacy index] produces the expected sqlite3 query plan for count 1"] = <<-SNAP | ||
| SCAN delayed_jobs | ||
| USE TEMP B-TREE FOR GROUP BY | ||
| SCAN delayed_jobs | ||
| USE TEMP B-TREE FOR GROUP BY | ||
| SNAP | ||
|
|
||
| snapshots["runs the expected sqlite3 query for future_count 1"] = <<-SNAP | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe a bit of a stretch, but this makes me think that a separate class that can calc the metrics might be helpful, and this class is more in charge of the looping/sleeping mechanics. then we don't need this overwritable internal state, we just new up a new metric-calc-class during each call to
run!. I see thatRunnablehandles some of this, but i guess organizing it that way means we have this notion of resettable state which is a bit smellyThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, agreed -- I was also starting to think about how we could reimagine
Runnableto take on less of the whole lifecycle ofDelayed::MonitorandDelayed::Workerkinds of classes.If I keep pulling on that thread, I imagine that this smelliness will go away, but I didn't want to refactor too much within this one PR.