mysql 除索引复制数据_Mysql: 利用强制索引去掉重数据

数据库版本:

[root@mysqltest ~]# mysql -u root -p123456

Welcome to the MySQL monitor.  Commands end with ; or \g.

Your MySQL connection id is 389805

Server version: 5.1.73-community MySQL Community Server (GPL)

Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its

affiliates. Other names may be trademarks of their respective

owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

mysql> show databases;

+--------------------+

| Database           |

+--------------------+

| information_schema |

| cacti              |

| centreon           |

| centreon_status    |

| centreon_storage   |

| mysql              |

| syslog             |

| test               |

+--------------------+

8 rows in set (0.01 sec)

1 .创建实验使用的a b表

mysql> use test;

Database changed

mysql> show tables;

Empty set (0.00 sec)

mysql> create table a (a1 char(10), a2 char(10), a3 char(10));

Query OK, 0 rows affected (0.08 sec)

mysql> insert into a values ('1', '2', '3');

Query OK, 1 row affected (0.00 sec)

mysql> insert into a select * from a;

Query OK, 1 row affected (0.00 sec)

Records: 1  Duplicates: 0  Warnings: 0

mysql> insert into a select * from a;

Query OK, 4194304 rows affected (7.85 sec)

Records: 4194304  Duplicates: 0  Warnings: 0

mysql> insert into a select * from a;

Query OK, 8388608 rows affected (27.81 sec)

Records: 8388608  Duplicates: 0  Warnings: 0

2  创建b表:

mysql> create table b (b1 char (10), b2 char(10), b3 char(10));

Query OK, 0 rows affected (0.06 sec)

mysql> insert into b select * from a;

Query OK, 16777216 rows affected (1 min 6.18 sec)

Records: 16777216  Duplicates: 0  Warnings: 0

mysql>

mysql>

mysql> commit;

Query OK, 0 rows affected (0.00 sec)

mysql>

mysql>

mysql> select * from b limit 10;

+------+------+------+

| b1   | b2   | b3   |

+------+------+------+

| 1    | 2    | 3    |

| 1    | 2    | 3    |

| 1    | 2    | 3    |

| 1    | 2    | 3    |

| 1    | 2    | 3    |

| 1    | 2    | 3    |

| 1    | 2    | 3    |

| 1    | 2    | 3    |

| 1    | 2    | 3    |

| 1    | 2    | 3    |

+------+------+------+

10 rows in set (0.04 sec)

3  加入b表不同样的数据

mysql> insert into b values ('4','5','6');

Query OK, 1 row affected (0.01 sec)

mysql> insert into b values ('4','5','6');

Query OK, 1 row affected (0.00 sec)

mysql> insert into b values ('4','5','6');

Query OK, 1 row affected (0.00 sec)

mysql> insert into b values ('4','5','6');

Query OK, 1 row affected (0.00 sec)

mysql>

mysql> insert into b values ('4','5','6');

Query OK, 1 row affected (0.00 sec)

mysql> commit

-> ;

Query OK, 0 rows affected (0.00 sec)

mysql>

mysql>

mysql>

4 查看a b表数据行数

mysql> select count(1) from b;

+----------+

| count(1) |

+----------+

| 16777224 |

+----------+

1 row in set (0.00 sec)

mysql> select count(1) from a;

+----------+

| count(1) |

+----------+

| 16777216 |

+----------+

1 row in set (0.00 sec)

5 创建c表

mysql> create table c (c1 char (10), c2 char(10), c3 char(10));

Query OK, 0 rows affected (0.31 sec)

6 创建临时temp表

mysql> create table temp select * from c where 1=2;

Query OK, 0 rows affected (0.06 sec)

Records: 0  Duplicates: 0  Warnings: 0

mysql> select * from temp;

Empty set (0.00 sec)

mysql> desc temp;

+-------+----------+------+-----+---------+-------+

| Field | Type     | Null | Key | Default | Extra |

+-------+----------+------+-----+---------+-------+

| c1    | char(10) | YES  |     | NULL    |       |

| c2    | char(10) | YES  |     | NULL    |       |

| c3    | char(10) | YES  |     | NULL    |       |

+-------+----------+------+-----+---------+-------+

3 rows in set (0.02 sec)

7 为b表创建索引

mysql> create index ind_b_b1 on b(b1);

Query OK, 16777224 rows affected (2 min 9.14 sec)

