さくらVPSでDBを立てていたんだけど、どうも重い。AWSへの乗り換えの検討の為色々比較してみた。
select文をEXPLAIN ANALYZEしただけという前提はあるが、$5/hのdb.m4.10xlargeと$0.1/hのdb.t2.mediumが5%しか違わないのがへっ?となった。
ソートはBのAVG。t2.midiumが4069でm4.10xlargeが3955。オリジナルのExcelファイルはこちら https://github.com/fushihara/aws-rds-cost
ただt2もmidiumとlargeは早いけどsmallとmicroになると4倍の時間がかかってるから、クエリがメモリに入り切らなくなった?ら急に重くなる可能性がかなりあるのかもしれない。そうなるとm4の一番少ないlargeがベストかもしれない。しかし月2万円。DBはお高い。
以下、比較条件
以下のクエリを10回投げて、かかった時間をソート。一番多い値と少ない値を一つづつ取り除いて8個の最大値・最小値・平均を取った。クエリAとBがあるけど、上記の図のBというのはクエリBの事。こっちの方が重いのでそっちの値を図に出した。
クエリA
select *
from home_timeline_tweet_ids left join tweet on home_timeline_tweet_ids.tweet_id = tweet.tweet_id
where home_timeline_tweet_ids.user_id = 123456 order by home_timeline_tweet_ids.tweet_id desc limit 100
クエリB
select to_char(tweet.created_at, 'yyyy/mm/dd') as time , count(*) as cnt from home_timeline_tweet_ids
left join tweet on home_timeline_tweet_ids.tweet_id = tweet.tweet_id
where home_timeline_tweet_ids.user_id = 123456 and
tweet.text ~ 'テスト'
group by to_char(tweet.created_at, 'yyyy/mm/dd')
order by to_char(tweet.created_at, 'yyyy/mm/dd') desc limit 100
こうやって10回測定 from linux
export Q="select ほにゃらら"
i=1; while [ $i -le 10 ]; do psql -d twitter -h xxxxx.ap-northeast-1.rds.amazonaws.com -U twitter -c "EXPLAIN ANALYZE ${Q}" | grep "Execution time" ; i=$(expr $i + 1);done
測定時間の結果をchromeのdevtoolのコンソールで計算
a=`[user@fushihara.io ~]$i=1; while [ $i -le 10 ]; do psql -d twitter -h twitter-r3-2xlarge.czpa3unkkrto.ap-northeast-1.rds.amazonaws.com -U twitter -c "EXPLAIN ANALYZE ${Q}" | grep "Execution time" ; i=$(expr $i + 1);done
Execution time: 4312.636 ms
Execution time: 4314.856 ms
Execution time: 4339.720 ms
Execution time: 4319.702 ms
Execution time: 4301.736 ms
Execution time: 4306.114 ms
Execution time: 4312.707 ms
Execution time: 4317.436 ms
Execution time: 4306.224 ms
Execution time: 4307.491 ms`;//例
b = a.split("\n").
map((i)=>i.trim()).
map(i=>i.match(/time: ([0-9.]+) ms/)[1]-0).
sort().slice(1, 9);
console.log(`MAX=${b[0]} MIN=${b[7]} AVG=${b.reduce((a,b)=>{return a+b})/8}`);
さくらvpsの使ってるプランはこちら。メモリ8GB、HDD 800GBモデル。この1台をDB専用にしてる訳ではなく、他にもnginxとか入ってるけど参考までに。
VPSが月8000円とすると、t2.midium相当。それでも自鯖だと6100ポイントが4000ポイント。自鯖がDB専用じゃない事を考えると、AWSにはもっと頑張って欲しい所だけどどうしよう…