Performance Comparison of MySQL insertion methods with large data volumes

Source: Internet
Author: User

Performance Comparison of MySQL insertion methods with large data volumes

Whether in daily business data processing or Database Import/Export, a large amount of data may be inserted. Both the insertion method and the database engine will affect the insertion speed. This article aims to analyze and compare various methods theoretically and practically to facilitate the selection of insertion methods in future applications.

Insert Analysis

The time required to insert a record in MySQL is composed of the following factors, and the number indicates the approximate proportion:

  • Connection: 3)
  • Send query to server: 2)
  • Analysis query: 2)
  • Insert record: 1x record size)
  • Insert index: 1x index)
  • Close: 1)

If we execute an SQL statement every time we insert an entry, we need to execute all the steps except connect and close N times. This is very time-consuming and there are several optimization methods:

The performance of each method is as follows.

Innodb Engine

InnoDB provides MySQL with a transaction-safe (ACID compliant) table with transaction (commit), rollback, and crash recovery capabilities. InnoDB provides locking on row level and foreign key constraints ).

InnoDB is designed to handle large-capacity database systems. Its CPU utilization is incomparable to other disk-based relational database engines. Technically, InnoDB is a complete database system on the MySQL background. InnoDB establishes a dedicated buffer pool in the primary memory for high-speed data buffering and indexing.

Test Environment

Macbook Air 12mid apache2.2.26 php5.5.10 mysql5.6.16

Data entries in total

After the database is inserted, the size of the database is 38.6 MB without an index.) 46.8 has an index)

  • Total insertion time of a single index: 229 s peak memory: 246KB
  • Total insertion time with an index: 242 s peak memory: 246KB
  • Total time consumed for batch insertion without indexes: 10 s peak memory: 8643KB
  • Total index insertion time: 16 s peak memory: 8643KB
  • Total insertion time of non-index transactions: 78 s peak memory: 246KB
  • Total insertion time of index transactions: 82 s peak memory: 246KB
  • No index Load Data insertion time: 12 s peak memory: 246KB
  • Total insertion time of index Load Data: 11 s peak memory: 246KB

MyIASM Engine

MyISAM is the default storage engine of MySQL. The design is simple and supports full-text search.

Test Environment

Macbook Air 12mid apache2.2.26 php5.5.10 mysql5.6.16

Data entries in total

After the data is inserted, the database size is 38.6 MB and no index is available)

  • Total insertion time of a single index: 82 s peak memory: 246KB
  • Total insertion time with an index: 86 s peak memory: 246KB
  • Total time consumed for batch insertion without indexes: 3 s peak memory: 8643KB
  • Total index insertion time: 7 s peak memory: 8643KB
  • No index Load Data insertion time: 6 s peak memory: 246KB
  • Total insertion time of index Load Data: 8 s peak memory: 246KB

Summary

I tested a small amount of Data, but I can probably understand the impact of these insertion methods on the speed. The fastest way is to Load Data. This method is relatively troublesome because it involves writing files, but it can take into account both the memory and speed.

Test code

 
 
  1. <? Php
  2. $ Dsn = 'mysql: host = localhost; dbname = test ';
  3. $ Db = new PDO ($ dsn, 'root', '', array (PDO: ATTR_PERSISTENT => true ));
  4. // Delete the last inserted data
  5. $ Db-> query ('delete from 'test '');
  6. // Start timing
  7. $ Start_time = time ();
  8. $ Sum = 1000000;
  9. // Test options
  10. $ Num = 1;
  11.  
  12. If ($ num = 1 ){
  13. // Insert a single entry
  14. For ($ I = 0; $ I <$ sum; $ I ++ ){
  15. $ Db-> query ("insert into 'test' ('id', 'name') values ($ I, 'tsetssdf ')");
  16. }
  17. } Elseif ($ num = 2 ){
  18. // Batch insert. To insert data to max_allowed_packet up to 0.1 million, insert data once every seconds.
  19. For ($ I = 0; $ I <$ sum; $ I ++ ){
  20. If ($ I = $ sum-1) {// last time
  21. If ($ I % 100000 = 0 ){
  22. $ Values = "($ I, 'testtest ')";
  23. $ Db-> query ("insert into 'test' ('id', 'name') values $ values ");
  24. } Else {
  25. $ Values. = ", ($ I, 'testtest ')";
  26. $ Db-> query ("insert into 'test' ('id', 'name') values $ values ");
  27. }
  28. Break;
  29. }
  30. If ($ I % 100000 = 0) {// insert only in this case
  31. If ($ I = 0 ){
  32. $ Values = "($ I, 'testtest ')";
  33. } Else {
  34. $ Db-> query ("insert into 'test' ('id', 'name') values $ values ");
  35. $ Values = "($ I, 'testtest ')";
  36. }
  37. } Else {
  38. $ Values. = ", ($ I, 'testtest ')";
  39. }
  40. }
  41. } Elseif ($ num = 3 ){
  42. // Insert a transaction
  43. $ Db-> beginTransaction ();
  44. For ($ I = 0; $ I <$ sum; $ I ++ ){
  45. $ Db-> query ("insert into 'test' ('id', 'name') values ($ I, 'tsetssdf ')");
  46. }
  47. $ Db-> commit ();
  48. } Elseif ($ num = 4 ){
  49. // File load data
  50. $ Filename = dirname (_ FILE _). '/test. SQL ';
  51. $ Fp = fopen ($ filename, 'w ');
  52. For ($ I = 0; $ I <$ sum; $ I ++ ){
  53. Fputs ($ fp, "$ I, 'testtest' \ r \ n ");
  54. }
  55. $ Db-> exec ("load data infile '$ filename 'into table test fields terminated ','");
  56. }
  57.  
  58. $ End_time = time ();
  59. Echo "Total time consumed", ($ end_time-$ start_time), "seconds \ n ";
  60. Echo "peak memory", round (memory_get_peak_usage ()/1000), "KB \ n ";
  61.  
  62. ?>

The above is the performance analysis and comparison of a variety of MySQL Data insertion methods, hoping to help you.

Blog Source: http://yansu.org/2014/04/16/insert-large-number-of-data-in-mysql.html

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.