首页 > 数据库 >mysql 为分区表添加主键

mysql 为分区表添加主键

时间:2023-02-23 11:47:24浏览次数:46  
标签:10 PARTITION 分区表 VALUES mysql test NULL 主键

目录

适用范围

MYSQL 5.7 +

问题概述

客户生产环境有一个每天一个分区的大表,经常频繁update操作,而且没有主键,导致晚上跑批时,slave延迟很大,基本在10个小时左右。

问题原因

为了优化该表,我们给的建议是将历史数据分离出去,并通过创建主键索引提长效率,而客户表结构上并无合适的字段添加组合索引(分区表必须包含分区字段),所以我们建议通过添加自增主键+分区键来创建组合索引。

解决方案

测试步骤如下:

1.创建测试表

mysql> CREATE TABLE `test` ( 
             `name` VARCHAR(50), 
             `purchased` DATE
           ) ENGINE=InnoDB DEFAULT CHARSET=utf8
           PARTITION BY RANGE( YEAR(purchased) ) (
           PARTITION p0 VALUES LESS THAN (1990),
           PARTITION p1 VALUES LESS THAN (1995),
           PARTITION p2 VALUES LESS THAN (2000),
           PARTITION p3 VALUES LESS THAN (2005),
           PARTITION p4 VALUES LESS THAN (2010),
           PARTITION p5 VALUES LESS THAN (2015)
           );
Query OK, 0 rows affected, 1 warning (0.19 sec)

mysql> desc test ;
+-----------+-------------+------+-----+---------+-------+
| Field     | Type        | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+-------+
| name      | varchar(50) | YES  |     | NULL    |       |
| purchased | date        | YES  |     | NULL    |       |
+-----------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)

mysql> INSERT INTO `test` VALUES
            ('desk organiser', '2003-10-15'),
            ('alarm clock', '1997-11-05'),
            ('chair', '2009-03-10'),
            ('bookcase', '1989-01-10'),
            ('exercise bike', '2014-05-09'),
            ('sofa', '1987-06-05'),
            ('espresso maker', '2011-11-22'),
            ('aquarium', '1992-08-04'),
            ('study desk', '2006-09-16'),
            ( 'lava lamp', '1998-12-25');
Query OK, 10 rows affected (0.01 sec)
Records: 10  Duplicates: 0  Warnings: 0

mysql> select * from test ;
+----------------+------------+
| name           | purchased  |
+----------------+------------+
| bookcase       | 1989-01-10 |
| sofa           | 1987-06-05 |
| aquarium       | 1992-08-04 |
| alarm clock    | 1997-11-05 |
| lava lamp      | 1998-12-25 |
| desk organiser | 2003-10-15 |
| chair          | 2009-03-10 |
| study desk     | 2006-09-16 |
| exercise bike  | 2014-05-09 |
| espresso maker | 2011-11-22 |
+----------------+------------+
10 rows in set (0.00 sec)

2.添加字段

mysql> alter table test add id int;
Query OK, 0 rows affected (0.08 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql>  desc test ;
+-----------+-------------+------+-----+---------+-------+
| Field     | Type        | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+-------+
| name      | varchar(50) | YES  |     | NULL    |       |
| purchased | date        | YES  |     | NULL    |       |
| id        | int         | YES  |     | NULL    |       |
+-----------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)

mysql> select * from test ;
+----------------+------------+------+
| name           | purchased  | id   |
+----------------+------------+------+
| bookcase       | 1989-01-10 | NULL |
| sofa           | 1987-06-05 | NULL |
| aquarium       | 1992-08-04 | NULL |
| alarm clock    | 1997-11-05 | NULL |
| lava lamp      | 1998-12-25 | NULL |
| desk organiser | 2003-10-15 | NULL |
| chair          | 2009-03-10 | NULL |
| study desk     | 2006-09-16 | NULL |
| exercise bike  | 2014-05-09 | NULL |
| espresso maker | 2011-11-22 | NULL |
+----------------+------------+------+
10 rows in set (0.00 sec)

3.备份表

[root@s2ahuoracle02 run]#  mysqldump -uroot -proot   -P3333 --socket=/u01/mysql8e/data/run/mysql3333.sock    --set-gtid-purged=OFF  --no-create-info pardb  test  > backup3.sql

mysql> rename table test to test_bak;
Query OK, 0 rows affected (0.09 sec)

4.创建带主的表结构

