Ognacの雑感

木漏れ日々

目次

Blog 利用状況

書庫

ギャラリ

横持ちと縦持ち_その後3 ~  縦持ちを横持ちに置換して読み取る

前回、実RDB上は縦持ちで、アプリケーション上は横持ちでアプリケーションを構築した場合を考察しました。
次のストアードを使い、言語からは横持ちとして呼び、実RDBには縦持ちで書き出すものでした。

ALTER PROCEDURE [dbo].[縦24行挿入]
@社員CODE char(10) ,
@予定額1月 int, @実績額1月 int,…@予定額12月 int, @実績額12月 int
AS
BEGIN
  insert into 縦持ち(社員CODE,月,予実,額) values(@社員CODE,1,'Y',@予定額1月)
  insert into 縦持ち(社員CODE,月,予実,額) values(@社員CODE,1,'J',@実績額1月)
  …
  insert into 縦持ち(社員CODE,月,予実,額) values(@社員CODE,1,'Y',@予定額12月)
  insert into 縦持ち(社員CODE,月,予実,額) values(@社員CODE,1,'J',@実績額12月)
END

この処理を行わない、縦持ちと横持ちの書き込み速度比は 10倍(明示トランザクション時) 位の差がでました。
それを、上記のように入力を横持ちにして、書き出し時に縦に分解して書き出すと、その比は2~3倍まで縮まりました。かなり改善されます。

読み出し時のケースも試行してみました。100000人分の2400000行のデータを作って計測しました。
横持ちは社員CODE,  縦持ちは 社員CODE,月,予実 で索引を張りました。

sql文は次のようにしました。

string 横 = @"
select top n
社員CODE,予定額1月,実績額1月,予定額2月,実績額2月,予定額3月,実績額3月,
予定額4月,実績額4月, 予定額5月,実績額5月,予定額6月,実績額6月,予定額7月,実績額7月,予定額8月,実績額8月,予定額9月,実績額9月,
予定額10月,実績額10月, 予定額11月,実績額11月,予定額12月,実績額12月
 from  横持ち  order by 社員CODE";

string sqlz = @"select top n 社員CODE 
                    ,SUM(CASE WHEN 月 = 01 and 予実='Y' THEN 額 else null END) 予定額1月
                    ,SUM(CASE WHEN 月 = 01 and 予実='J' THEN 額 else null END) 実績額1月
                    ,SUM(CASE WHEN 月 = 02 and 予実='Y' THEN 額 else null END) 予定額2月
                    ,SUM(CASE WHEN 月 = 02 and 予実='J' THEN 額 else null END) 実績額2月
                    ,SUM(CASE WHEN 月 = 03 and 予実='Y' THEN 額 else null END) 予定額3月
                    ,SUM(CASE WHEN 月 = 03 and 予実='J' THEN 額 else null END) 実績額3月
                    ,SUM(CASE WHEN 月 = 04 and 予実='Y' THEN 額 else null END) 予定額4月
                    ,SUM(CASE WHEN 月 = 04 and 予実='J' THEN 額 else null END) 実績額4月
                    ,SUM(CASE WHEN 月 = 05 and 予実='Y' THEN 額 else null END) 予定額5月
                    ,SUM(CASE WHEN 月 = 05 and 予実='J' THEN 額 else null END) 実績額5月
                    ,SUM(CASE WHEN 月 = 06 and 予実='Y' THEN 額 else null END) 予定額6月
                    ,SUM(CASE WHEN 月 = 06 and 予実='J' THEN 額 else null END) 実績額6月
                    ,SUM(CASE WHEN 月 = 07 and 予実='Y' THEN 額 else null END) 予定額7月
                    ,SUM(CASE WHEN 月 = 07 and 予実='J' THEN 額 else null END) 実績額7月
                    ,SUM(CASE WHEN 月 = 08 and 予実='Y' THEN 額 else null END) 予定額8月
                    ,SUM(CASE WHEN 月 = 08 and 予実='J' THEN 額 else null END) 実績額8月
                    ,SUM(CASE WHEN 月 = 09 and 予実='Y' THEN 額 else null END) 予定額9月
                    ,SUM(CASE WHEN 月 = 09 and 予実='J' THEN 額 else null END) 実績額9月
                    ,SUM(CASE WHEN 月 = 10 and 予実='Y' THEN 額 else null END) 予定額10月
                    ,SUM(CASE WHEN 月 = 10 and 予実='J' THEN 額 else null END) 実績額10月
                    ,SUM(CASE WHEN 月 = 11 and 予実='Y' THEN 額 else null END) 予定額11月
                    ,SUM(CASE WHEN 月 = 11 and 予実='J' THEN 額 else null END) 実績額11月
                    ,SUM(CASE WHEN 月 = 12 and 予実='Y' THEN 額 else null END) 予定額12月
                    ,SUM(CASE WHEN 月 = 12 and 予実='J' THEN 額 else null END) 実績額12月
                    from  縦持ち  group by 社員CODE order by 社員CODE;

