LoginSignup
5
5

More than 5 years have passed since last update.

ActiveAndroidで複合インデックスを構築させる方法

Posted at

通常のindexは以下の通りindex = trueって書いておけば簡単に作れる。

宣言(カラム部分のみ)
@Column(name = "score", index = true)
public int score;
生成されるクエリ(一部)
CREATE INDEX index_XXX_score on XXX(score);

これを複合indexにする場合、組み合わせたいカラム同士で同じ名前をindexGroupsに追加することで複合indexが構築される。

宣言(カラム部分のみ)
@Column(name = "category_id", indexGroups = {"category_score"})
public int categoryId;
@Column(name = "score", indexGroups = {"category_score"})
public int score;
生成されるクエリ(一部)
CREATE INDEX index_XXX_category_score on XXX(category_id, score);

Groups、という名前からお察しの通り、あるカラムを複数のindexに絡めることも可能。例えば、A-BとB-Cの二つの複合indexを使うとか。

例えばこうやってcategoryとscoreで複合indexを設定しておけば、きっと「category = 1をscoreで昇順」みたいなリクエストが爆速になってくれるはず。id取得ばかりがクエリじゃない。ORMはSQLを理解してこそ真の力を発揮できるはず。

5
5
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
5
5