1. 程式人生 > >MySQL DML操作--------合並查詢結果實戰

MySQL DML操作--------合並查詢結果實戰

總結 extra blank 分析 from idt table ref ima

1. 背景

* 全並查詢結果是將多個 select 語句的查詢結果合並到一起。

* 參與合並的結果集需要字段統一。

* 字段可以用空字符串‘‘代替。

2. 合並查詢結果實戰 [ users1 and users2 ]

* 查看 users1 表和 users2 表結構

mysql> desc users1;
+-------+---------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------------+------+-----+---------+----------------+
| id | bigint(20) | NO | PRI | NULL | auto_increment |
| name | varchar(64) | NO | | NULL | |
| sex | enum(‘M‘,‘F‘) | NO | | NULL | |
| age | int(11) | NO | | NULL | |
+-------+---------------+------+-----+---------+----------------+
4 rows in set (0.00 sec)

mysql> desc users2;
+-------+---------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------------+------+-----+---------+----------------+
| id | bigint(20) | NO | PRI | NULL | auto_increment |
| name | varchar(64) | NO | | NULL | |
| sex | enum(‘M‘,‘F‘) | NO | | NULL | |
| age | int(11) | NO | | NULL | |
+-------+---------------+------+-----+---------+----------------+
4 rows in set (0.01 sec)

* 查看 users1 表和 users2 表數據

users1和users2表中有相同字段 tom

mysql> select * from users1;
+----+------+-----+-----+
| id | name | sex | age |
+----+------+-----+-----+
| 1 | tom | M | 25 |
| 2 | jak | F | 42 |
+----+------+-----+-----+
2 rows in set (0.00 sec)

mysql> select * from users2;
+----+-------+-----+-----+
| id | name | sex | age |
+----+-------+-----+-----+
| 1 | tom | M | 25 |
| 2 | lisea | M | 42 |
+----+-------+-----+-----+
2 rows in set (0.00 sec)

* union 合並並去重

技術分享

mysql> (select * from users1) union (select * from users2);
+----+-------+-----+-----+
| id | name | sex | age |
+----+-------+-----+-----+
| 1 | tom | M | 25 |
| 2 | jak | F | 42 |
| 2 | lisea | M | 42 |
+----+-------+-----+-----+
3 rows in set (0.00 sec)

* union all 只全並不去重

技術分享

mysql> (select * from users1) union all (select * from users2);
+----+-------+-----+-----+
| id | name | sex | age |
+----+-------+-----+-----+
| 1 | tom | M | 25 |
| 2 | jak | F | 42 |
| 1 | tom | M | 25 |
| 2 | lisea | M | 42 |
+----+-------+-----+-----+
4 rows in set (0.01 sec)

* 查看union 性能分析

[ 使用了臨時表 ]

mysql> explain (select * from users1) union (select * from users2);
+----+--------------+------------+------------+------+---------------+------+---------+------+------+----------+-----------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+--------------+------------+------------+------+---------------+------+---------+------+------+----------+-----------------+
| 1 | PRIMARY | users1 | NULL | ALL | NULL | NULL | NULL | NULL | 2 | 100.00 | NULL |
| 2 | UNION | users2 | NULL | ALL | NULL | NULL | NULL | NULL | 2 | 100.00 | NULL |
| NULL | UNION RESULT | <union1,2> | NULL | ALL | NULL | NULL | NULL | NULL | NULL | NULL | Using temporary |
+----+--------------+------------+------------+------+---------------+------+---------+------+------+----------+-----------------+
3 rows in set, 1 warning (0.01 sec)

* 查看union all 性能分析

[ 未使用臨時表 ]

mysql> explain (select * from users1) union all (select * from users2);
+----+-------------+--------+------------+------+---------------+------+---------+------+------+----------+-------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+--------+------------+------+---------------+------+---------+------+------+----------+-------+
| 1 | PRIMARY | users1 | NULL | ALL | NULL | NULL | NULL | NULL | 2 | 100.00 | NULL |
| 2 | UNION | users2 | NULL | ALL | NULL | NULL | NULL | NULL | 2 | 100.00 | NULL |
+----+-------------+--------+------------+------+---------------+------+---------+------+------+----------+-------+
2 rows in set, 1 warning (0.01 sec)

3. union 與 union all 總結

* union 相對於 union all多了一步去重操作,此操作會創建臨時表,降低性能。

* 當兩邊結果集數據相對都確定了唯一性,推薦使用union all。

4. 總結

以需求驅動技術,技術本身沒有優略之分,只有業務之分。

?

MySQL DML操作--------合並查詢結果實戰