データ投入
ようやく準備ができたので,データを投入してみた.
まずテーブルを確認してみる.
benchmark=> \d cluster
Table "cluster"
Column | Type | Modifiers
----------+---------+-----------
id | integer | not null
name | text | not null
cpunum | integer |
cpuclock | integer |
memory | integer |
network | text |
Primary key: cluster_pkey
Triggers: RI_ConstraintTrigger_24859,
RI_ConstraintTrigger_24861,
RI_ConstraintTrigger_24871,
RI_ConstraintTrigger_24873
idとnameがNOT NULLである.まずINSERTを使用してデータを投入してみる.全属性を指定してINSERTする場合は以下の形式でINSERTできる.
INSERT INTO テーブル名 VALUES(値, 値, ...);実際にやってみる.
benchmark=> INSERT INTO cluster VALUES (1, 'hoge', 256, 1.5, 1024, '1000BASE-TX'); INSERT 24875 1 benchmark=> select * FROM cluster ; id | name | cpunum | cpuclock | memory | network ----+------+--------+----------+--------+------------- 1 | hoge | 256 | 2 | 1024 | 1000BASE-TX (1 row)NOT NULLではないものに対してはデータを指定できない場合もある.そのような場合は行値コンストラクタを使用した形式でINSERTできる.
INSERT INTO テーブル名(属性a, 属性b...) VALUES(属性aの値, 属性bの値, ...);当然ながらテーブル名の後の()内とVALUESの後の()内の順番は対応していなければならない.実際にやってみる.
benchmark=> INSERT INTO cluster(id, name) VALUES (2, 'fuga'); INSERT 24877 1 benchmark=> INSERT INTO cluster(id, name, memory) VALUES (3, 'foo', 256); INSERT 24879 1 benchmark=> INSERT INTO cluster(id, name, cpunum, cpuclock, memory) VALUES (4, 'var', 512, 3, 2048); INSERT 24880 1 benchmark=> SELECT * FROM cluster ; id | name | cpunum | cpuclock | memory | network ----+------+--------+----------+--------+------------- 1 | hoge | 256 | 2 | 1024 | 1000BASE-TX 2 | fuga | | | | 3 | foo | | | 256 | 4 | var | 512 | 3 | 2048 | (4 rows)念のため,データをファイルにとっておく.これにはPostgreSQL特有のコマンドcopyを使用する.
\copy テーブル名 to ファイル名実際にやってみる.
benchmark=> \copy cluster to cluster-records値を入力していないフィールドには\Nと出力されるようだ(cluster-records).
念のため.本頁で作成した計測データは実際に計測したものであるが,テーブルclusterの内容がデタラメである.本頁の目的はPostgreSQLでDBを構築してみることなので,本頁のデータの内容自体に関しては気にしていない.逆に言うと本頁のデータの内容を信用しないように.
まず確認.
benchmark=> \d result_linpack
Table "result_linpack"
Column | Type | Modifiers
------------+------------------+-----------
n | integer | not null
nb | integer | not null
cluster_id | integer | not null
gflops | double precision | not null
exec_date | date |
Primary key: result_linpack_pkey
Triggers: RI_ConstraintTrigger_24857
cluster_idを外部キーにしておいたことに気をつける.先節でcluster_idが参照するcluster.idは1〜4まで投入してある.実際の利用を想定すると,ベンチマーク計測データは大抵一度に沢山のデータを投入したくなるものだと思う.そこで,今回はツールを使用してテキストファイルにデータを記述し,それをcopyで読み込む方法をとる.以下データ作成作業.
$ hpl_filter.pl --field N --field NB --field GFLOPS --filterw W00L2L2 --data hpl1.out |\
> awk '// {print $1 "\t" $2 "\t1\t" $3 "\t\\N"}' - >> result_linpack-records
$ hpl_filter.pl --field N --field NB --field GFLOPS --filterw W00L2L2 --data hpl2.out |\
> awk '// {print $1 "\t" $2 "\t2\t" $3 "\t\\N"}' - >> result_linpack-records
$ hpl_filter.pl --field N --field NB --field GFLOPS --filterw W00L2L2 --data hpl3.out |\
> awk '// {print $1 "\t" $2 "\t3\t" $3 "\t\\N"}' - >> result_linpack-records
$ hpl_filter.pl --field N --field NB --field GFLOPS --filterw W00L2L2 --data hpl4.out |\
> awk '// {print $1 "\t" $2 "\t4\t" $3 "\t2003-12-13"}' - >> result_linpack-records
クラスタvar(cluster_id=4)以外は計測日付がわからないというシナリオにしてみた.また,テーブルのフィールドや一意制約の都合上,hpl_filterでフィルタリングした(result_linpack-records).投入にはcopyを使用する.
\copy テーブル名 from ファイル名copyではテーブル名とファイル名の間がtoかfromかによって,データの取り込みか吐き出しかが決まる.実際にやってみる.
benchmark=> \copy result_linpack from result_linpack-records \.「\.」と出力されたので,無事成功したようだ.
benchmark=> SELECT * from result_linpack ; n | nb | cluster_id | gflops | exec_date -------+-----+------------+--------+------------ 10000 | 120 | 1 | 3.242 | 11000 | 120 | 1 | 3.467 | 12000 | 120 | 1 | 3.64 | 13000 | 120 | 1 | 3.904 | 14000 | 120 | 1 | 4.059 | ... 20000 | 240 | 4 | 10.31 | 2003-12-13 21000 | 120 | 4 | 8.255 | 2003-12-13 (56 rows)psqlはレコード数が多いと環境変数PAGERで指定したpagerで表示してくれるようだ(※).
※\?で\psetを参照.以下でpagerの使用/不使用を切替える.
\pset pager\psetは他にもいろいろなものの使用/不使用を切替えるのに使用できる.
まず確認.
benchmark=> \d result_netpipe
Table "result_netpipe"
Column | Type | Modifiers
------------+------------------+-----------
type | text | not null
size | integer | not null
cluster_id | integer | not null
mbps | double precision | not null
exec_date | date |
Primary key: result_netpipe_pkey
Triggers: RI_ConstraintTrigger_24869
result_linpackと同様に,以下のような要領でデータファイルを作成する.
cat netpipe-data |\
> awk '// {print "lam\t", $1, "\t3\t", $2, "\t", "2003-05-03"}' - >> result_netpipe-records
作成したファイルresult_netpipe-records(※).では,投入して確認.
benchmark=> \copy result_netpipe from result_netpipe-records \. benchmark=> SELECT * FROM result_netpipe ; type | size | cluster_id | mbps | exec_date -------+-----------+------------+------------+------------ lam | 1 | 1 | 0.111561 | 2003-12-01 lam | 2 | 1 | 0.220551 | 2003-12-01 lam | 3 | 1 | 0.3412 | 2003-12-01 lam | 4 | 1 | 0.423666 | 2003-12-01 ... tcp | 25165821 | 2 | 172.300624 | 2003-05-03 tcp | 25165824 | 2 | 172.293367 | 2003-05-03 tcp | 25165827 | 2 | 172.308917 | 2003-05-03 (971 rows)
※netpipeの出力データにはスペースが紛れ込んでいるようなので

