LoginSignup
0
0

【MyBatis Generator】RowBoundsPluginは使うべきではない

Posted at

【MyBatis Generator】RowBoundsPluginは性能的に問題があるため使うべきではないという話。

generatorConfig.xmlに

<plugin type="org.mybatis.generator.plugins.RowBoundsPlugin" />

を定義すると、マッパークラスにselectByExampleWithRowbounds()メソッドが生成される。
オフセットとリミット指定が可能になるものである。

以下例:

var commentExample = new CommentsExample(){{
    setOrderByClause("comment_id desc");
}};
var rowBounds = new RowBounds(0, 100);
try{
    var latestCommentInfo = commentsMapper.selectByExampleWithRowbounds(commentExample, rowBounds);
}

が、、、クエリログを確認すると、発行されるクエリ自体にはオフセット、リミット指定がされていない。
つまり、テーブルから全レコード取得した上で、Java側で絞込みを行っている。
そのため基本的にレコード数が多いほど性能面で著しい支障が発生してくる。

Rowboundsの使用は避け、setOrderByClause()にオフセット、リミット指定も混ぜ込むべきである。

以下例:

var commentExample = new CommentsExample(){{
    setOrderByClause("comment_id desc limit 100");
}};
try{
    var latestCommentInfo = commentsMapper.selectByExample(commentExample);
}
0
0
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
0