Skip to Content
CockroachDB:決定版ガイド
book

CockroachDB:決定版ガイド

by Guy Harrison, Jesse Seldess, Ben Darnell
May 2025
Intermediate to advanced
488 pages
7h 56m
Japanese
O'Reilly Media, Inc.
Content preview from CockroachDB:決定版ガイド

第8章 SQLチューニング SQLチューニング

この作品はAIを使って翻訳されている。ご意見、ご感想をお待ちしている:translation-feedback@oreilly.com

CockroachDBは、スケーラブルな分散トランザクションデータベースシステムとして設計された。CockroachDBは、高性能で可用性が高く、グローバルに分散したアプリケーションの要求を満たすことができる。 しかし、アプリケーションコードやスキーマ設計の非効率性をすべて排除できる魔法の箱ではない。時折、開発者やアプリケーションは、必要なほど高速に動作しないSQL文をデータベースに送信する。その結果、有史以来、少なくともリレーショナルデータベースが出現して以来、SQLチューニングはデータベース開発者や管理者にとって大きな関心事となってきた。

この章では、CockroachDBがSQL文をどのように最適化するのか、またCockroachDBがこれらのクエリをより高速に実行できるようにするにはどうすればよいのかを説明する。また、チューニングが必要と思われるクエリを発見する方法、それらのSQL文が最適化されているかどうかを判断する方法、それらのクエリを高速化する方法についても説明する。

遅いSQLの発見

最新の 、包括的なクエリ統計が整備されていると仮定すると、クラスタを監視して問題のあるSQL文を特定する必要がある。

多くのアプリケーションはSQL文や論理トランザクション時間をログに記録し、パフォーマンスの悪いSQL文についての洞察を提供する。しかし、アプリケーションレベルのトレースがない場合、CockroachDB自体が全体的なSQL実行時間に関する情報を提供できる。

図8-1に示すように、CockroachDBコンソールのStatementsページから始めるのが最適であろう。

statementsPage
図8-1. CockroachDBコンソールのステートメントページ(形式拡大版)

States(ステートメント)ページには、基本的な実行統計とともに、サーバ上で実行された SQL ステートメントがリストされている。平均ステートメント時間または累積実行時間でソートして、注意が必要な SQL ステートメントを特定できる。特定の SQL 文をクリックすると、図 8-2 に示すように「Statement Details」に移動する。

チューニングが必要なSQL文を特定する方法は他にもいくつかある。SHOW STATEMENTS コマンドは、現在実行中のSQLステートメントを表示し、リソースを消費している長時間実行中のクエリを表示することができる。

CockroachDBは、クラスタ変数sql.log.slow_query.latency_threshold をゼロ以外の値にセットすることで、「遅い」クエリをログに記録することができる。 これにより、しきい値を超えるクエリが低速クエリ・ログにレコードを出力するようになる。ログ・レコードは以下のようになる:

I210809 07:47:09.663658 12467601 10@util/log/event_log.go:32 ⋮ [n1,client=‹192.168.0.245:57136›,hostnossl,user=root] 17 ={"Timestamp":1628495229538628000,"EventType":"slow_query","Statement": ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

実践的レイクハウス・アーキテクチャー

実践的レイクハウス・アーキテクチャー

Gaurav Ashok Thalpati
ゲームストーム2.0

ゲームストーム2.0

Dave Gray, Sunni Brown
実践者向けの説明可能なAI

実践者向けの説明可能なAI

Michael Munn, David Pitman

Publisher Resources

ISBN: 9798341651838