{"id":104,"date":"2010-03-24T12:39:08","date_gmt":"2010-03-24T17:39:08","guid":{"rendered":"http:\/\/thenoyes.com\/littlenoise\/?p=104"},"modified":"2010-03-24T15:41:21","modified_gmt":"2010-03-24T20:41:21","slug":"load-data-and-recovery","status":"publish","type":"post","link":"https:\/\/thenoyes.com\/littlenoise\/?p=104","title":{"rendered":"LOAD DATA and recovery"},"content":{"rendered":"<p>A little two-part quiz. If you get the first one without peeking, you&#8217;re worth your pay as a DBA. If you get the second one without peeking, you may tell your boss that some random guy on the Internet says you deserve a raise.<\/p>\n<p>Start with a text file, &#8216;test.txt&#8217;, with these three lines:<\/p>\n<p><code>1<br \/>\n1<br \/>\n2<\/code><\/p>\n<p>Set up the test in MySQL:<\/p>\n<p><code>DROP TABLE IF EXISTS t1;<br \/>\nCREATE TABLE t1 (id int primary key);<br \/>\nLOAD DATA INFILE 'test.txt' INTO TABLE t1;<\/code><\/p>\n<p><!--more--><\/p>\n<p>This gives &#8220;<code>ERROR 1062 (23000): Duplicate entry '1' for key 'PRIMARY'<\/code>&#8220;, which is expected. <\/p>\n<p>What&#8217;s in the table? <\/p>\n<p><spoiler 'Show Answer' 'Hide Answer'>It depends. If the engine is MyISAM, then you&#8217;ll have one row: the first &#8216;1&#8217; from the file was inserted, everything else was skipped. If the engine is InnoDB, you&#8217;ll have no rows, because the transaction would rollback. So either 1 row or 0 rows.<\/spoiler><\/p>\n<p>Now, pretend you&#8217;re setting up a slave, or there was a crash and you&#8217;re recovering from binary logs:<\/p>\n<p><code>mysqlbinlog bin.000001 | mysql<\/code><\/p>\n<p>How many rows are in t1 now?<\/p>\n<p><spoiler 'Show Answer' 'Hide Answer'><code><br \/>\nmysql> SELECT * FROM t1;<br \/>\n+----+<br \/>\n| id |<br \/>\n+----+<br \/>\n|  1 |<br \/>\n|  2 |<br \/>\n+----+<br \/>\n2 rows in set (0.00 sec)<\/code><\/p>\n<p>Why? The manual says, &#8220;mysqlbinlog converts LOAD DATA INFILE statements to LOAD DATA LOCAL INFILE statements,&#8221; and, &#8220;with LOCAL, the default duplicate-key handling behavior is the same as if IGNORE is specified.&#8221;<br \/>\n<a href=\" http:\/\/dev.mysql.com\/doc\/refman\/5.1\/en\/mysqlbinlog.html\"><br \/>\nhttp:\/\/dev.mysql.com\/doc\/refman\/5.1\/en\/mysqlbinlog.html<\/a><br \/>\n<a href=\"http:\/\/dev.mysql.com\/doc\/refman\/5.1\/en\/load-data.html\">http:\/\/dev.mysql.com\/doc\/refman\/5.1\/en\/load-data.html<\/a><\/p>\n<p>Note that a replicating slave will handle it correctly &#8211; if the master used LOCAL (and therefore IGNORE), the slave will do IGNORE. If the master did not use LOCAL or IGNORE and so got the error above, the slave will do the same, and so the data will match. So be advised: replication and <code>mysqlbinlog | mysql<\/code> may not give the same results.<\/spoiler><\/p>\n","protected":false},"excerpt":{"rendered":"<p>A little two-part quiz. If you get the first one without peeking, you&#8217;re worth your pay as a DBA. If you get the second one without peeking, you may tell your boss that some random guy on the Internet says you deserve a raise. Start with a text file, &#8216;test.txt&#8217;, with these three lines: 1 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[6],"tags":[],"class_list":["post-104","post","type-post","status-publish","format-standard","hentry","category-gotchas"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p2IBF1-1G","jetpack_sharing_enabled":true,"jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/thenoyes.com\/littlenoise\/index.php?rest_route=\/wp\/v2\/posts\/104","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/thenoyes.com\/littlenoise\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/thenoyes.com\/littlenoise\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/thenoyes.com\/littlenoise\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/thenoyes.com\/littlenoise\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=104"}],"version-history":[{"count":8,"href":"https:\/\/thenoyes.com\/littlenoise\/index.php?rest_route=\/wp\/v2\/posts\/104\/revisions"}],"predecessor-version":[{"id":112,"href":"https:\/\/thenoyes.com\/littlenoise\/index.php?rest_route=\/wp\/v2\/posts\/104\/revisions\/112"}],"wp:attachment":[{"href":"https:\/\/thenoyes.com\/littlenoise\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=104"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/thenoyes.com\/littlenoise\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=104"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/thenoyes.com\/littlenoise\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=104"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}