{"id":417,"date":"2014-10-08T09:48:54","date_gmt":"2014-10-08T07:48:54","guid":{"rendered":"http:\/\/dbahire.com\/?p=417"},"modified":"2019-08-27T12:05:19","modified_gmt":"2019-08-27T10:05:19","slug":"testing-the-fastest-way-to-import-a-table-into-mysql-and-some-interesting-5-7-performance-results","status":"publish","type":"post","link":"https:\/\/jynus.com\/dbahire\/testing-the-fastest-way-to-import-a-table-into-mysql-and-some-interesting-5-7-performance-results\/","title":{"rendered":"Testing the Fastest Way to Import a Table into MySQL (and some interesting 5.7 performance results)"},"content":{"rendered":"<p>As I mentioned on my last post, <a href=\"https:\/\/jynus.com\/dbahire\/changes-in-configuration-of-global-variables-between-mysql-5-6-20-and-mysql-5-7-4-milestone-14\/\" title=\"Changes in Configuration of Global Variables between MySQL 5.6.20 and MySQL 5.7.4 \u201cMilestone 14\u2033\">where I compared the default configurations options in 5.6 and 5.7<\/a>, I have been doing some testing for a particular load in several versions of MySQL. What I have been checking is different ways to load a CSV file (the same file <a href=\"https:\/\/jynus.com\/dbahire\/which-compression-tool-should-i-use-for-my-database-backups\/\" title=\"Which Compression Tool Should I Use for my Database Backups? (Part I: Compression)\">I used for testing the compression tools<\/a>) into MySQL. For those seasoned MySQL DBAs and programmers, you probably know the answer, so you can jump over to my 5.6 versus 5.7 results. However, the first part of this post is dedicated for developers and MySQL beginners that want to know the answer to the title question, in a step-by-step fashion. I must say I also learned something, as I under- and over-estimated some of the effects of certain configuration options for this workload.<\/p>\n<p><strong>Disclaimers: I do not intend to do proper benchmarks, most of the results obtained here were produced in a couple of runs, and many of them with a default configuration. This is intended, as I want to show &#8220;bad practices&#8221;<\/strong> for people that is just starting to work with MySQL, and what they should avoid doing. It is only the 5.6 to 5.7 comparison that have left me wondering. <strong>Additional disclaimer<\/strong>: I do not call myself a programmer, and much less a python programmer, so I apologize in advance for my code- after all, <em>this is about MySQL<\/em>. The download link for the scripts is at the bottom.<\/p>\n<h3>The Rules<\/h3>\n<p>I start with a <a href=\"https:\/\/jynus.com\/dbahire\/nodes.osm.7z\">CSV file<\/a> (remember that it is actually a tab-separated values file) that is 3,700,635,579 bytes in size, has 46,741,126 rows and looks like this:<\/p>\n<pre>171773  38.6048402      -0.0489871      4       2012-08-25 00:37:46     12850816        472193  rubensd\n171774  38.6061981      -0.0496867      2       2008-01-19 10:23:21     666916  9250    j3m\n171775  38.6067166      -0.0498342      2       2008-01-19 10:23:21     666916  9250    j3m\n171776  38.6028122      -0.0497136      5       2012-08-25 00:26:40     12850816        472193  rubensd\n171933  40.4200658      -3.7016652      6       2011-11-29 05:37:42     9984625 33103   sergionaranja \n<\/pre>\n<p>I want to load it into a table with the following structure:<\/p>\n<pre lang=\"sql\">CREATE TABLE `nodes` (\n  `id` bigint PRIMARY KEY,\n  `lat` decimal(9,7),\n  `lon` decimal(10,7),\n  `version` int,\n  `timestamp` timestamp,\n  `changeset` bigint,\n  `uid` bigint,\n  `user` varchar(255)\n);\n<\/pre>\n<p>The import finish time will be defined as the moment the table is crash safe (even if there is some pending IO). That means that for InnoDB, the last <code>COMMIT<\/code> has to be successful and <code>flush_log_at_trx_commit<\/code> must be equal to 1, meaning that even if there is pending IO to be made, it is fully durable on disk (it is crash-resistant). For MyISAM, that means that I force a <code>FLUSH TABLES<\/code> before finishing the test. Those are, of course, not equivalent but it is at least a way to make sure that everything is more or less disk-synced. This is the ending part of all my scripts:<\/p>\n<pre lang=\"python\"># finishing\nif (options.engine == 'MyISAM'):\n    cursor.execute('FLUSH TABLES')\nelse:\n    db.commit()\n\ncursor.close()\ndb.close()\n<\/pre>\n<p>For the hardware and OS, check the specs on <a href=\"https:\/\/jynus.com\/dbahire\/which-compression-tool-should-i-use-for-my-database-backups\/\">this previous post<\/a>&#8211; I used the same environment as the one mentioned there, with the exception of using CentOS7 instead of 6.5.<\/p>\n<h3>The naive method<\/h3>\n<p>Let&#8217;s say I am a developer being tasked with loading a file regularly into MySQL- how would I do that? I would probably be tempted to use a CSV parsing library, the mysql connector and link them together in a loop. That would work, wouldn&#8217;t it? The main parts of the code would look like this (<code>load_data_01.py<\/code>):<\/p>\n<pre lang=\"python\"># import\ninsert_node = \"INSERT INTO nodes (id, lat, lon, version, timestamp, changeset, uid, user) VALUES (%s, %s, %s, %s, %s, %s, %s, %s)\"\n\nwith open(CSV_FILE, 'rb') as csv_file:\n    csv_reader = csv.reader(csv_file, delimiter='\\t')\n    for node in csv_reader:\n        cursor.execute(insert_node, node)\n<\/pre>\n<p>As I am playing the role of a developer without MySQL experience, I would also use the default configuration. Let&#8217;s see what we get (again, that is why I call these &#8220;tests&#8221;, and not benchmarks). Lower is better:<br \/>\n<a href=\"https:\/\/jynus.com\/dbahire\/wp-content\/uploads\/2014\/10\/load_data_011.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/jynus.com\/dbahire\/wp-content\/uploads\/2014\/10\/load_data_011.png\" alt=\"load_data_01\" class=\"aligncenter size-full wp-image-487\" width=\"955\" height=\"769\"><\/a><\/p>\n<table>\n<thead style=\"text-align:center; font-weight: bold;\">\n<tr>\n<td><em>MySQL Version<\/em><\/td>\n<td>5.1.72<\/td>\n<td>5.5.39<\/td>\n<td>5.6.20<\/td>\n<td>5.7.4<\/td>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td><em>Load time (seconds)<\/em><\/td>\n<td>4708.594<\/td>\n<td>6274.304<\/td>\n<td>6499.033<\/td>\n<td>6939.722<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Wow, is that 5.1 being a 50% faster than the rest of versions? <strong>Absolutely not<\/strong>, remember that 5.5 was the first version to introduce InnoDB as the default engine, and InnoDB has additional transactional overhead and usually not good default configuration (unlike MyISAM, which is so simple that the default options can work in many cases). Let&#8217;s normalize our results by engine:<br \/>\n<a href=\"https:\/\/jynus.com\/dbahire\/wp-content\/uploads\/2014\/10\/load_data_01_by_engine.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/jynus.com\/dbahire\/wp-content\/uploads\/2014\/10\/load_data_01_by_engine.png\" alt=\"load_data_01_by_engine\" class=\"aligncenter size-full wp-image-489\" width=\"850\" height=\"716\"><\/a><\/p>\n<table>\n<thead style=\"text-align:center; font-weight: bold;\">\n<tr>\n<td><em>MySQL Version<\/em><\/td>\n<td>5.1.72<\/td>\n<td>5.5.39<\/td>\n<td>5.6.20<\/td>\n<td>5.7.4<\/td>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td><em>MyISAM<\/em><\/td>\n<td>4708.594<\/td>\n<td>5010.655<\/td>\n<td>5149.791<\/td>\n<td>5365.005<\/td>\n<\/tr>\n<tr>\n<td><em>InnoDB<\/em><\/td>\n<td>6238.503<\/td>\n<td>6274.304<\/td>\n<td>6499.033<\/td>\n<td>6939.722<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>This seems more reasonable, doesn&#8217;t it? However, in this case, it seems that there is a slight regression in single-thread performance as the versions go up, specially on MySQL 5.7. Of course, it is early to draw conclusions, because <strong>this method of importing a CSV file, row by row, is one of the slowest ones, and we are using very poor configuration options (the defaults), which vary from version to version and should not be taken into account to draw conclusions<\/strong>.<\/p>\n<p>What we can say is that MyISAM seems to work better by default for this very particular scenario for the reasons I mentioned before, but it still takes 1-2 hours to load such a simple file.<\/p>\n<h3>The even more naive method<\/h3>\n<p>The next question is not: can we do it better, but, can we do it even slower? A particular text draw my attention when looking at the MySQL connector documentation:<\/p>\n<blockquote><p>Since by default Connector\/Python does not autocommit, it is important to call this method after every transaction that modifies data for tables that use transactional storage engines.<\/p><\/blockquote>\n<p>-from the <a href=\"http:\/\/dev.mysql.com\/doc\/connector-python\/en\/connector-python-api-mysqlconnection-commit.html\">connector\/python documentation<\/a><br \/>\nI though to myself- oh, so maybe we can speedup the import process by committing every single row to the database, one by one, don&#8217;t we? After all, we are inserting the table on a single huge transaction. Certainly, a huge number of small transactons will be better! \ud83d\ude42 This is the slightly modified code (<code>load_data_02.py<\/code>):<\/p>\n<pre lang=\"python\">insert_node = \"INSERT INTO nodes (id, lat, lon, version, timestamp, changeset, uid, user) VALUES (%s, %s, %s, %s, %s, %s, %s, %s)\"\n\nwith open('\/tmp\/nodes.csv', 'rb') as csv_file:\n    csv_reader = csv.reader(csv_file, delimiter='\\t')\n    for node in csv_reader:\n        cursor.execute(insert_node, node)\n        db.commit()\n<\/pre>\n<p>And I do not even have a fancy graphic to show you because <strong>after 2 hours, 19 minutes and 39.405 seconds, I canceled the import because only 111533 nodes had been inserted<\/strong> in MySQL 5.1.72 for InnoDB with the default configuration (<code>innodb_flush_log_at_trx_commit = 1<\/code>). Obviously, millions of <code>fsyinc<\/code>s will not make our load faster, consider this a leason learned.<\/p>\n<h3>Going forward: multi-inserts<\/h3>\n<p>The next step I wanted to test is how effective grouping queries was in a multi-insert statement. This method is used by <code>mysqldump<\/code>, and supposedly minimizes the SQL overhead of handling every single query (parsing, permission checking, query planning, etc.). This is the main code (<code>load_data_03.py<\/code>):<\/p>\n<pre lang=\"python\">concurrent_insertions = 100\n[...]\nwith open(CSV_FILE, 'rb') as csv_file:\n    csv_reader = csv.reader(csv_file, delimiter='\\t')\n    i = 0\n    node_list = []\n    for node in csv_reader:\n        i += 1\n\tnode_list += node\n        if (i == concurrent_insertions):\n            cursor.execute(insert_node, node_list)\n            i = 0\n            node_list = []\n    csv_file.close()\n\n# insert the reminder nodes\nif i &gt; 0:\n    insert_node = \"INSERT INTO nodes (id, lat, lon, version, timestamp, changeset, uid, user) VALUES (%s, %s, %s, %s, %s, %s, %s, %s)\"\n    for j in xrange(i - 1):\n        insert_node += ', (%s, %s, %s, %s, %s, %s, %s, %s)'\n    cursor.execute(insert_node, node_list)\n<\/pre>\n<p>We tested it with a sample of 100 rows inserted with every query. What are the results? Lower is better:<br \/>\n<a href=\"https:\/\/jynus.com\/dbahire\/wp-content\/uploads\/2014\/10\/load_data_03.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/jynus.com\/dbahire\/wp-content\/uploads\/2014\/10\/load_data_03.png\" alt=\"load_data_03.py results\" class=\"aligncenter size-full wp-image-494\" width=\"835\" height=\"703\"><\/a><\/p>\n<table>\n<thead style=\"text-align:center; font-weight: bold;\">\n<tr>\n<td><em>MySQL Version<\/em><\/td>\n<td>5.1.72<\/td>\n<td>5.5.39<\/td>\n<td>5.6.20<\/td>\n<td>5.7.4<\/td>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td><em>MyISAM<\/em><\/td>\n<td>1794.693<\/td>\n<td>1822.081<\/td>\n<td>1861.341<\/td>\n<td>1888.283<\/td>\n<\/tr>\n<tr>\n<td><em>InnoDB<\/em><\/td>\n<td>3645.454<\/td>\n<td>3455.800<\/td>\n<td>2849.299<\/td>\n<td>3032.496<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>With this method we observe <strong>an improvement of the import time of 262-284% from the original time for MyISAM and of 171-229% from the original time for InnoDB<\/strong>. Remember that this method will not scale indefinitely, as we will encounter the package size limit if we try to insert too many rows at the same time. However, it is a clear improvement over the one-row-at-a-time approach.<\/p>\n<p>MyISAM times are essentially the same between versions while InnoDB shows an improvement over time (which may be due to code and optimization changes, but also to the defaults like the transaction log size changing, too), except again between 5.6 and 5.7.<\/p>\n<h3>The right method for importing data: Load Data<\/h3>\n<p>If you have a minimum of experience with MySQL, you know that there is a specialized keyword for data imports, and that is <code>LOAD DATA<\/code>. Let&#8217;s see how the code would end up looking like by using this option (<code>load_data_04.py<\/code>):<\/p>\n<pre lang=\"python\"># data import\nload_data = \"LOAD DATA INFILE '\" + CSV_FILE + \"' INTO TABLE nodes\"\n\ncursor.execute(load_data)\n<\/pre>\n<p>Simple, isn&#8217;t it? With this we are minimizing the SQL overhead, and executing the loop in the compiled C MySQL code. Let&#8217;s have a look at the results (lower is better):<br \/>\n<a href=\"https:\/\/jynus.com\/dbahire\/wp-content\/uploads\/2014\/10\/load_data_041.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/jynus.com\/dbahire\/wp-content\/uploads\/2014\/10\/load_data_041.png\" alt=\"load_data_04.py results\" class=\"aligncenter size-full wp-image-498\" width=\"833\" height=\"696\"><\/a><\/p>\n<table>\n<thead style=\"text-align:center; font-weight: bold;\">\n<tr>\n<td><em>MySQL Version<\/em><\/td>\n<td>5.1.72<\/td>\n<td>5.5.39<\/td>\n<td>5.6.20<\/td>\n<td>5.7.4<\/td>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td><em>MyISAM<\/em><\/td>\n<td>141.414<\/td>\n<td>149.612<\/td>\n<td>155.181<\/td>\n<td>166.836<\/td>\n<\/tr>\n<\/tbody>\n<tbody>\n<tr>\n<td><em>InnoDB<\/em><\/td>\n<td>2091.617<\/td>\n<td>1890.972<\/td>\n<td>920.615<\/td>\n<td>1041.702<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>In this case, <strong>MyISAM has a very dramatic improvement &#8211; <code>LOAD DATA<\/code> speeds up to 12x times the import. InnoDB, again still each one with the default parameters can improve the speed up to 3x times, and more significantly in the newer versions (5.6, 5.7) than the older ones (5.1, 5.5).<\/strong> I predict that this has to do much more with the different configuration of log files than with the code changes.<\/p>\n<h3>Trying to improve Load Data for MyISAM<\/h3>\n<p>Can we improve the load times for MyISAM? There are 2 things that I tried to do -augmenting the key_cache_size and disabling the Performance Schema for 5.6 and 5.7. I set up the <code>key_cache_size<\/code> to <code>600M<\/code> (trying to fit the primary key on memory) and I set the <code>performance_schema = 0<\/code>, and I tested the 3 remaining combinations. Lower is better:<br \/>\n<a href=\"https:\/\/jynus.com\/dbahire\/wp-content\/uploads\/2014\/10\/load_data_04_myisam.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/jynus.com\/dbahire\/wp-content\/uploads\/2014\/10\/load_data_04_myisam.png\" alt=\"load_data_04.py results for myisam\" class=\"aligncenter size-full wp-image-500\" width=\"795\" height=\"773\"><\/a><\/p>\n<table>\n<thead style=\"text-align:center; font-weight: bold;\">\n<tr>\n<td><em>MySQL Version<\/em><\/td>\n<td>5.1.72<\/td>\n<td>5.5.39<\/td>\n<td>5.6.20<\/td>\n<td>5.7.4<\/td>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td><em>default<\/em><\/td>\n<td>141.414<\/td>\n<td>149.612<\/td>\n<td>155.181<\/td>\n<td>166.836<\/td>\n<\/tr>\n<tr>\n<td><em>key_buffer_size=600M<\/em><\/td>\n<td>136.649<\/td>\n<td>170.622<\/td>\n<td>182.698<\/td>\n<td>191.228<\/td>\n<\/tr>\n<tr>\n<td><em>key_buffer_size=600M, P_S = OFF<\/em><\/td>\n<td>133.967<\/td>\n<td>170.677<\/td>\n<td>177.724<\/td>\n<td>186.171<\/td>\n<\/tr>\n<tr>\n<td><em>P_S = OFF<\/em><\/td>\n<td>142.592<\/td>\n<td>145.679<\/td>\n<td>150.684<\/td>\n<td>159.702<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>There are certain things to notice here:<\/p>\n<ul>\n<li><code>P_S=ON<\/code> and <code>P_S=OFF<\/code> should have no effect for MySQL 5.1 and 5.5, but it brings different results because of measuring errors. We must understand that only 2 significative figures should be taken into account.<\/li>\n<li><code>key_buffer_cache<\/code> does not in general improve performance, in fact I would say that it statistically worsens the performance. This is reasonable because after all, I am writing to filesystem cache, and a larger key cache might require costlier memory reservations, or more memory copys. This should be researched further to make a conclusion.<\/li>\n<li>Performance_schema may worsen the performance on this workload, but I am not statistically sure.<\/li>\n<li>MyISAM (or maybe the MySQL server) seems to have slightly worsen its performance for this specific workload (single threaded batch import).<\/li>\n<\/ul>\n<p>There are more things that I would like to try with MyISAM, like seeing the impact of the several row formats (fixed), but I wanted to follow up for other engines.<\/p>\n<h3>Trying to improve Load Data for InnoDB<\/h3>\n<p>InnoDB is a much more interesting engine, as it is ACID by default, and more complex. Can we make it as fast as MyISAM for importing?<\/p>\n<p>The first thing I wanted to do is to change the default values of the <code>innodb_log_file_size<\/code> and <code>innodb_buffer_pool_size<\/code>. The log is different by default before and after 5.6, and it is not suitable for a heavy write load. I set it for a first test to <code>2G<\/code>, as it is the largest size that 5.1 and 5.5 can use (actually, I set it to 2,147,483,136 as it has to be less than 2G), meaning that we have logs of about 4G. I also set the buffer pool for a convenient size, 8GB, enough to hold the whole dataset. Remember that one of the problems why InnoDB is so slow for imports is because it writes the new pages (at least) twice on disk -on the log, and on the tablespace. However, with these parameters, the second write should be mostly buffered on memory. These are the new results (lower is better):<br \/>\n<a href=\"https:\/\/jynus.com\/dbahire\/wp-content\/uploads\/2014\/10\/load_data_04_innodb1.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/jynus.com\/dbahire\/wp-content\/uploads\/2014\/10\/load_data_04_innodb1.png\" alt=\"load_data_04.py innodb results\" class=\"aligncenter size-full wp-image-505\" width=\"804\" height=\"697\"><\/a><\/p>\n<table>\n<thead style=\"text-align:center; font-weight: bold;\">\n<tr>\n<td><em>MySQL Version<\/em><\/td>\n<td>5.1.72<\/td>\n<td>5.5.39<\/td>\n<td>5.6.20<\/td>\n<td>5.7.4<\/td>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td><em>default<\/em><\/td>\n<td>1923.751<\/td>\n<td>1797.220<\/td>\n<td>850.636<\/td>\n<td>1008.349<\/td>\n<\/tr>\n<tr>\n<td><em>log_file_size=2G, buffer_pool=8G<\/em><\/td>\n<td>1044.923<\/td>\n<td>1012.488<\/td>\n<td>743.818<\/td>\n<td>850.868<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Now this is a test that starts to be more reasonable. We can comment that:<\/p>\n<ul>\n<li>Most of the improvements that we had before in 5.6 and 5.7 respect to 5.1 and 5.5 was due to the 10x size in logs.<\/li>\n<li>Still, 5.6 and 5.7 are faster than 5.1 and 5.5 (reasonable, as 5.6 had quite some impresive InnoDB changes, both on code and on configuration)<\/li>\n<li>InnoDB continues being at least 5x slower than MyISAM<\/li>\n<li>Still, 5.7 is slower than 5.6! We are having consistently a 13-18% regression in 5.7 (now I am starting to worry)<\/li>\n<\/ul>\n<p>I said before that the main overhead of InnoDB is writing the data twice (log and tables). This is actually wrong, as it may actually write it 3 times (on the double write area) and even 4 times, in the binary log. The binary log is not enabled by default, but the double write is, as it protects from corruption. While we never recommend disabling the latter on a production, the truth is that on an import, we do not care if the data ends up corrupted (we can delete it and import it again). There is also <a href=\"http:\/\/www.percona.com\/blog\/2014\/05\/23\/improve-innodb-performance-write-bound-loads\/\">some options on certain filesystems to avoid setting it up<\/a>.<\/p>\n<p>Other features that are in InnoDB for security, not for performance are the InnoDB checksums- they even were the cause of bottlenecks on very fast storage devices like flash PCI cards. In those cases, the CPU was too slow to calculate it! I suspect that that will not be a problem because more modern versions of MySQL (5.6 and 5.7) have the option to change it to the hardware-sped up function CRC32 and, mainly, because I am using a magnetic disk, which is the real bottleneck here. But let&#8217;s not believe on what we&#8217;ve learned and let&#8217;s test it.<\/p>\n<p>The other thing I can check is performance_schema overhead. I&#8217;ve found cases of workload where it produces significative overhead, while almost none in others. Let&#8217;s also test enabling and disabling it.<\/p>\n<p>These are the results (lower is better):<br \/>\n<a href=\"https:\/\/jynus.com\/dbahire\/wp-content\/uploads\/2014\/10\/load_data_04_innodb_22.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/jynus.com\/dbahire\/wp-content\/uploads\/2014\/10\/load_data_04_innodb_22.png\" alt=\"load_data_04.py results for innodb optimized\" class=\"aligncenter size-full wp-image-558\" width=\"815\" height=\"774\"><\/a><\/p>\n<table>\n<thead style=\"text-align:center; font-weight: bold;\">\n<tr>\n<td><em>MySQL Version<\/em><\/td>\n<td>5.1.72<\/td>\n<td>5.5.39<\/td>\n<td>5.6.20<\/td>\n<td>5.7.4<\/td>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td><em>default security and monitoring enabled<\/em><\/td>\n<td>1044.923<\/td>\n<td>1012.488<\/td>\n<td>743.818<\/td>\n<td>850.868<\/td>\n<\/tr>\n<tr>\n<td><em>doublewrite=off<\/em><\/td>\n<td>896.423<\/td>\n<td>848.110<\/td>\n<td>483.542<\/td>\n<td>468.943<\/td>\n<\/tr>\n<tr>\n<td><em>doublewrite=off,checksums=none<\/em><\/td>\n<td>889.827<\/td>\n<td>846.552<\/td>\n<td>488.311<\/td>\n<td>476.916<\/td>\n<\/tr>\n<tr>\n<td><em>doublewrite=off,checksums=none,P_S=off<\/em><\/td>\n<td><\/td>\n<td><\/td>\n<td>488.273<\/td>\n<td>467.716<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>There are several things to comment here, some of them I cannot even explain:<\/p>\n<ul>\n<li>The doublewrite feature doesn&#8217;t halve the performance, but it impacts it significantly (between a 15-30%)<\/li>\n<li>Without the doublewrite, most of the 5.7 regression goes away (why?)<\/li>\n<li>The doublewrite is also more significative in 5.6 and 5.7 than previous versions of MySQL. I would dare to tell that most of the other bottleneck may have been eliminated (or maybe it is just something like the buffer pool partitions being active by default?)<\/li>\n<li>The innodb checksum makes absolutely no difference for this workload and hardware.<\/li>\n<li>Again, I cannot give statistical significance to the overhead of the performance schema. However, I have obtained very variables results in these tests, having results with a 10% higher latency than the central values of the ones with it disabled, so I am not a hundred percent sure on this.<\/li>\n<\/ul>\n<p>In summary, with just a bit of tweaking, we can get results on InnoDB that are only 2x slower than MyISAM, instead of 5x or 12x.<\/p>\n<h3>Import in MyISAM, convert it to InnoDB<\/h3>\n<p>I&#8217;ve seem some people at some forums recommending importing a table as MyISAM, then convert it to InnoDB. Let&#8217;s see if we can bust or confirm this myth with the following code (<code>load_data_06.py<\/code>):<\/p>\n<pre lang=\"python\">load_data = \"LOAD DATA INFILE '\/tmp\/nodes.csv' INTO TABLE nodes\"\nalter_table = \"ALTER TABLE nodes ENGINE=InnoDB\"\n\ncursor.execute(load_data)\ncursor.execute(alter_table)\n<\/pre>\n<p>These are the comparisons (lower is better):<br \/>\n<a href=\"https:\/\/jynus.com\/dbahire\/wp-content\/uploads\/2014\/10\/load_data_06.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/jynus.com\/dbahire\/wp-content\/uploads\/2014\/10\/load_data_06.png\" alt=\"load_data_06.py results\" class=\"aligncenter size-full wp-image-509\" width=\"811\" height=\"729\"><\/a><\/p>\n<table>\n<thead style=\"text-align:center; font-weight: bold;\">\n<tr>\n<td><em>MySQL Version<\/em><\/td>\n<td>5.1.72<\/td>\n<td>5.5.39<\/td>\n<td>5.6.20<\/td>\n<td>5.7.4<\/td>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td><em>LOAD DATA InnoDB<\/em><\/td>\n<td>1923.751<\/td>\n<td>1797.220<\/td>\n<td>850.636<\/td>\n<td>1008.349<\/td>\n<\/tr>\n<tr>\n<td><em>LOAD DATA MyISAM; ALTER TABLE ENGINE=InnoDB<\/em><\/td>\n<td>2075.445<\/td>\n<td>2041.893<\/td>\n<td>1537.775<\/td>\n<td>1600.467<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>I can see how that could be almost true in 5.1, but it is definitely not true in supported versions of MySQL. <em>It is actually faster<\/em> than importing twice the table, once for MyISAM and another for InnoDB.<\/p>\n<p>I leave as a homework as a reader to check it for other engines, like MEMORY or CSV [Hint: Maybe we could import to this latest engine in a <em>different way<\/em>].<\/p>\n<h3>Parallel loading<\/h3>\n<p>MyISAM writes to tables using a full table lock (although it can perform in some cases concurrent inserts), but InnoDB only requires row-level locks in many cases. Can we speed up the process by doing a parallel loading? This is what I tried to test with my last test. I do not trust my programming skills (or do not have time) to perform the file-seeking and chunking in a performant way, so I will start with a pre-sliced .csv file into 8 chunks. It should not consume much time, but the limited synchronization tools on the default threading library, together with my limited time made me opt for this plan. We only need to understand that we do not start with the exact same scenario in this case. This is the code (<code>load_data_08.py<\/code>):<\/p>\n<pre lang=\"python\">NUM_THREADS = 4\n\ndef load_data(port, csv_file):\n    db = mysql.connector.connect(host=\"localhost\", port=port, user=\"msandbox\", passwd=\"msandbox\", database=\"test\")\n    cursor = db.cursor()\n    load_data_sql = \"LOAD DATA INFILE '\" + csv_file + \"' INTO TABLE nodes\"\n    cursor.execute(load_data_sql)   \n    db.commit()\n\nthread_list = []\n\nfor i in range(1, NUM_THREADS + 1):\n   t = threading.Thread(target=load_data, args=(port, '\/tmp\/nodes.csv' + `i`))\n   thread_list.append(t)\n\nfor thread in thread_list:\n   thread.start()\n\nfor thread in thread_list:\n   thread.join()\n<\/pre>\n<p>And these are the results, with different parameters:<br \/>\n<a href=\"https:\/\/jynus.com\/dbahire\/wp-content\/uploads\/2014\/10\/load_data_08.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/jynus.com\/dbahire\/wp-content\/uploads\/2014\/10\/load_data_08.png\" alt=\"load_data_08.py results\" class=\"aligncenter size-full wp-image-511\" width=\"794\" height=\"772\"><\/a><\/p>\n<table>\n<thead style=\"text-align:center; font-weight: bold;\">\n<tr>\n<td><em>MySQL Version<\/em><\/td>\n<td>5.1.72<\/td>\n<td>5.5.39<\/td>\n<td>5.6.20<\/td>\n<td>5.7.4<\/td>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td><em>1 thread, log_file_size=2G, buffer_pool=8G<\/em><\/td>\n<td>894.367<\/td>\n<td>859.965<\/td>\n<td>488.273<\/td>\n<td>467.716<\/td>\n<\/tr>\n<tr>\n<td><em>8 threads, log_file_size=2G, buffer_pool=8G<\/em><\/td>\n<td>752.233<\/td>\n<td>704.444<\/td>\n<td>370.598<\/td>\n<td>290.343<\/td>\n<\/tr>\n<tr>\n<td><em>8 threads, log_file_size=5G, buffer_pool=20G<\/em><\/td>\n<td><\/td>\n<td><\/td>\n<td>301.693<\/td>\n<td>243.544<\/td>\n<\/tr>\n<tr>\n<td><em>4 threads, log_file_size=5G, buffer_pool=20G<\/em><\/td>\n<td><\/td>\n<td><\/td>\n<td>295.884<\/td>\n<td>245.569<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>From this we can see that:<\/p>\n<ul>\n<li>There is little performance changes between loading in parallel with 4 or 8 threads. This is a machine with 4 cores (8 HT)<\/li>\n<li>Parallelization helps, although it doesn&#8217;t scale (4-8 threads gives around a 33% speed up)<\/li>\n<li>This is where 5.6 and specially 5.7 shines<\/li>\n<li>A larger transaction log and buffer pool (larger than 4G, only available in 5.6+) still helps with the load<\/li>\n<li>Parallel load with 5.7 is the fastest way in which I can load this file into a table using InnoDB: 243 seconds. It is 1.8x times the fastest way I can load a MyISAM table (5.1, single-threaded): 134 seconds. That is almost 200K rows\/s!<\/li>\n<\/ul>\n<h3>Summary and open questions<\/h3>\n<ul>\n<li>The fastest way you can import a table into MySQL without using raw files is the <code>LOAD DATA<\/code> syntax. Use parallelization for InnoDB for better results, and remember to tune basic parameters like your transaction log size and buffer pool. Careful programming and importing can make a &gt;2-hour problem became a 2-minute process. You can disable temporarily some security features for extra performance<\/li>\n<li>There seems to be an important regression in 5.7 for this particular single-threaded insert load for both MyISAM and InnoDB, with up to 15% worse performance than 5.6. I do not know yet why.<\/li>\n<li>On the bright side, there is also an important improvement (up to 20%) in relation to 5.6 with parallel write-load.<\/li>\n<li>Performance schema may have an impact on this particular workload, but I am unable to measure it reliably (it is closer to 0 than my measuring error). That is a good thing.<\/li>\n<\/ul>\n<p>I would be grateful if you can tell me if I have made any mistakes on my assumptions here.<\/p>\n<p>Here you can download the different <a href=\"https:\/\/jynus.com\/dbahire\/wp-content\/uploads\/2014\/10\/load_data_python.zip\">scripts in Python tested for the MySQL data loading<\/a>.<\/p>\n<p>Remember that these were not &#8220;formal&#8221; benchmarks, and I have no longer access to the machine where I generated them. I have yet to analyze if the same problem exists on 5.7.5. There are <a href=\"http:\/\/smalldatum.blogspot.com\/2014\/10\/low-concurrency-performance-for-point.html\">other people pointing to regressions under low concurrency, like Mark Callaghan<\/a>, maybe these are related? As usual, post a comment <a href=\"\/testing-the-fastest-way-to-import-a-table-into-mysql-and-some-interesting-5-7-performance-results\">here<\/a> or reach me on <a href=\"http:\/\/twitter.com\/dbahire_en\">Twitter<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>As I mentioned on my last post, where I compared the default configurations options in 5.6 and 5.7, I have been doing some testing for a particular load in several versions of MySQL. What I have been checking is different<\/p>\n","protected":false},"author":1,"featured_media":504,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[189,190,33,151,192,191,123,168,32,71,53,455,118,188,124],"class_list":["post-417","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-mysql","tag-5-1","tag-5-5","tag-5-6","tag-5-7","tag-5-7-4-en","tag-benchmark","tag-csv","tag-import","tag-innodb","tag-load-data","tag-myisam","tag-mysql","tag-performance","tag-regression","tag-tsv"],"_links":{"self":[{"href":"https:\/\/jynus.com\/dbahire\/wp-json\/wp\/v2\/posts\/417","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jynus.com\/dbahire\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jynus.com\/dbahire\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jynus.com\/dbahire\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jynus.com\/dbahire\/wp-json\/wp\/v2\/comments?post=417"}],"version-history":[{"count":54,"href":"https:\/\/jynus.com\/dbahire\/wp-json\/wp\/v2\/posts\/417\/revisions"}],"predecessor-version":[{"id":1006,"href":"https:\/\/jynus.com\/dbahire\/wp-json\/wp\/v2\/posts\/417\/revisions\/1006"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/jynus.com\/dbahire\/wp-json\/wp\/v2\/media\/504"}],"wp:attachment":[{"href":"https:\/\/jynus.com\/dbahire\/wp-json\/wp\/v2\/media?parent=417"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jynus.com\/dbahire\/wp-json\/wp\/v2\/categories?post=417"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jynus.com\/dbahire\/wp-json\/wp\/v2\/tags?post=417"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}