1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

SQLパフォーマンス詳解 2章:Where句_1. プライマリキー

Last updated at Posted at 2025-12-20

2章:Where句

1. プライマリキー

1.1: プライマリキーの基本動作

  • プライマリキーを定義すると
    DBは自動的に一意インデックス(B-tree)を作成する
     
  • WHERE句でプライマリキーを指定すると
    必ず1行に特定できる
SELECT *
FROM employees
WHERE employee_id = 1001;

1.2: 実行計画の特徴

  • 実行計画では INDEX UNIQUE SCAN
INDEX UNIQUE SCAN (EMPLOYEES_PK)

1.3: なぜ速いのか

  • B-treeインデックスを
     ルート → ブランチ → リーフ
     と辿るだけで目的の行に到達できる

  • 一意性が保証されているため
     複数行を探す必要がない

  • リーフノード間を横断する処理が発生しない

➡ 計算量は O(log N)

1.4: まとめ

  • WHERE句でプライマリキーを使う限り
    SQL自体が遅くなる可能性はほぼない

  • INDEX UNIQUE SCAN が出ているなら
    チューニング対象はSQL以外(アプリ処理・ロック等)

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?