Copying a table structure in MySQL

Source: Internet
Author: User

MySQL may encounter problems when modifying the table structure. The copy time of MySQL when modifying the table structure seriously affects the operation of MySQL when modifying the table structure. The problem is analyzed below.

In the mysql database, perform DDL operations on a created table, such as adding a field. During the test, it is found that the ddl operation takes a very long time. In oracle, you can only modify the data dictionary. The operation time is very short, and the blocking time of DML is relatively short. The ddl operation on a mysql database table is very different from that on an oracle database. It first needs to copy the original table to a temporary table, which does not block the select operation during this period, all changes (update, delete, insert) are blocked. ddl operations on the temporary table are completed. The original table is deleted and the temporary table is renamed.
If a large table is changed to ddl, for example, 40 GB, the copy time is intolerable and all DML operations are blocked, making the business unable to continue.

The following is the test process:

 
 
  1. [Coolcode]
  2. Mysql> desc t1;
  3. + ----- + -- + --- +
  4. | Field | Type | Null | Key | Default | Extra |
  5. + ----- + -- + --- +
  6. | Id | int (11) | YES | MUL | NULL |
  7. | Nick | varchar (32) | YES | NULL |
  8. | Email | varchar (32) | YES | NULL |
  9. | Gmt_create | datetime | YES | NULL |
  10. | Gmt_modified | datetime | YES | NULL |
  11. + ----- + -- + --- +
  12. Mysql> select count (*) from t1;
  13. + ---- +
  14. | Count (*) |
  15. + ---- +
  16. | 1, 2228017 |
  17. + ---- +
  18. 1 row in set (1.78 sec)
  19. [/Coolcode]
  20. Add a column to change the table structure:
  21. [Coolcode]
  22. Mysql> alter table t1 add (tel varchar (20 ));
  23. Query OK, 2304923 rows affected (41.03 sec)
  24. Records: 2304923 Duplicates: 0 Warnings: 0
  25. [/Coolcode]
  26. During the above table structure change process, start another session to perform select query and update operations:
  27. [Coolcode]
  28. Mysql> select count (*) from t1;
  29. + ---- +
  30. | Count (*) |
  31. + ---- +
  32. | 1, 2304923 |
  33. + ---- +
  34. 1 row in set (2.10 sec)
  35.  
  36. Mysql> select * from t1 limit 10;
  37. + -- + --- + ------ + ------- +
  38. | Id | nick | email | gmt_create | gmt_modified |
  39. + -- + --- + ------ + ------- +
  40. | 0 | nick0 | nick0@taobao.com | 00:00:00 | 00:00:00 |
  41. | 1 | nick1 | nick1@taobao.com | 00:00:00 | 00:00:00 |
  42. | 2 | nick2 | nick2@taobao.com | 00:00:00 | 00:00:00 |
  43. | 3 | nick3 | nick3@taobao.com | 00:00:00 | 00:00:00 |
  44. | 4 | nick4 | nick4@taobao.com | 00:00:00 | 00:00:00 |
  45. | 5 | nick5 | nick5@taobao.com | 00:00:00 | 00:00:00 |
  46. | 6 | nick6 | nick6@taobao.com | 00:00:00 | 00:00:00 |
  47. | 7 | nick7 | nick7@taobao.com | 00:00:00 | 00:00:00 |
  48. | 8 | nick8 | nick8@taobao.com | 00:00:00 | 00:00:00 |
  49. | 9 | nick9 | nick9@taobao.com | 00:00:00 | 00:00:00 |
  50. + -- + --- + ------ + ------- +
  51. 10 rows in set (0.00 sec)
  52.  
  53. Mysql> update t1 set nick = 'test _ nick 'where id = 1;
  54. Query OK, 4 rows affected (43.89 sec)-blocking time here
  55. Rows matched: 4 Changed: 4 Warnings: 0
  56. [/Coolcode]
  57.  

From the above experiment, we can see that mysql does not block select queries when performing ddl operations on tables, but seriously blocks dml operations. In addition, if you want to perform ddl operations on the table, because of a copy operation, do you have to calculate whether your available space is sufficient? If your system often needs to modify the table structure in MySQL, you have to consider this issue!
 

10 common MySQL command lines

Add new users through MySQL Command Line

Optimize MySQL statement instances through Indexes

MySQL permission table Introduction

Instances that grant MySQL user permissions

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.