SlideShare ist ein Scribd-Unternehmen logo
1 von 13
postgreSQLを魔改造して
緊急対応した話
こういう案件もあるよ、っていうだけの話
書いた人
使ってる言語:だいたいbashとpython、oracleとpostgreSQL
好きなエディタ:サクラエディタ
最近のトピック:頭から血が出て入院した
最近のトレンド:slackの個人timelineで毎日使えない豆知識を披露
最近気になるもの:ヘルゲート 地獄の門 原題:Houndz from Hell(2011)
注意:Caution
本件は割と時間のない中、無理やりやった対応になります。
なので基本的には参考になりません。
正しいコーディング < 実行期限に間に合わせる
という状況のもとやった対応になるので、真似しないでください。
多分偉い人に怒られます。
状況
bash+postgreSQLで動いてるプログラムに、条件が1行追加された。
これを実現しようとするとかなりでかいテーブルを見る必要があり、
SQLを一応効率化したけど、56回動くプログラムだったので、1日中に終わらな
くて突き抜けて翌日に突入し、他プログラムにも迷惑をかけた。
状況2
遅いと思って後からログを見たら、一部のスクリプトがエラーになっていて正常
なデータを獲得できていなかった。
ERROR: canceling statement due to conflict with recovery
ただし、bash+postgreSQLの構成でこのエラーはエラー終了せず、正常終了して
いるように見える。鬼畜ステルスエラー。
selectがデカすぎて、色々あってpostgreSQLが途中で諦めることが原因。
とにかくクリティカルなレポートなので急ぎ修正+再実行が急務。
現状のプログラムと制約
・およそ600行くらいのプログラム(行数としては小さい)
・SQLを動的に作りまくるタイプ(とにかく読みづらい)
・現状の1回の実行は数分〜30分程度
ただし一部の実行が2時間以上かかりエラー ←こいつをどうにかしたい
・QAをする人の事も考えて修正は最小限にしたい(小手先でどうにかしたい)
・タイムリミットはおよそQAと実行を含め24時間
・数ヶ月後には今のロジック自体が必要なくなる(安心してね)
SQLの現状
解析してみると、同じSQLをselect句が違う形で7回実行している。
SQLの効率化はある程度できており、indexやヒント句でどうにかなりそうもない。
というか自分がpostgreSQLに詳しくないので、そんなに効率化できない。
インドの魔法(select句内select)はすでに使っている。
↑まあこれがpostgreSQLで早いかは、あんまりちゃんと検証してない
oracleじゃないので中間テーブルを作りまくる作戦が使えない(環境的に)
じゃあoracleでいいじゃん
postgreSQLのSQLを細かく分割し、joinを減らすほうが格段に早い→join負荷
必要部分の各テーブルは精々数百行程度→繋げるデータ自体は小さい
すでに別データの部分は一旦oracleにデータ出力してる場所がある→実績あり
よし、oracleだ ←この時点で夜3時なんで考えるのをやめた
でっけえSQLは大体そのままで、oracle化
現状 改善後
でっけえSQL
7回呼ぶ、基礎部分が
終わってるので早い
基礎部分を実行
中間テーブルを全て作成
コピー
7回呼ぶ
コピー
実行結果
現状:selectがデカすぎて2時間くらいかけてエラーで正常終了しない
改善後:多いデータの場合も、だいたい数分くらいで終わるようになった
良い点1:殆ど流用なので100行くらいの追加変更で済んだ。
良い点2:ロジック自体変更してないので、実行までにQAも間に合った。
まとめ
時間制約のキツイ、かなり特殊な案件だったので、無理やり対応しましたが、
本来であればpython+pandasにする案件だったと思います。
みんなも作るときに、SQL動的作成は出来るだけ辞めて、基礎部分と
データ利用部にちゃんと分けましょう。
まあ緊急措置ですがとりあえず動いたのでよし。あとDB自体を変えてるから、
ロジックが一緒でも割り算とか小数点とかの挙動が違うケースがあるので、
100%同一保証にはならない点も注意。
余談
あと、古いプログラムに念の為 set -e を入れたら超大量に関係ないところの
エラーが出まくったので、レガシーレポートを触るときには根気が必要。
しかもset -e 入れても、パイプのエラーの措置(set -eu -o pipefail)を入れても
postgreSQLのエラーはpsqlがエラーでも正常終了するので関係なかった。
後から知ったけど、psqlでON_ERROR_STOPをするとエラーストップ
できるらしい。
そして、ヘルゲート地獄の門はおすすめ、みんなで1時間を捨てよう。

Weitere ähnliche Inhalte

Empfohlen

How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfmarketingartwork
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Applitools
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at WorkGetSmarter
 

Empfohlen (20)

How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
 
ChatGPT webinar slides
ChatGPT webinar slidesChatGPT webinar slides
ChatGPT webinar slides
 

postgreSQLを魔改造して 緊急対応した話