making anti pattern opitmization script generic for any input table (… · bufferapp/bigquery-utils@eb5a105 · GitHub
Skip to content

Commit eb5a105

Browse files
making anti pattern opitmization script generic for any input table (GoogleCloudPlatform#399)
* adding anti pattern recognition step to optimization scripts * using viewable_queries_grouped_by_hash for anti pattern processing * moving anti pattern recognition tool steps to separate script * fixing bug in column names * fixing bug in column names * adding anti pattern script, accounting for null has * adding anti pattern script, supporting multiple executions * adding anti pattern script, addressing duplicate hashes * adding anti pattern script, addressing duplicate hashes * making anti pattern opitmization script generic for any input table, removing query column from queries_grouped_by_hash at org level, adding a project level version of queries_grouped_by_hash * updating readme with examples on how to execute the anti pattern optimization script * updating readme with examples on how to execute the anti pattern optimization script and removing test file * changing location of the anti pattern optimization script for clarity * enhancing readme with instructions to run the anti pattern optimization scripts
1 parent c5c3c14 commit eb5a105

6 files changed

Lines changed: 153 additions & 18 deletions

File tree

‎scripts/optimization/README.md‎

Lines changed: 16 additions & 3 deletions

scripts/optimization/anti_pattern_recoginition_tool_tables.sql renamed to scripts/optimization/anti-pattern-recognittion-tool-scripts/anti_pattern_recoginition_tool_tables.sql

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,16 @@ CREATE OR REPLACE TABLE optimization_workshop.antipattern_output_table (
2626

2727
CREATE OR REPLACE VIEW optimization_workshop.antipattern_tool_input_view AS
2828
SELECT
29-
Query_Hash id,
30-
ANY_VALUE(Query_Raw_Sample) query,
29+
<input_table_id_col_name> id,
30+
ANY_VALUE(<input_table_query_text_col_name>) query
3131
FROM
32-
optimization_workshop.viewable_queries_grouped_by_hash
32+
<input_table>
3333
WHERE
34-
Query_Hash is not null
34+
<input_table_id_col_name> is not null
3535
GROUP BY
36-
Query_Hash
36+
<input_table_id_col_name>
3737
ORDER BY
38-
ANY_VALUE(Total_Slot_Hours) desc
38+
ANY_VALUE(<input_table_slots_col_name>) desc
3939
LIMIT
4040
1000
4141
;

scripts/optimization/run_anti_pattern_tool.sh renamed to scripts/optimization/anti-pattern-recognittion-tool-scripts/run_anti_pattern_tool.sh

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,35 @@
1616

1717
# Exit immediately if a command exits with a non-zero status.
1818
set -e
19+
20+
# Get input_table name as input
21+
for i in "$@"; do
22+
case $i in
23+
--input_table_name=*)
24+
input_table_name="${i#*=}"
25+
shift # past argument=value
26+
;;
27+
--input_table_id_col_name=*)
28+
input_table_id_col_name="${i#*=}"
29+
shift # past argument=value
30+
;;
31+
--input_table_query_text_col_name=*)
32+
input_table_query_text_col_name="${i#*=}"
33+
shift # past argument=value
34+
;;
35+
--input_table_slots_col_name=*)
36+
input_table_slots_col_name="${i#*=}"
37+
shift # past argument=value
38+
;;
39+
-*|--*)
40+
echo "Unknown option $i"
41+
exit 1
42+
;;
43+
*)
44+
;;
45+
esac
46+
done
47+
1948
# Set the following flags for the bq command:
2049
# --quiet: suppress status updates while jobs are running
2150
# --nouse_legacy_sql: use standard SQL syntax
@@ -24,7 +53,13 @@ bq_flags="--quiet --nouse_legacy_sql --nouse_cache"
2453

2554

