[Solved] MySQL index not used

EverSQL Database Performance Knowledge Base

MySQL index not used

Database type:

have some table with index for two columns (user_id,date) and SQL query

select  user_id, stat.in, stat.out, stat.time, date
from stat
where user_id in (select id from users force index (street_id) where street_id=30);

or

select  user_id, stat.in, stat.out, stat.time, date 
from stat where user_id in (select id from users force index (street_id) where street_id=30)
and date between STR_TO_DATE('2010-01-01 00:00:00', '%Y-%m-%d %H:%i:%s') and TR_TO_DATE('2014-05-22 23:59:59', '%Y-%m-%d %H:%i:%s')

In two case index must work, but I sink problem in in statement. If it's possible, how make it work? Explain:

+----+--------------------+-------+------+---------------+-----------+---------+-------+----------+--------------------------+
| id | select_type        | table | type | possible_keys | key       | key_len | ref   | rows     | Extra                    |
+----+--------------------+-------+------+---------------+-----------+---------+-------+----------+--------------------------+
|  1 | PRIMARY            | stat  | ALL  | NULL          | NULL      | NULL    | NULL  | 32028701 | Using where              |
|  2 | DEPENDENT SUBQUERY | users | ref  | street_id     | street_id | 8       | const |      650 | Using where; Using index |
+----+--------------------+-------+------+---------------+-----------+---------+-------+----------+--------------------------+

if search with one user_id index work

explain select  user_id, stat.in, stat.out, stat.time, date
from stat
where user_id=3991;

Explain:

+----+-------------+-------+------+---------------+-----------+---------+-------+------+-------+
| id | select_type | table | type | possible_keys | key       | key_len | ref   | rows | Extra |
+----+-------------+-------+------+---------------+-----------+---------+-------+------+-------+
|  1 | SIMPLE      | stat  | ref  | user_id_2     | user_id_2 | 8       | const | 2973 |       |
+----+-------------+-------+------+---------------+-----------+---------+-------+------+-------+

How to optimize this SQL query?

The following recommendations will help you in your SQL tuning process.
You'll find 3 sections below:

  1. Description of the steps you can take to speed up the query.
  2. The optimal indexes for this query, which you can copy and create in your database.
  3. An automatically re-written query you can copy and execute in your database.
The optimization process and recommendations:
  1. Avoid Optimizer Hints (modified query below): Using optimizer hints such as FORCE INDEX can be valuable in the short term. When important aspects such as the amount of data or the data distribution change, these hints can do more harm than good.
  2. Create Optimal Indexes (modified query below): The recommended indexes are an integral part of this optimization effort and should be created before testing the execution duration of the optimized query.
  3. Replace In Subquery With Correlated Exists (modified query below): In many cases, an EXISTS subquery with a correlated condition will perform better than a non correlated IN subquery.
Optimal indexes for this query:
ALTER TABLE `users` ADD INDEX `users_idx_street_id_id` (`street_id`,`id`);
The optimized query:
SELECT
        stat.user_id,
        stat.in,
        stat.out,
        stat.time,
        stat.date 
    FROM
        stat 
    WHERE
        EXISTS (
            SELECT
                1 
            FROM
                users 
            WHERE
                (
                    users.street_id = 30
                ) 
                AND (
                    stat.user_id = users.id
                )
        )

Related Articles



* original question posted on StackOverflow here.