psql
で以下コマンドを実行することで各SQLの実行時間を計測できます
\timing
実行すると Timing is on. と出力されます
SAMPLEDB=> \timing Timing is on.
コマンドを実行した後、SQLを発行すると以下のように実行時間が出力されます
SAMPLEDB=> select count(*) from samletbl1; count ------- 3210 (1 row) Time: 0.548 ms
psql
で以下コマンドを実行することで各SQLの実行時間を計測できます
\timing
実行すると Timing is on. と出力されます
SAMPLEDB=> \timing Timing is on.
コマンドを実行した後、SQLを発行すると以下のように実行時間が出力されます
SAMPLEDB=> select count(*) from samletbl1; count ------- 3210 (1 row) Time: 0.548 ms
「<インターネット>の次に来るもの 未来を決める12の法則」の気になった部分の抜粋です。
すごく面白い本です。正直言ってこのブログエントリ見ても何のこっちゃでしかないので、実際に書籍を読まれることをお勧めします。
私の抜粋には全章は登場しませんが、実際にはこの本は以下の12章から成り立っています。
PostgreSQLでのindexの作成方法や削除方法など
以下の構文でできます
CREATE INDEX {index名} ON {Table名} ({Column名});
これにより作成されるindexの種類はbtreeになります。
■実行例
SAMPLEDB=> CREATE INDEX sampletbl1_column1_idx ON sampletbl1 (column1); CREATE INDEX
CREATE UNIQUE INDEX {index名} ON {Table名} ({Column名});
indexの種類をbtree以外にしたい場合はUSING
で指定します。
例えばGiSTのindexを使用する場合は以下のようになります
CREATE INDEX {index名} ON {Table名} USING GIST ({Column名});
対象のindexが存在しない場合だけ作成する(存在する場合でもエラーにならないようにする)には以下の構文
CREATE INDEX IF NOT EXISTS {index名} ON {Table名} ({Column名});
以下の構文でできます。CREATE INDEXで指定したindex名を指定して
DROP INDEX {index名};
■実行例
SAMPLEDB=> DROP INDEX sampletbl1_column1_idx; DROP INDEX
対象のindexが存在する場合だけ削除する(存在しない場合でもエラーにならないようにする)には以下の構文
DROP INDEX IF EXISTS {index名};
psql
で以下操作をすることでindexの状況を各印できます
以下でindexの一覧を確認できます
\di
■実行例
SAMPLEDB=> \di List of relations Schema | Name | Type | Owner | Table --------+------------------------------------+-------+--------+------------------ public | sampletbl1_column1_idx | index | user | sampletbl1 public | sampletbl1_column2_idx | index | user | sampletbl1 public | sampletbl2_columnA_idx | index | user | sampletbl2 public | sampletbl2_columnB_idx | index | user | sampletbl2
以下でindexの詳細を確認できます
\d {index名}
■実行例
SAMPLEDB=> \d sampletbl1_column2_idx Index "public.sampletbl1_column2_idx" Column | Type | Definition --------------------+---------+-------------------- id | bigint | id column2 | integer | column2 primary key, btree, for table "public.sampletbl1"
bashのワンライナーループは何だかんだでよく使うので、ここからコピペする目的のためメモです。
基本的に以下で実行できる
for i in {1..<end>} ; do <something command> ; done
10回コマンドを実行するなら以下
for i in {1..10} ; do <something command> ; done
例えば10個のディレクトリ(dir1~dir10)を作成するなら以下のような感じ
for i in {1..10} ; do mkdir dir${i} ; done
例えば100個のファイル(file1.txt~file100.txt)を作成するなら以下のような感じ
for i in {1..100} ; do echo ${i} > file${i}.txt ; done
前回のエントリでND4J
を利用して、ベクトルの演算を試しました
今回は行列の演算を試してみます。
行列と行列の足し算です。
■サンプルコード
import org.nd4j.linalg.api.ndarray.INDArray; import org.nd4j.linalg.factory.Nd4j; INDArray matrixA = Nd4j.create(new double[]{1.0, 2.0, 3.0, 4.0}, new int[]{2, 2}); INDArray matrixB = Nd4j.create(new double[]{11.0, 12.0, 13.0, 14.0}, new int[]{2, 2}); INDArray matrixRet1 = matrixA.add(matrixB); System.out.println("Ret1: \n" + matrixRet1);
■実行結果
Ret1: [[12.00, 14.00], [16.00, 18.00]]
行列と行列の引き算です。
■サンプルコード
import org.nd4j.linalg.api.ndarray.INDArray; import org.nd4j.linalg.factory.Nd4j; INDArray matrixA = Nd4j.create(new double[]{1.0, 2.0, 3.0, 4.0}, new int[]{2, 2}); INDArray matrixB = Nd4j.create(new double[]{11.0, 12.0, 13.0, 14.0}, new int[]{2, 2}); INDArray matrixRet2 = matrixA.sub(matrixB); System.out.println("Ret2: \n" + matrixRet2);
■実行結果
Ret2: [[-10.00, -10.00], [-10.00, -10.00]]
行列をスカラー倍します。
■サンプルコード
import org.nd4j.linalg.api.ndarray.INDArray; import org.nd4j.linalg.factory.Nd4j; INDArray matrixA = Nd4j.create(new double[]{1.0, 2.0, 3.0, 4.0}, new int[]{2, 2}); INDArray matrixRet3 = matrixA.mul(3); System.out.println("Ret3: \n" + matrixRet3);
■実行結果
Ret3: [[3.00, 6.00], [9.00, 12.00]]
行列と行列のかけ算です
■サンプルコード
import org.nd4j.linalg.api.ndarray.INDArray; import org.nd4j.linalg.factory.Nd4j; INDArray matrixA = Nd4j.create(new double[]{1.0, 2.0, 3.0, 4.0}, new int[]{2, 2}); INDArray matrixB = Nd4j.create(new double[]{11.0, 12.0, 13.0, 14.0}, new int[]{2, 2}); INDArray matrixRet4 = matrixA.mmul(matrixB); System.out.println("Ret4: \n" + matrixRet4);
■実行結果
Ret4: [[37.00, 40.00], [85.00, 92.00]]
行列と零行列のかけ算です
■サンプルコード
import org.nd4j.linalg.api.ndarray.INDArray; import org.nd4j.linalg.factory.Nd4j; INDArray matrixA = Nd4j.create(new double[]{1.0, 2.0, 3.0, 4.0}, new int[]{2, 2}); INDArray zeroMatrix = Nd4j.zeros(2, 2); INDArray matrixRet5 = matrixA.mmul(zeroMatrix); System.out.println("Ret5: \n" + matrixRet5);
■実行結果
[[0.00, 0.00], [0.00, 0.00]]
行列と単位行列のかけ算です
■サンプルコード
import org.nd4j.linalg.api.ndarray.INDArray; import org.nd4j.linalg.factory.Nd4j; INDArray matrixA = Nd4j.create(new double[]{1.0, 2.0, 3.0, 4.0}, new int[]{2, 2}); INDArray identityMatrix = Nd4j.eye(2); INDArray matrixRet6 = matrixA.mmul(identityMatrix); System.out.println("Ret6: \n" + matrixRet6);
■実行結果
Ret6: [[1.00, 2.00], [3.00, 4.00]]
対象行列Cの転置行列を求めます
■サンプルコード
import org.nd4j.linalg.api.ndarray.INDArray; import org.nd4j.linalg.factory.Nd4j; INDArray matrixC = Nd4j.create(new double[]{1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0}, new int[]{3, 3}); INDArray matrixRet7 = matrixC.transpose(); System.out.println("Raw Matrix: \n" + matrixC); System.out.println("Transposed Matrix: \n" + matrixRet7);
■実行結果
Raw Matrix: [[1.00, 2.00, 3.00], [4.00, 5.00, 6.00], [7.00, 8.00, 9.00]] Transposed Matrix: [[1.00, 4.00, 7.00], [2.00, 5.00, 8.00], [3.00, 6.00, 9.00]]
対象行列Aの逆行列を求めます
■サンプルコード
import org.nd4j.linalg.api.ndarray.INDArray; import org.nd4j.linalg.factory.Nd4j; import org.nd4j.linalg.inverse.InvertMatrix; INDArray matrixC = Nd4j.create(new double[]{1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0}, new int[]{3, 3}); INDArray matrixRet8 = InvertMatrix.invert(matrixA, false); System.out.println("Raw Matrix: \n" + matrixA); System.out.println("Invert Matrix: \n" + matrixRet8); // 元の行列×逆行列=単位行列 System.out.println("identity Matrix: \n" + matrixA.mmul(matrixRet8));
■実行結果
Raw Matrix: [[1.00, 2.00], [3.00, 4.00]] Invert Matrix: [[-2.00, 1.00], [1.50, -0.50]] identity Matrix: [[1.00, 0.00], [0.00, 1.00]]