Using Variables

set @a:='1000-01-01 00:00:00';  
set @b:=' ';  
set @f:=0;  
truncate t_target;  
insert into t_target  
select item_id,created_time,modified_time,item_name,other  
  from   
(select t0.*,if(@a=created_time and @b=item_name,@f:=0,@f:=1) f, @a:=created_time,@b:=item_name  
  from   
(select * from t_source order by created_time,item_name) t0) t1 where f=1;
        这种方法用时13秒,查询计划如下:

mysql> explain select item_id,created_time,modified_time,item_name,other  
    ->   from   
    -> (select t0.*,if(@a=created_time and @b=item_name,@f:=0,@f:=1) f, @a:=created_time,@b:=item_name  
    ->   from   
    -> (select * from t_source order by created_time,item_name) t0) t1 where f=1; 
+----+-------------+------------+------------+------+---------------+-------------+---------+-------+--------+----------+----------------+
| id | select_type | table      | partitions | type | possible_keys | key         | key_len | ref   | rows   | filtered | Extra          |
+----+-------------+------------+------------+------+---------------+-------------+---------+-------+--------+----------+----------------+
|. 1 | a PRIMARY | <Derived2> | NULL | REF | <auto_key0> | <auto_key0> |. 4 | const | 10 | the 100.00 | NULL |
| 2 | DERIVED | <derived3> | NULL | ALL | NULL | NULL | NULL | NULL | 997 282 | 100.00 | NULL |
| 3 | DERIVED | t_source | NULL | ALL | NULL | NULL | NULL | NULL | 997 282 | 100.00 | the Using filesort |
+ ---- + ---------- --- + ------ + ------------ + ------------ + ------------- - + ------------- + --------- + -------- + ------- + ------ + ---------------- + ----
3 rows in the SET, 5 Represents warnings (0.00 sec)
100 million lines of inquiry scan t_source innermost table, and use the file sorting generate export table derived3.
The second layer inquiry to scan 1 million lines derived3 generate export table derived2, completes the comparison and assignment variables, and automatically create an export index auto_key0 on the column f.
Results outermost layer auto_key0 index scan line to obtain derived2 weight.
        Compared with the above method, the total number of scanning lines unchanged, are two million lines. There is only a couple of minor differences, the index is automatically generated on a constant column f, and the associated table indexes are automatically generated on item_id column, so almost the same query time.

        So far, we have not created any index on the source table. Regardless of the wording used to check the weight and need for created_time item_name sort fields, so it is natural to think, if the establishment of a joint index on both fields, use the index itself ordered the elimination of additional sort properties, thereby improving query performance.
---------------------

Guess you like

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