データ取得は DataTable dt = ur.Execute(sql);
の形で、アプリにDataTableで取得する形式をとりました。
(*) Top nの nは 1~10000000 まで、10^nの形で指定しましたが、top 100,000以上は幾ら指定しても100,000件しか、DataTableに戻らないのですね。
  もっとも、そんなに取得しても閲覧できないので、取得できても困りますが。

純粋な横持を取得すると次のようになりました。(単位:秒)

       1件取得  00.0140000■取得件数:1
      10件取得  00.0150000■取得件数:10
     100件取得  00.0170000■取得件数:100
    1000件取得  00.0480000■取得件数:1000 
   10000件取得  01.9130000■取得件数:10000
  100000件取得  03.3340000■取得件数:100000
 1000000件取得  02.9640000■取得件数:100000
10000000件取得  03.1790000■取得件数:100000

取得件数と処理時間の関連性は見られませんね。ネットワーク転送時間が大きな要因かもしれません。
1000件目と10,000件目の増加が大きいのは、処理のメモリー状態のReAllocなのか、なんなのか掴めてません。


縦持ちを横持ちに変換すると次のようになりました。(単位:秒)

       1件取得  00.0040000■取得件数:1
      10件取得  00.0080000■取得件数:10
     100件取得  00.0420000■取得件数:100
    1000件取得  00.3220000■取得件数:1000
   10000件取得  21.9130000■取得件数:10000
  100000件取得  22.7160000■取得件数:100000
 1000000件取得  24.6870000■取得件数:100000
10000000件取得  21.4410000■取得件数:100000

横持ちに比べると、相関性が見えるような見えないような。

こちらも横持ち共に 1000と10000の比が 極端に大きいのは、ADOの動作のような感じですが、判りませんでした。

横持ちと縦横変換を比較すると 次のようになります。

          横持ち     縦横変換  
     1件  00.014   00.004
    10件  00.015   00.008
   100件  00.017   00.042
  1000件  00.048   00.322
 10000件  01.913   21.913
100000件  03.334   22.716

100件のとき、縦横変換したほうが早かっり、10000件の時は 10倍以上、要したりしてます。
相関関係が一次的ではないですね。当初予想していたより差が小さかったので、妙に安心。 <-- 何が?

この程度の差でしたら、縦持ちにしておいて、見せかけ横持ちという構築でも、問題は少ないかなぁって思いました。(追記)1000件以下の時)

投稿日時 : 2009年6月16日 0:17

Feedback

# re: 横持ちと縦持ち_その後3 ~  縦持ちを横持ちに置換して読み取る 2009/06/16 1:46 Pasie.

 お疲れ様でした ^^;
 なにかこうしてみると、1件のときの数値が逆転するのはなぜ?というのがとても気になりますね。

 それはともかくも。
 >この程度の差でしたら(中略)問題は少ないかなぁって思いました。
 釈迦に説法ですが、パフォーマンスの問題は比率ではなくて絶対時間だ、と私は思ってます。要は極端には時間比が1:2ならOKで1:1000ならNGという話ではなくて、大切なのは要件にはまるかどうか、だと思うわけです。
 つまりパフォーマンス要件が1秒以内だとすると、0.1ms:100ms(1:1000)はOKですが、800ms:1600ms(1:2)はNGだということです。
 なのでパフォーマンスが叫ばれた時、要件をよく確かめつつ議論しないと、あらぬ方向に議論が飛んでいきがちなので、注意が必要だと思ってます。
 とはいえ、1/1000で済む処理を1000倍かけて実行するいわれもないわけなので、無駄に時間をかけているのは論外ですが。
 今回のケースでは、一旦DB設計で24行/年/社員になったのだとしたら、私としては24行1トランザクションとするところまでは必須で、それ以上のパフォーマンスが必要なら、そこからいろいろ検討する、ということになるでしょうか。バルクインサートを使えないかとか、予/実を横カラムにして12行に減らすとか、etc.etc..

 ああなんかまとまらなかった…けど寝ます><;

# re: 横持ちと縦持ち_その後3 ~  縦持ちを横持ちに置換して読み取る 2009/06/16 3:58 れい

うん。
結論は
「縦でも問題ない」
ですね。

やはり私は教科書的に縦に持つだろうと思います。

さて、横がいいと言っていた方々、どう思いになりますか?

# re: 横持ちと縦持ち_その後3 ~  縦持ちを横持ちに置換して読み取る 2009/06/16 10:05 trapemiya

(独り言)
縦横変換はleft joinを使った場合は速度的にどうなんだろうか? テーブル結合は遅そうなイメージがありますが・・・

pivotを使った場合は速いのかなぁ、遅いのかなぁ・・・?

# re: 横持ちと縦持ち_その後3 ~  縦持ちを横持ちに置換して読み取る 2009/06/16 21:22 Ognac

>縦横変換はleft joinを使った場合は速度的にどうなんだろうか?
うん? left joinで横展開.....具体的なSQL文が浮かびません。 項目毎に副SQL文にするのは、非現実的な気がしますし、

>pivotを使った場合は速いのかなぁ、遅いのかなぁ・・・?
こちらは、次回のエントリーで。

