Establish joint index comparison test on created_time and item_name

- establish created_time item_name field of joint index and
the Create index idx_sort ON t_source (created_time, item_name, item_id);  
the Analyze the Table t_source;
(1) the use of the relevant sub-queries
truncate t_target; (http://www.amjmh.com/v/BIBRGZ_558768 /)
INSERT INTO T_target  
SELECT DISTINCT from t_source T1 * T1 WHERE item_id in.   
(SELECT min (item_id) from t_source T2 WHERE t1.created_time = t2.created_time and t1.item_name = t2.item_name);
        the time of this for 19 seconds, the query plan is as follows:

mysql> explain select distinct t1.* from t_source t1 where item_id in   
    -> (select min(item_id) from t_source t2 where t1.created_time=t2.created_time and t1.item_name=t2.item_name);  
+----+--------------------+-------+------------+------+---------------+----------+---------+----------------------------------------+--------+----------+------------------------------+
| id | select_type        | table | partitions | type | possible_keys | key      | key_len | ref                                    | rows   | filtered | Extra                        |
+----+--------------------+-------+------------+------+---------------+----------+---------+----------------------------------------+--------+----------+------------------------------+
|. 1 | a PRIMARY | T1 | NULL | ALL | NULL | NULL | NULL | NULL | 997 281 | the 100.00 | the Using WHERE; the Using Temporary |
| 2 | DEPENDENT SUBQUERY | T2 | NULL | REF | idx_sort | idx_sort | 89 | test.t1 .created_time, test.t1.item_name | 2 | 100.00 | the Using index |
+ -------------------- + ---- + ------- + --------------- + ------ + ------------ + ---------- + - ------- + ---------------------------------------- + - ------- + ---------- + ------------------------------ +
in the SET rows 2, 3 Represents warnings (0.00 sec)
t_source the outer query table is the driving table, you need to scan 1 million lines.
For each row drive item_id table, the index query idx_sort by two rows of data.

Guess you like

Origin www.cnblogs.com/hyhy904/p/11311213.html