I have a query that JOINs 2 tables; lineitem and part,
select
sum(l_extendedprice* (1 - l_discount)) as revenue
from
lineitem force index for join (l_pk),
part
where
(
p_partkey = l_partkey
and p_brand = 'Brand#12'
and p_container in ('SM CASE', 'SM BOX', 'SM PACK', 'SM PKG')
and l_quantity >= 1 and l_quantity <= 1 + 10
and p_size between 1 and 5
and l_shipmode in ('AIR', 'AIR REG')
and l_shipinstruct = 'DELIVER IN PERSON'
)
or
(
p_partkey = l_partkey
and p_brand = 'Brand#23'
and p_container in ('MED BAG', 'MED BOX', 'MED PKG', 'MED PACK')
and l_quantity >= 10 and l_quantity <= 10 + 10
and p_size between 1 and 10
and l_shipmode in ('AIR', 'AIR REG')
and l_shipinstruct = 'DELIVER IN PERSON'
)
or
(
p_partkey = l_partkey
and p_brand = 'Brand#34'
and p_container in ('LG CASE', 'LG BOX', 'LG PACK', 'LG PKG')
and l_quantity >= 20 and l_quantity <= 20 + 10
and p_size between 1 and 15
and l_shipmode in ('AIR', 'AIR REG')
and l_shipinstruct = 'DELIVER IN PERSON'
);
I have an index on lineitem and want the query to use this index for the join p_partkey = l_partkey
create index l_pk on tpch.lineitem(l_partkey);
MySQL explain shows:
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+----------+--------+---------------+---------+---------+-------------------------+---------+-------------+
| 1 | SIMPLE | lineitem | ALL | l_pk | NULL | NULL | NULL | 5982534 | Using where |
| 1 | SIMPLE | part | eq_ref | PRIMARY | PRIMARY | 4 | tpch.lineitem.L_PARTKEY | 1 | Using where |
+----+-------------+----------+--------+---------------+---------+---------+-------------------------+---------+-------------+
Why is the index l_pk not used?
for jointo see if it works. "To affect only the indexes used when MySQL decides how to find rows in the table and how to process joins, useFOR JOIN", yet tthere is no explicitjoinon yourselectso yourforce index for joinmay not work in this case. – Pacerier Feb 03 '15 at 03:00