いろいろなケースが考えられるものですね。
SQL文、侮るべからず。(何事もですが。)

# re: 横持ちと縦持ち_その後3 ~  縦持ちを横持ちに置換して読み取る 2009/06/16 22:36 Pasie.

 はあーい。次はupdate派とdelete/insert派の比較検討がいいとおもいまーす(ぉひ #自分でヤレ -_-メ

# 縦も横も正解なんだと思う 2009/06/16 23:51 黒龍's Blog

縦も横も正解なんだと思う

# re: 横持ちと縦持ち_その後3 ~  縦持ちを横持ちに置換して読み取る 2009/06/17 9:35 trapemiya

>うん? left joinで横展開.....具体的なSQL文が浮かびません。 項目毎に副SQL文にするのは、非現実的な気がしますし、

「SQL Hacks」という本に紹介されていました。家にあるので具体的なSQLは確認できませんが、代わりに探してみました。

(初歩的なSQL文?)あるテーブルだけは行列を入れ替えたい
http://oshiete1.goo.ne.jp/qa2085277.html

# Why viewers still make use of to read news papers when in this technological world all is presented on net? 2018/10/05 1:43 Why viewers still make use of to read news papers

Why viewers still make use of to read news papers when in this technological world all
is presented on net?

# Outstanding post but I was wanting to know if you could write a litte more on this subject? I'd be very thankful if you could elaborate a little bit further. Many thanks! 2018/10/10 15:52 Outstanding post but I was wanting to know if you

Outstanding post but I was wanting to know if
you could write a litte more on this subject?
I'd be very thankful if you could elaborate a little bit
further. Many thanks!

# Sling tv coupons and promo codes for november 2018 As the admin of this web page is working, no doubt very soon it will be renowned, due to its feature contents. Sling tv coupons and promo codes for november 2018 2018/11/17 10:06 Sling tv coupons and promo codes for november 2018

Sling tv coupons and promo codes for november 2018
As the admin of this web page is working, no doubt very soon it will be renowned,
due to its feature contents. Sling tv coupons and promo codes for november 2018

# At this time I am going to do my breakfast, later than having my breakfast coming again to read more news. 2019/04/18 20:59 At this time I am going to do my breakfast, later

At this time I am going to do my breakfast, later than having my breakfast coming again to read more news.

# Good day! I know this is kind of off topic but I was wondering which blog platform are you using for this website? I'm getting tired of Wordpress because I've had problems with hackers and I'm looking at alternatives for another platform. I would be gre 2019/07/17 14:31 Good day! I know this is kind of off topic but I w

Good day! I know this is kind of off topic but I was wondering which blog platform are you using for
this website? I'm getting tired of Wordpress because I've
had problems with hackers and I'm looking at alternatives for another platform.

I would be great if you could point me in the direction of a good platform.

# Good day! I know this is kind of off topic but I was wondering which blog platform are you using for this website? I'm getting tired of Wordpress because I've had problems with hackers and I'm looking at alternatives for another platform. I would be gre 2019/07/17 14:32 Good day! I know this is kind of off topic but I w

Good day! I know this is kind of off topic but I was wondering which blog platform are you using for
this website? I'm getting tired of Wordpress because I've
had problems with hackers and I'm looking at alternatives for another platform.

I would be great if you could point me in the direction of a good platform.

# Good day! I know this is kind of off topic but I was wondering which blog platform are you using for this website? I'm getting tired of Wordpress because I've had problems with hackers and I'm looking at alternatives for another platform. I would be gre 2019/07/17 14:33 Good day! I know this is kind of off topic but I w

Good day! I know this is kind of off topic but I was wondering which blog platform are you using for
this website? I'm getting tired of Wordpress because I've
had problems with hackers and I'm looking at alternatives for another platform.

I would be great if you could point me in the direction of a good platform.

# Good day! I know this is kind of off topic but I was wondering which blog platform are you using for this website? I'm getting tired of Wordpress because I've had problems with hackers and I'm looking at alternatives for another platform. I would be gre 2019/07/17 14:34 Good day! I know this is kind of off topic but I w

Good day! I know this is kind of off topic but I was wondering which blog platform are you using for
this website? I'm getting tired of Wordpress because I've
had problems with hackers and I'm looking at alternatives for another platform.

I would be great if you could point me in the direction of a good platform.

# Why people still make use of to read news papers when in this technological globe everything is accessible on web? 2019/08/02 8:44 Why people still make use of to read news papers w

Why people still make use of to read news papers when in this
technological globe everything is accessible on web?

# What a material of un-ambiguity and preserveness of valuable familiarity on the topic of unexpected emotions. 2019/09/08 21:28 What a material of un-ambiguity and preserveness

What a material of un-ambiguity and preserveness of valuable familiarity on the
topic of unexpected emotions.

# It's going to be ending of mine day, except before finish I am reading this impressive article to increase my knowledge. 2021/08/09 15:36 It's going to be ending of mine day, except before

It's going to be ending of mine day, except before
finish I am reading this impressive article to increase my knowledge.

タイトル
名前
Url
コメント