金子邦彦研究室情報工学全般リレーショナルデータベース(全11回)二次索引 (2)

二次索引 (2)

大学で使用した自作の資料等を,手直しの上公開している. クリエイティブ・コモンズ BY NC SA.

リレーショナルデータベースの基礎であるテーブル定義,一貫性制約,SQL,結合と分解,トランザクション,埋め込みSQL,実行計画,二次索引を学ぶ.SQLite 3 を用いて,SQL についての演習も行う.

【サイト内のリレーショナルデータベース関連の資料】

要点

Sqliteman で既存のデータベースを開く (Open an existing database using Sqliteman)

すでに作成済みのデータベースを,下記の手順で開くことができる.

以下の手順で,既存のデータベースファイルを開く. (Open an existing database file)

  1. File」→ 「Open

    [image]
  2. データベースファイルを開く (Open Database File)

    Ubuntu での実行例(「/home/ubuntuuser/mydb」を開く場合)

    データベースファイル /home/ubuntuuser/mydb を選び, 「開く」をクリック (Click '開く' after choosing the database file "/home/ubuntuuser/mydb")

    [image]

    Windows での実行例(「C:\SQLite\mydb」を開く場合)

    データベースファイル C:\SQLite\mydb を選び, 「開く」をクリック (Click '開く' after choosing the database file "C:\SQLite\mydb")

    要するに,/home/<ユーザ名>/SQLite 3の mydb を選ぶ. 

    [image]

SQL を用いたテーブル定義と一貫性制約の記述 (Table definition and integrity constraint specification using SQL)

今回の演習では 前回の演習で作成したpoint3テーブルを用いる

前回の演習でpoint3テーブルを作成していなかった場合は、次の手順で作成する

SQL を用いて,point3 テーブルを定義し,一貫性制約を記述する. (Define 'point3' table and specify integrity constrants of the table using SQL)

リレーショナル・スキーマ (relational schema): point3(id, x, y, z, created_at)
  1. point3 テーブルの定義 (Define a table)

    次の SQL を入力し,「Run SQL」のアイコンをクリック (Write the following SQL, and click "Run SQL" icon).

    create table point3 (
        id          integer primary key autoincrement not null,
        x           real not null,
        y           real not null,
        z           real not null,
        created_at  datetime not null );
    

    ※ 「SQL Editor」のウインドウには,SQL プログラムを書くことができる. In the 'SQL string' window, you can write down SQL program(s).

    [image]
  2. コンソールの確認 (Inspect console)

    エラーメッセージが出ていないことを確認

    [image]
  3. SQL を用いたテーブルへの行の挿入 (Insert rows into a table using SQL)

    下記の操作により、演習用のデータ(1000行)を、point3 テーブルに格納する。

    1. Sqliteman で、point3右クリック (right click) し、「Populate Table...」を選ぶ.

      [image]
    2. Number of Rows to Populate に「1000」を設定する。 x の行, y の行, z の行は「Random Number」に設定し、 「Populate」をクリック

      [image]
    3. 確認 (Inspect console)

      エラーメッセージが出ていないことを確認

      [image]
    4. 「Close」をクリック

      [image]

Sqliteman を用いた SQL 問い合わせ計画の表示

単一テーブルに対する問い合わせの SQL 問い合わせ計画の表示例 (SQL query plan)

ここでは 条件を満足する行のみの表示する SQL のSQL 問い合わせ計画の表示例を示す.

データベース管理システムは, SQL 文をコンパイルし,SQL 問い合わせ計画を作る. SQL 問い合わせ計画とは,データベースに関する基本的なオペレータの並びである. (Database management system compliles a SQL statement into a SQL query plan. SQL query plan is a sequence or a tree of database operations ).

SQLite 3の問い合わせ計画については,SQLite Virtual Machine Opcodes の Web ページなどを見てください.(please refer to https://www.sqlite.org/opcode.html for SQLite opcodes)

Sqliteman を用いた二次索引の追加 (generate a secondary index using Sqliteman)

ここでは,テーブル point3 の属性 xの二次索引を作る. (Generate a secondary index of the table 'point3')
  1. CREATE INDEX を用いた二次索引の生成 (Generate a secondary index using 'CREATE INDEX')

    CREATE INDEX idx2 ON point3( x, y );
    

    「idx2は索引名である.索引の管理(索引の削除など)に使用される. ('idx2' is index name).

    [image]

    索引は数秒以内で生成される.(The secondary index will be generated in a several seconds)

  2. コンソールの確認 (Inspect console)

    エラーメッセージが出ていないことを確認

    [image]

SQL 問い合わせ計画の表示 (SQL query plan)

  1. 先ほどと同じ SQL 問い合わせを評価させる. SQL 文の前に「EXPLAIN」を付けている.(Add 'EXPLAIN' before a SQL statement)
    EXPLAIN SELECT * FROM point3 WHERE x < 1000000000 AND y < 1000000000;
    

    [image]

    索引を使うような実行計画になっている