mysql> CREATE TABLE `test` (
            `id` INT AUTO_INCREMENT, 
            `name` VARCHAR(50), 
            `purchased` DATE ,
        PRIMARY KEY(id, purchased) 
          ) ENGINE=InnoDB DEFAULT CHARSET=utf8
          PARTITION BY RANGE( YEAR(purchased) ) (
          PARTITION p0 VALUES LESS THAN (1990),
          PARTITION p1 VALUES LESS THAN (1995),
          PARTITION p2 VALUES LESS THAN (2000),
          PARTITION p3 VALUES LESS THAN (2005),
          PARTITION p4 VALUES LESS THAN (2010),
          PARTITION p5 VALUES LESS THAN (2015)
          );
Query OK, 0 rows affected, 1 warning (0.17 sec)

mysql> 
mysql> desc test;
+-----------+-------------+------+-----+---------+----------------+
| Field     | Type        | Null | Key | Default | Extra          |
+-----------+-------------+------+-----+---------+----------------+
| id        | int         | NO   | PRI | NULL    | auto_increment |
| name      | varchar(50) | YES  |     | NULL    |                |
| purchased | date        | NO   | PRI | NULL    |                |
+-----------+-------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)

mysql> select * from test ;
Empty set (0.00 sec)

5.修改备份脚本

由于备份原表的字段顺序,与新表的字段顺序不一致,此时我们需要调整备份脚本的字段顺便。添字段顺序 (name,purchased,id) 到备份的脚本中。vi backup3.sql
image.png

6.将数据导入到表中

mysql> source backup3.sql
mysql> select * from test ;
+----+----------------+------------+
| id | name           | purchased  |
+----+----------------+------------+
|  1 | bookcase       | 1989-01-10 |
|  2 | sofa           | 1987-06-05 |
|  3 | aquarium       | 1992-08-04 |
|  4 | alarm clock    | 1997-11-05 |
|  5 | lava lamp      | 1998-12-25 |
|  6 | desk organiser | 2003-10-15 |
|  7 | chair          | 2009-03-10 |
|  8 | study desk     | 2006-09-16 |
|  9 | exercise bike  | 2014-05-09 |
| 10 | espresso maker | 2011-11-22 |
+----+----------------+------------+
10 rows in set (0.01 sec)

 

参考文档

标签:10,PARTITION,分区表,VALUES,mysql,test,NULL,主键
From: https://www.cnblogs.com/cqdba/p/17147367.html

相关文章

  • 通过init-connect 实现MYSQL 普通用户登录审计
    目录适用范围问题概述解决方案创建审计日志表开启审计日志表创建普通用户并授审计日志表结果说明参考文档适用范围MYSQL8+问题概述数据库审计功能主要将用户对数据库......
  • mysql允许root用户在任何地方进行远程登录,并具有所有库任何操作权限
    在本机先使用root用户登录mysql:mysql-uroot-p"youpass"进行授权操作:mysql>GRANTALLPRIVILEGESON*.*TO'root'@'%'IDENTIFIEDBY'youpassword'WITHGRANTOPT......
  • php-mysql 函数练习和使用!
    1.回顾:上篇学习和使用了wamp+eclipse的环境搭建2.这篇将学习一个例子,使用mysql函数来学习3.基本操作  3.1设置编码header("Content-type:text/html;charset=utf-8......
  • mysql查询语句简单优化
    showprocesslist;killID;2. explain分析查询语句explainSELECT*FROMCASE_TESTPCPLEFTJOINSYS_USERSUONSU.USERID=PCP.UPDATE_PERSON......
  • 决战圣地玛丽乔亚Day18----关于mysql死锁+ mysql索引的数据结构相关
    Mysql为什么会产生死锁? 事务A、B分别拿到1,4的独占行锁,然后再去拿4,1的行锁,都会由于另一个事务不释放锁等待,造成了循环等待的局面。(1)同一时刻只能有一个事务持有这......
  • python 把mysql数据导入到execl中
    importpymysqlimportpandasaspddb=pymysql.connect(host='127.0.0.1',user='root',passwd='123456',port=3306,database='world',......
  • mysql备份
    mysql备份1.备份的分类完全备份增量备份差异备份2.备份的方式1,使用专用的备份工具---mysqldump(1)对单个库进行完全备份mysqldump-h[mysql_address]-u[userna......
  • 6.mysql优化案例
    1.单表优化;   进行优化:删除原来的三个字段的索引,创建二个字段的索引;    2.两表关联:左连接,在右表创建索引 右连接,在左表创建......
  • MySQL查询(二)
    MySQL查询(二)函数调用:select函数名(实参列表)[from表]1.常见函数1.1.单行函数字符函数length获取参数值的字节个数,一个字母是1个字节,一个汉字3个字节sele......
  • MySQL函数
     GROUP_CONCATSUBSTRING_INDEX例子:SUBSTRING_INDEX(GROUP_CONCAT(ll.xorderbyll.seqdesc,ll.createdesc),",",2) mysql中函数CONCAT及GROUP_CONCAT的使用ht......