Records: 16777224  Duplicates: 0  Warnings: 0

mysql> desc b;

+-------+----------+------+-----+---------+-------+

| Field | Type     | Null | Key | Default | Extra |

+-------+----------+------+-----+---------+-------+

| b1    | char(10) | YES  | MUL | NULL    |       |

| b2    | char(10) | YES  |     | NULL    |       |

| b3    | char(10) | YES  |     | NULL    |       |

+-------+----------+------+-----+---------+-------+

3 rows in set (0.02 sec)

8 把a b表数据插入temp表中

mysql> insert into temp select * from a;

Query OK, 16777216 rows affected (29.84 sec)

Records: 16777216  Duplicates: 0  Warnings: 0

mysql> insert into temp select * from b;

Query OK, 16777224 rows affected (59.79 sec)

Records: 16777224  Duplicates: 0  Warnings: 0

mysql>

mysql>

mysql> commit;

Query OK, 0 rows affected (0.00 sec)

mysql> select count(1) from temp;

+----------+

| count(1) |

+----------+

| 33554440 |

+----------+

1 row in set (0.00 sec)

9 创建联合索引      强制索引去掉重复数据

mysql> create index ind_temp_c123 on temp(c1, c2, c3);

Query OK, 33554440 rows affected (6 min 57.80 sec)

Records: 33554440  Duplicates: 0  Warnings: 0

mysql>

mysql>

mysql> explain select c1, c2, max(c3) from temp force index (ind_temp_c123) group by c1, c2;

+----+-------------+-------+-------+---------------+---------------+---------+------+------+--------------------------+

| id | select_type | table | type  | possible_keys | key           | key_len | ref  | rows | Extra                    |

+----+-------------+-------+-------+---------------+---------------+---------+------+------+--------------------------+

|  1 | SIMPLE      | temp  | range | NULL          | ind_temp_c123 | 22      | NULL |    3 | Using index for group-by |

+----+-------------+-------+-------+---------------+---------------+---------+------+------+--------------------------+

1 row in set (0.00 sec)

mysql> select count(*) from temp;

+----------+

| count(*) |

+----------+

| 33554440 |

+----------+

1 row in set (0.00 sec)

mysql> explain select c1, c2, c3 from temp force index (ind_temp_c123) group by c1, c2;

+----+-------------+-------+-------+---------------+---------------+---------+------+------+--------------------------+

| id | select_type | table | type  | possible_keys | key           | key_len | ref  | rows | Extra                    |

+----+-------------+-------+-------+---------------+---------------+---------+------+------+--------------------------+

|  1 | SIMPLE      | temp  | range | NULL          | ind_temp_c123 | 22      | NULL |    3 | Using index for group-by |

+----+-------------+-------+-------+---------------+---------------+---------+------+------+--------------------------+

1 row in set (0.00 sec)

mysql> insert into c select c1, c2, max(c3) from temp force index (ind_temp_c123) group by c1, c2;

Query OK, 2 rows affected (0.03 sec)

Records: 2  Duplicates: 0  Warnings: 0

10 去重复后c表的数据

mysql> select * from c;

+------+------+------+

| c1   | c2   | c3   |

+------+------+------+

| 1    | 2    | 3    |

| 4    | 5    | 6    |

+------+------+------+

2 rows in set (0.00 sec)

mysql>

mysql>

mysql> select * from temp order by c1 desc limit 10;

+------+------+------+

| c1   | c2   | c3   |

+------+------+------+

| 4    | 5    | 6    |

| 4    | 5    | 6    |

| 4    | 5    | 6    |

| 4    | 5    | 6    |

| 4    | 5    | 6    |

| 4    | 5    | 6    |

| 4    | 5    | 6    |

| 4    | 5    | 6    |

| 1    | 2    | 3    |

| 1    | 2    | 3    |

+------+------+------+

10 rows in set (0.00 sec)

mysql> Ctrl-C -- exit!

Aborted

11 删除表 temp

mysql> drop table temp;

Query OK, 0 rows affected (1.59 sec)

mysql> drop table a;

Query OK, 0 rows affected (0.55 sec)

mysql> drop table b;

Query OK, 0 rows affected (0.73 sec)

mysql> show tables;

+----------------+

| Tables_in_test |

+----------------+

| c              |

+----------------+

1 row in set (0.00 sec)

mysql>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值