2655
# Run setup for anti pattern recognition tool
27-
bq query ${bq_flags} <anti_pattern_recoginition_tool_tables.sql
56+
anti_pattern_recoginition_tool_tables_sql=$(sed -e "s/<input_table>/$input_table_name/g" \
57+
-e "s/<input_table_id_col_name>/$input_table_id_col_name/g" \
58+
-e "s/<input_table_query_text_col_name>/$input_table_query_text_col_name/g" \
59+
-e "s/<input_table_slots_col_name>/$input_table_slots_col_name/g" \
60+
"./anti-pattern-recognittion-tool-scripts/anti_pattern_recoginition_tool_tables.sql")
61+
62+
bq query ${bq_flags} <<< "$anti_pattern_recoginition_tool_tables_sql"
2863

2964
{ # try
3065

@@ -38,8 +73,11 @@ bq query ${bq_flags} <anti_pattern_recoginition_tool_tables.sql
3873
--input_bq_table ${PROJECT_ID}.optimization_workshop.antipattern_tool_input_view \
3974
--output_table ${PROJECT_ID}.optimization_workshop.antipattern_output_table
4075

41-
# write anti pattern output to queries by has table
42-
bq query ${bq_flags} <update_queries_by_hash_w_anti_patterns.sql
76+
# write anti pattern output to queries by has table
77+
update_queries_by_hash_w_anti_patterns_sql=$(sed -e "s/<input_table>/$input_table_name/g" \
78+
-e "s/<input_table_id_col_name>/$input_table_id_col_name/g" \
79+
"./anti-pattern-recognittion-tool-scripts/update_queries_by_hash_w_anti_patterns.sql")
80+
bq query ${bq_flags} <<< "$update_queries_by_hash_w_anti_patterns_sql"
4381

4482
} || { # catch
4583
echo 'Error: could not run Anti-pattern Recognition Tool. Try using GCP Cloud Shell https://cloud.google.com/shell/docs/launching-cloud-shell'

scripts/optimization/update_queries_by_hash_w_anti_patterns.sql renamed to scripts/optimization/anti-pattern-recognittion-tool-scripts/update_queries_by_hash_w_anti_patterns.sql

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@
1414
* limitations under the License.
1515
*/
1616

17-
ALTER TABLE optimization_workshop.viewable_queries_grouped_by_hash
17+
ALTER TABLE <input_table>
1818
ADD COLUMN IF NOT EXISTS recommendation ARRAY<STRUCT<name STRING, description STRING>>;
1919

20-
UPDATE optimization_workshop.viewable_queries_grouped_by_hash t1
20+
UPDATE <input_table> t1
2121
SET t1.recommendation = t2.recommendation
2222
FROM optimization_workshop.antipattern_output_table t2
23-
WHERE t1.Query_Hash = t2.job_id;
23+
WHERE t1.<input_table_id_col_name> = t2.job_id;

scripts/optimization/queries_grouped_by_hash.sql renamed to scripts/optimization/queries_grouped_by_hash_org.sql

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ CREATE TEMP FUNCTION num_stages_with_perf_insights(query_info ANY TYPE) AS (
4141
);
4242

4343
CREATE SCHEMA IF NOT EXISTS optimization_workshop;
44-
CREATE OR REPLACE TABLE optimization_workshop.queries_grouped_by_hash AS
44+
CREATE OR REPLACE TABLE optimization_workshop.queries_grouped_by_hash_org AS
4545
SELECT
4646
statement_type,
4747
query_info.query_hashes.normalized_literals AS query_hash,
@@ -53,8 +53,7 @@ SELECT
5353
ARRAY_AGG(
5454
STRUCT(
5555
bqutil.fn.job_url(project_id || ':us.' || parent_job_id) AS parent_job_url,
56-
bqutil.fn.job_url(project_id || ':us.' || job_id) AS job_url,
57-
query as query_text
56+
bqutil.fn.job_url(project_id || ':us.' || job_id) AS job_url
5857
)
5958
ORDER BY total_slot_ms
6059
DESC LIMIT 10) AS top_10_jobs,
Lines changed: 85 additions & 0 deletions

0 commit comments

Comments
 (0)