1. 程式人生 > >利用臨時表優化SQL

利用臨時表優化SQL

開發說他們有個SQL特別慢,讓我幫忙看看

select id_ from t_channel where id_  not in (select distinct cdbh from sjkk_gcjl where jgsj>'2015-01-02 09:00:00' and jgsj<'2015-01-02 10:00:00' );
......
30分鐘以上
+----+--------------------+-----------+-------+---------------+----------+---------+------+--------+------------------------------+
| id | select_type        | table     | type  | possible_keys | key      | key_len | ref  | rows   | Extra                        |
+----+--------------------+-----------+-------+---------------+----------+---------+------+--------+------------------------------+
|  1 | PRIMARY            | t_channel | index | NULL          | ID_      | 74      | NULL |  10476 | Using where; Using index     |
|  2 | DEPENDENT SUBQUERY | sjkk_gcjl | range | idx_jgsj      | idx_jgsj | 8       | NULL | 201263 | Using where; Using temporary |
+----+--------------------+-----------+-------+---------------+----------+---------+------+--------+------------------------------+

我看了下這個SQL,感覺如果查詢In而不是not in,並改寫下sql應該會快。和開發交流了下,可以換成in,他們處理邏輯上可以改下。
select id_ from t_channel where id_  in (select distinct cdbh from sjkk_gcjl where jgsj>'2015-01-02 09:00:00' and jgsj<'2015-01-02 10:00:00' );
......
1223 rows in set (29 min 29.29 sec)
+----+--------------------+-----------+-------+---------------+----------+---------+------+--------+------------------------------+
| id | select_type        | table     | type  | possible_keys | key      | key_len | ref  | rows   | Extra                        |
+----+--------------------+-----------+-------+---------------+----------+---------+------+--------+------------------------------+
|  1 | PRIMARY            | t_channel | index | NULL          | ID_      | 74      | NULL |  10476 | Using where; Using index     |
|  2 | DEPENDENT SUBQUERY | sjkk_gcjl | range | idx_jgsj      | idx_jgsj | 8       | NULL | 201263 | Using where; Using temporary |
+----+--------------------+-----------+-------+---------------+----------+---------+------+--------+------------------------------+

 改寫上面SQL,速度快了很多。
select id_ from t_channel t1,(select distinct cdbh from sjkk_gcjl where jgsj>'2015-01-02 09:00:00' and jgsj<'2015-01-02 10:00:00' ) t2 where t1.id_= t2.cdbh;
.....
1223 rows in set (0.82 sec)
+----+-------------+------------+--------+---------------+----------+---------+---------+--------+------------------------------+
| id | select_type | table      | type   | possible_keys | key      | key_len | ref     | rows   | Extra                        |
+----+-------------+------------+--------+---------------+----------+---------+---------+--------+------------------------------+
|  1 | PRIMARY     | <derived2> | ALL    | NULL          | NULL     | NULL    | NULL    |   1700 |                              |
|  1 | PRIMARY     | t1         | eq_ref | PRIMARY,ID_   | PRIMARY  | 74      | t2.cdbh |      1 | Using where; Using index     |
|  2 | DERIVED     | sjkk_gcjl  | range  | idx_jgsj      | idx_jgsj | 8       | NULL    | 201263 | Using where; Using temporary |
+----+-------------+------------+--------+---------------+----------+---------+---------+--------+------------------------------+

後來他們說他們其實想更具查詢出來的結果去重新整理另一個表資料狀態。然後更具他們的目的用臨時表實現。
mysql> drop table if exists tmp_channel;
Query OK, 0 rows affected (0.00 sec)

mysql> create temporary table tmp_channel  engine=memory (select distinct cdbh from sjkk_gcjl where jgsj>'2015-01-02 09:00:00' and jgsj<'2015-01-02 10:00:00' );
Query OK, 1700 rows affected (2.22 sec)
Records: 1700  Duplicates: 0  Warnings: 0

mysql> update t_channel set channelStatus = 1;
Query OK, 9075 rows affected (0.09 sec)
Rows matched: 10298  Changed: 9075  Warnings: 0

mysql> update t_channel set channelStatus = 0 where  id_   not in(select cdbh from t);
Query OK, 9075 rows affected (1.41 sec)
Rows matched: 9075  Changed: 9075  Warnings: 0

mysql> drop table if exists tmp_channel;
Query OK, 0 rows affected (0.00 sec)

這樣整個過程也就3s+