複合索引

次の例は、複数の表フィールドの索引付け、ネストされたフィールドの索引付け、および索引スキャン中のフィルタリング述語の使用を示しています。

sql-> create index idx_state_city_income on
Persons (address.state, address.city, income);
Statement completed successfully
sql-> SELECT * from Persons p WHERE p.address.state = "MA"
and income > 79000;

> Row 0                                                  
 +-------------+------------------------------------------------+
 | id          | 4                                              |
 +-------------+------------------------------------------------+
 | firstname   | Peter                                          |
 +-------------+------------------------------------------------+
 | lastname    | Smith                                          |
 +-------------+------------------------------------------------+
 | age         | 38                                             |
 +-------------+------------------------------------------------+
 | income      | 80000                                          |
 +-------------+------------------------------------------------+
 | lastLogin   | 2016-10-19T09:18:05.5555                       |
 +-------------+------------------------------------------------+
 | address     | street                   | 364 Mulberry Street |
 |             | city                     | Leominster          |
 |             | state                    | MA                  |
 |             | zipcode                  | NULL                |
 |             | phones                                         |
 |             |     type                 | work                |
 |             |     areacode             | 339                 |
 |             |     number               | 4120211             |
 |             |                                                |
 |             |     type                 | work                |
 |             |     areacode             | 339                 |
 |             |     number               | 8694021             |
 |             |                                                |
 |             |     type                 | home                |
 |             |     areacode             | 339                 |
 |             |     number               | 1205678             |
 |             |                                                |
 |             |     type                 | home                |
 |             |     areacode             | 305                 |
 |             |     number               | 8064321             |
 +-------------+------------------------------------------------+
 | connections | 3                                              |
 |             | 5                                              |
 |             | 1                                              |
 |             | 2                                              |
 +-------------+------------------------------------------------+
 | expenses    | books                    | 240                 |
 |             | clothes                  | 2000                |
 |             | food                     | 6000                |
 |             | shoes                    | 1200                |
 +-------------+------------------------------------------------+

1 row returned 

前述の問合せに索引idx_state_city_incomeを適用できます。具体的には、state = "MA"条件を使用して、索引スキャンの範囲を確立できます(最初のフィールドがMAである索引エントリのみがスキャンされます)。さらに、索引スキャン中に、収入条件をフィルタリング条件として使用して、3番目のフィールドが79000以下である索引エントリをスキップできます。その結果、両方の条件を満たす行のみが表から取得されます。