-
Notifications
You must be signed in to change notification settings - Fork 322
compute ddl script #460
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
Open
afleisc
wants to merge
4
commits into
master
Choose a base branch
from
compute_ddl
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
compute ddl script #460
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
93 changes: 93 additions & 0 deletions
93
scripts/optimization/compute_billing_model_savings_ddl.sql
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 |
---|---|---|
@@ -0,0 +1,93 @@ | ||
/* | ||
* Copyright 2024 Google LLC | ||
* | ||
* 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. | ||
*/ | ||
|
||
-- US pricing for reference: https://cloud.google.com/bigquery/pricing#storage | ||
-- standard_baseline_rate FLOAT64 DEFAULT .04; | ||
-- enterprise_baseline_1yr_rate FLOAT64 DEFAULT .048; | ||
-- enterprise_baseline_3yr_rate FLOAT64 DEFAULT .036; | ||
-- enterprise_plus_baseline_1yr_rate FLOAT64 DEFAULT .08; | ||
-- enterprise_plus_baseline_3yr_rate FLOAT64 DEFAULT .06; | ||
-- standard_payg_rate FLOAT64 DEFAULT .04; | ||
-- enterprise_payg_rate FLOAT64 DEFAULT .06; | ||
-- enterprise_plus_payg_rate FLOAT64 DEFAULT .1; | ||
|
||
DECLARE num_days_to_scan INT64 DEFAULT 30; | ||
DECLARE on_demand_rate FLOAT64 DEFAULT 6.25; | ||
-- REMEMBER: Replace with the values of your current or target baseline slot price. | ||
DECLARE baseline_rate FLOAT64 DEFAULT .04; | ||
-- REMEMBER: Replace with the values of your current or target autoscaling slot price. | ||
DECLARE autoscaling_rate DEFAULT .04; | ||
|
||
-- REMEMBER: (optional) Change this to filter based on savings absolute value or percentage | ||
DECLARE threshold_percent FLOAT64 DEFAULT 10.0; -- 10% difference | ||
DECLARE absolute_threshold FLOAT64 DEFAULT 100.0; -- $100 absolute difference | ||
|
||
-- REMEMBER: Set the Admin Project ID, Location, and Reservation Name | ||
DECLARE admin_project_id STRING DEFAULT 'your-admin-project-id'; -- Replace with your Admin Project ID | ||
DECLARE location STRING DEFAULT 'US'; -- Replace with your location | ||
DECLARE reservation_name STRING DEFAULT 'your-reservation-name'; -- Replace with your Reservation Name | ||
|
||
|
||
CREATE SCHEMA IF NOT EXISTS optimization_workshop; | ||
CREATE OR REPLACE TABLE optimization_workshop.compute_billing_model_savings_ddl AS ( | ||
WITH job_data AS ( | ||
SELECT | ||
project_id, | ||
TIMESTAMP_TRUNC(end_time, HOUR) AS time_window, | ||
SUM(total_slot_ms) as total_slot_ms, | ||
SUM((total_slot_ms/1000/60/60)) as total_slot_hours, | ||
SUM(total_bytes_billed/ POW(1024, 4)) as tb_billed, | ||
CASE | ||
WHEN reservation_id IS NULL THEN 'on_demand' | ||
ELSE 'reservation' | ||
END AS usage_type | ||
FROM | ||
`region-us`.INFORMATION_SCHEMA.JOBS_BY_ORGANIZATION as jbo | ||
WHERE | ||
job_type = 'QUERY' | ||
AND statement_type != 'SCRIPT' | ||
AND DATE(jbo.creation_time, "US/Central") >= CURRENT_DATE - 30 | ||
afleisc marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
GROUP BY | ||
project_id, time_window, usage_type | ||
), | ||
cost_analysis AS ( | ||
SELECT | ||
project_id, | ||
usage_type, | ||
sum(total_slot_hours) as total_slot_hours, | ||
sum(tb_billed) as tb_billed, | ||
sum(tb_billed)*on_demand_rate as cost_on_demand, | ||
sum(total_slot_hours)*autoscaling_rate as worst_case_cost_reservation, -- Worst case is that only autoscaling slots are used. | ||
sum(total_slot_hours)*baseline_rate as best_case_cost_reservation -- Best case is that only baseline slots are used. | ||
FROM job_data | ||
GROUP BY 1, 2 | ||
) | ||
SELECT | ||
*, | ||
-- Generate DDL templates with threshold logic and parameters | ||
CASE | ||
WHEN usage_type = 'on_demand' AND cost_on_demand > 0 AND ( | ||
(cost_on_demand - worst_case_cost_reservation) / cost_on_demand * 100 >= threshold_percent OR | ||
cost_on_demand - worst_case_cost_reservation >= absolute_threshold | ||
) THEN CONCAT('CREATE ASSIGNMENT `', admin_project_id, '.', 'region-', location, '.', reservation_name, '.ASSIGNMENT_ID` OPTIONS ( assignee="projects/', project_id, '", job_type="QUERY");') | ||
WHEN usage_type = 'reservation' AND cost_on_demand > 0 AND ( | ||
(best_case_cost_reservation - cost_on_demand) / cost_on_demand * 100 >= threshold_percent OR | ||
best_case_cost_reservation - cost_on_demand >= absolute_threshold | ||
) THEN CONCAT('CREATE ASSIGNMENT `', admin_project_id, '.', 'region-', location, '.', 'none', '.ASSIGNMENT_ID` OPTIONS ( assignee="projects/', project_id, '", job_type="QUERY");') | ||
ELSE NULL -- No DDL if threshold not met or cost_on_demand is zero | ||
END AS ddl | ||
FROM cost_analysis | ||
); | ||
afleisc marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
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.
Uh oh!
There was an error while loading. Please reload this page.