| --- |
| title: Using Indexes with Equi-Join Queries |
| --- |
| |
| <!-- |
| Licensed to the Apache Software Foundation (ASF) under one or more |
| contributor license agreements. See the NOTICE file distributed with |
| this work for additional information regarding copyright ownership. |
| The ASF licenses this file to You under the Apache License, Version 2.0 |
| (the "License"); you may not use this file except in compliance with |
| the License. You may obtain a copy of the License at |
| |
| http://www.apache.org/licenses/LICENSE-2.0 |
| |
| Unless required by applicable law or agreed to in writing, software |
| distributed under the License is distributed on an "AS IS" BASIS, |
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| See the License for the specific language governing permissions and |
| limitations under the License. |
| --> |
| |
| Equi-join queries are queries in which two regions are joined through an equality condition in the WHERE clause. |
| |
| <a id="concept_A90C5FD84FCB45B2B28D6CE78DE1D117__section_47CFF4EF4D964FCFBB772B0347C02214"></a> |
| To use an index with an equi-join query: |
| |
| 1. Create an index for each side of the equi-join condition. The query engine can quickly evaluate the query's equi-join condition by iterating over the keys of the left-side and right-side indexes for an equality match. |
| |
| **Note:** |
| Equi-join queries require regular indexes. Key indexes are not applied to equi-join queries. |
| |
| For this query: |
| |
| ``` pre |
| SELECT DISTINCT inv.name, ord.orderID, ord.status |
| FROM /investors inv, /orders ord |
| WHERE inv.investorID = ord.investorID |
| ``` |
| |
| Create two indexes: |
| |
| | FROM clause | Indexed expression | |
| |----------------|--------------------| |
| | /investors inv | inv.investorID | |
| | /orders ord | ord.investorID | |
| |
| 2. If there are additional, single-region queries in a query with an equi-join condition, create additional indexes for the single-region conditions only if you are able to create at least one such index for each region in the query. Any indexing on a subset of the regions in the query will degrade performance. |
| |
| For this example query: |
| |
| ``` pre |
| SELECT DISTINCT * |
| FROM /investors inv, /securities sc, inv.heldSecurities inv_hs |
| WHERE sc.status = "active" |
| AND inv.name = "xyz" |
| AND inv.age > 75 |
| AND inv_hs.secName = sc.secName |
| ``` |
| |
| Create the indexes for the equi-join condition: |
| |
| | FROM clause | Indexed expression | |
| |--------------------------------------------|--------------------| |
| | /investors inv, inv.heldSecurities inv\_hs | inv\_hs.secName | |
| | /securities sc | sc.secName | |
| |
| Then, if you create any more indexes, create one on `sc.status` and one on `inv.age` or `inv.name` or both. |
| |
| |