0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

テーブルのデータを分割して全件取得する パフォーマンス

Posted at

パフォーマンスについて
例えば、データ数が多いため分割取得するためのselect文はどのようにすればよいか。

あまり、詳しくはないのですが最近調べたことをメモします。

分割取得でたまに見かけるのが、limit offsetです。
ただ、並び順を担保するために、order byが必要となります。(DBによって違う??)

こういう風にソートをしてしまうと、テーブルのデータをいったんメモリに展開して並び替えを行うため、テーブルのデータサイズに比例して使用メモリが増大する。
また、CPUの負荷も高い。

ではどうすればいいか。
分割をwhere条件でいい感じに分けた方がパフォーマンスが良い。

例えば、更新日を持っているのであれば、全件のカウントと、minとmaxの更新日をとっていい感じに等分して、結果limit offsetのように分割できるようにすればいい。

そうすると、order byが不要。ということは、メモリ使用量がへり、CPU負荷も低く抑えられる。

と思っています。あっていますかね~(=_=)

こちら、参考にさせてもらいました。

なぜ、SQLは重たくなるのか?
https://employment.en-japan.com/engineerhub/entry/2017/06/26/110000

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?