继续测试博客
2017-07-10 13:28
246 查看
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表中有相同字段 tommysql> 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)
lisea 2017/7/4 星期二 上午 10:45:53
* 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. 总结以需求驱动技术,技术本身没有优略之分,只有业务之分。
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表中有相同字段 tommysql> 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)
lisea 2017/7/4 星期二 上午 10:45:53
* 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. 总结以需求驱动技术,技术本身没有优略之分,只有业务之分。
相关文章推荐
- 继续测试博客
- 继续测试博客
- 3 继续测试一下 esp_tunnel 用 racoon协商 用setkey设置 spd
- 首先测试一下使用live writer录入博客
- 博客目前还有没有的优势可言,怎样才能继续下去呢
- HTML本地测试成功后上传博客注意事项
- 好久没写博客了,今天没事测试一下浏览器for循环顺序不同及定义变量位置不同的效率
- 10款和博客有关的在线测试工具
- 用Windows Live Writer 写的第一个博客文章,测试
- 测试用ZOUNDRY发博客
- 我的测试博客
- 测试金山WPS博客发布器插件!!
- 在51testing的博客上新开了个栏目 - 界面测试
- 使用Office-Word的博客发布功能(测试博文)
- 也谈测试数据 [ 光影人像 东海陈光剑 的博客 ]
- 接口测试在淘宝的应用 [ 光影人像 东海陈光剑 的博客 ]
- 测试人员与开发经验 [ 光影人像 东海陈光剑 的博客 ]
- 博客域名不能改了...测试一下markdown的 table
- 用户体验与测试 [ 光影人像 东海陈光剑 的博客 ]