【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);
}