{"id":10921,"date":"2024-03-09T16:56:44","date_gmt":"2024-03-09T08:56:44","guid":{"rendered":"https:\/\/www.orczhou.com\/?p=10921"},"modified":"2024-03-12T17:42:12","modified_gmt":"2024-03-12T09:42:12","slug":"sysbench-duplicate-entry-key","status":"publish","type":"post","link":"https:\/\/www.orczhou.com\/index.php\/2024\/03\/sysbench-duplicate-entry-key\/","title":{"rendered":"Sysbench\u538b\u6d4bMySQL\u4e2d\u9047\u5230\u7684&#8221;Duplicate entry&#8221;\u95ee\u9898"},"content":{"rendered":"\n<p>\u6700\u8fd1\uff0c\u4f7f\u7528sysbench\u6d4b\u8bd5\u65f6\uff0c\u5c1d\u8bd5\u4f7f\u7528\u53c2\u6570<code>--skip_trx=on<\/code>\uff0c\u5f88\u5feb\u5728\u5b9e\u9645\u7684\u6d4b\u8bd5\u4e2d\u5c31\u9047\u5230\u4e86<code>Duplicate entry<\/code>\u7684\u62a5\u9519\uff0c\u8be6\u7ec6\u7684\u62a5\u9519\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">[ 105s ] thds: 64 tps: 845.68 qps: 15226.20 (r\/w\/o: 11833.15\/3393.04\/0.00) lat (ms,95%): 82.96 err\/s: 0.00 reconn\/s: 0.00\n[ 108s ] thds: 64 tps: 828.65 qps: 14846.32 (r\/w\/o: 11547.73\/3298.59\/0.00) lat (ms,95%): 92.42 err\/s: 0.00 reconn\/s: 0.00\n[ 111s ] thds: 64 tps: 811.67 qps: 14705.36 (r\/w\/o: 11439.69\/3265.67\/0.00) lat (ms,95%): 92.42 err\/s: 0.00 reconn\/s: 0.00\nFATAL: mysql_drv_query() returned error 1062 (Duplicate entry '172978' for key 'sbtest4.PRIMARY') for query 'INSERT INTO sbtest4 (id, k, c, pad) V\nALUES (172978, 743044, '85734897298-37760631172-31656179599-77290009462-94351507893-97022333300-02606364258-99231394161-86310536236-00514105136',\n'50908340877-51595671823-98046322819-52667567569-56801127593')'\nFATAL: `thread_run' function failed: \/usr\/share\/sysbench\/oltp_common.lua:488: SQL error, errno = 1062, state = '23000': Duplicate entry '172978' f\nor key 'sbtest4.PRIMARY'<\/code><\/pre>\n\n\n\n<p>\u4e5f\u6ce8\u610f\u5230\uff0c\u8fd9\u4e2a\u9519\u8bef\u7684\u51fa\u73b0\u6709\u4e00\u5b9a\u7684\u5076\u53d1\u6027\uff0c\u4f46\u662f\u9ad8\u5e76\u53d1\u3001\u957f\u65f6\u95f4\u538b\u6d4b\u51e0\u4e4e\u4e00\u5b9a\u4f1a\u9047\u5230\u4e86\uff08\u5728\u5f00\u542f\u4e86<code>--skip_trx=on<\/code>\u53c2\u6570\u540e\uff09\u3002\u56e0\u4e3aSysbench\u6ca1\u6709\u5bf9&#8221;Duplicate entry&#8221;\u8be5\u9519\u8bef\u8fdb\u884c\u5904\u7406\uff0c\u6d4b\u8bd5\u4f1a\u9000\u51fa\uff0c\u4e5f\u5c31\u65e0\u6cd5\u6b63\u5e38\u5b8c\u6210\u6d4b\u8bd5\u3002<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u539f\u56e0\u5206\u6790<\/h4>\n\n\n\n<p>\u5f53\u591a\u4e2a\u7ebf\u7a0b\u5e76\u53d1\u65f6\uff0c\u540c\u65f6\u6ca1\u6709\u4f7f\u7528<code>--skip_trx=on<\/code>\uff0c\u800c\u662f\u4f7f\u7528MySQL\u9ed8\u8ba4\u7684<a href=\"https:\/\/dev.mysql.com\/doc\/refman\/8.0\/en\/innodb-autocommit-commit-rollback.html\">auto commit<\/a>\u6a21\u5f0f\uff0c\u90a3\u4e48\u5728<code>oltp_read_write<\/code>\u6a21\u578b\u4e0b\u5219\u4e00\u5b9a\u7684\u6982\u7387\uff08\u5c0f\u6982\u7387\uff09\u4f1a\u51fa\u73b0\u5982\u4e0b\u7684\u573a\u666f\uff1a<\/p>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\" style=\"flex-basis:75%\">\n<figure class=\"wp-block-table\"><table><tbody><tr><td class=\"has-text-align-center\" data-align=\"center\">\u65f6\u95f4\u7ebf<\/td><td>\u7ebf\u7a0bA<\/td><td>\u7ebf\u7a0bB<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">1<\/td><td>\u751f\u6210\u968f\u673aID <strong>X<\/strong><\/td><td><\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">2<\/td><td><\/td><td>\u751f\u6210\u968f\u673aID <strong>X<\/strong><\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">3<\/td><td>\u5220\u9664 id \u4e3a <strong>X<\/strong> \u7684\u8bb0\u5f55 <br>delete from sbtest where id = X<\/td><td><\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">4<\/td><td><\/td><td>\u5220\u9664 id \u4e3a <strong>X<\/strong> \u7684\u8bb0\u5f55<br>delete from sbtest where id = X<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">5<\/td><td>\u5199\u5165 id \u4e3a <strong>X<\/strong> \u7684\u8bb0\u5f55<br>insert into sbtest (id&#8230;) values ( X &#8230;)<\/td><td><\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">6<\/td><td><\/td><td>\u5199\u5165 id \u4e3a <strong>X<\/strong> \u7684\u8bb0\u5f55<br>insert into sbtest (id&#8230;) values ( X &#8230;)<\/td><\/tr><\/tbody><\/table><\/figure>\n<\/div>\n<\/div>\n\n\n\n<p>\u5728\u4e0a\u9762\u7684\u573a\u666f\u4e0b\uff0c\u6700\u540e\u4e00\u6b65\uff0c\u7ebf\u7a0bB\u518d\u6b21\u5199\u5165id \u4e3a <strong>X<\/strong> \u7684\u8bb0\u5f55\u65f6\uff0c\u5219\u4f1a\u51fa\u73b0\u51b2\u7a81\u3002<\/p>\n\n\n\n<p>\u4e00\u822c\u6765\u8bf4\uff0c\u5373\u4fbf\u53d1\u751f\u5982\u4e0a\u60c5\u51b5\uff0c\u4e5f\u4e0d\u4f1a\u51fa\u73b0<code>Duplicate entry<\/code>\u7684\u62a5\u9519\u3002\u4f46\uff0c\u7ec4\u5408\u4e00\u4e9b\u60c5\u51b5\uff0c\u5219\u4f1a\u51fa\u73b0\u3002\u4f8b\u5982\uff0c\u5728\u8fd9\u91cc\uff0c\u6211\u4eec\u4f7f\u7528\u4e86<code>--skip_trx=on<\/code>\uff0c\u90a3\u4e48\u7ebf\u7a0bA\u7684\u5982\u4e0a\u884c\u4e3a\u4e0d\u662f\u5728\u4e00\u4e2a\u4e8b\u52a1\u4e2d\uff0c\u6bcf\u4e2a\u64cd\u4f5c\u662f\u4e00\u4e2a\u72ec\u7acb\u7684\u4e8b\u52a1\uff0c\u90a3\u4e48\u5c31\u4f1a\u51fa\u73b0<code>Duplicate entry<\/code>\u62a5\u9519\u3002<\/p>\n\n\n\n<p>\u5982\u679c\u6ca1\u6709\u4f7f\u7528<code>--skip_trx=on<\/code>\u53c2\u6570\uff0c\u90a3\u4e48\u5728\u7ebf\u7a0b2\u5c1d\u8bd5\u5220\u9664\u8bb0\u5f55\u65f6\uff0c\u5219\u4f1a\u9047\u5230\u9501\u7b49\u5f85\uff0c\u76f4\u5230\u7ebf\u7a0b1\u7684\u76f8\u5173\u64cd\u4f5c\u5168\u90e8\u5b8c\u6210\u3002\u4e5f\u5c31\u4e0d\u4f1a\u51fa\u73b0\u62a5\u9519\u3002<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u907f\u514d\u8be5\u9519\u8bef<\/h4>\n\n\n\n<p>\u5728\u5f00\u542f\u4e86&#8211;skip_trx=on\u4e4b\u540e\uff0c\u5982\u679c\u8fd0\u884c\u65f6\u95f4\u8db3\u591f\u957f\uff0c\u4e14\u662f\u591a\u7ebf\u7a0b\u5e76\u53d1\uff0c\u5219\u51e0\u4e4e\u4e00\u5b9a\u4f1a\u9047\u5230\u5982\u4e0a\u9519\u8bef\u3002\u53ef\u4ee5\u8003\u8651\u5982\u4e0b\u65b9\u6848\u907f\u514d\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u4fee\u6539sysbench\u4f7f\u7528MySQL\u7684<code>INSERT ... ON DUPLICATE KEY UPDATE<\/code>\uff08<a href=\"https:\/\/dev.mysql.com\/doc\/refman\/8.0\/en\/insert-on-duplicate.html\">\u53c2\u8003<\/a>\uff09\u66ff\u6362\u539f\u59cb\u7684<code>INSERT<\/code>\u8bed\u53e5<\/li>\n\n\n\n<li>\u5c1d\u8bd5\u6587\u6863<a href=\"https:\/\/archive.fosdem.org\/2017\/schedule\/event\/sysbench\/attachments\/slides\/1519\/export\/events\/attachments\/sysbench\/slides\/1519\/sysbench.pdf\">sysbench 1.0: teaching an old dog new tricks<\/a>\u4e2d\u4f7f\u7528hook\u5c1d\u8bd5\u5904\u7406  <\/li>\n<\/ul>\n\n\n\n<p>\u4e00\u822c\u6765\u8bf4\uff0c\u56e0\u4e3a\u5f53\u8868\u7684\u8bb0\u5f55\u6570\u975e\u5e38\u591a\u65f6\uff0c\u9047\u5230\u8be5\u7c7b\u51b2\u7a81\u7684\u6982\u7387\u6bd4\u8f83\u5c0f\uff0c\u505a\u5982\u4e0a\u5904\u7406\u5e76\u4e0d\u4f1a\u5f71\u54cd\u6d4b\u8bd5\u7684\u201c\u4e00\u81f4\u6027\u201d\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"c\" class=\"language-c\">function sysbench.hooks.sql_error_ignorable(err)\n  if err.sql_errno == 1062 then -- ER_DUP_ENTRY\n    -- do nothing\n    -- con:reconnect()\n    return true\n  end\nend<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6700\u8fd1\uff0c\u4f7f\u7528sysbench\u6d4b\u8bd5\u65f6\uff0c\u5c1d\u8bd5\u4f7f\u7528\u53c2\u6570&#8211;skip_trx=on\uff0c\u5f88\u5feb\u5728\u5b9e\u9645\u7684\u6d4b\u8bd5\u4e2d\u5c31\u9047\u5230\u4e86Duplicate entry\u7684\u62a5\u9519\uff0c\u8be6\u7ec6\u7684\u62a5\u9519\u5982\u4e0b\uff1a \u4e5f\u6ce8\u610f\u5230\uff0c\u8fd9\u4e2a\u9519\u8bef\u7684\u51fa\u73b0\u6709\u4e00\u5b9a\u7684\u5076\u53d1\u6027\uff0c\u4f46\u662f\u9ad8\u5e76\u53d1\u3001\u957f\u65f6\u95f4\u538b\u6d4b\u51e0\u4e4e\u4e00\u5b9a\u4f1a\u9047\u5230\u4e86\uff08\u5728\u5f00\u542f\u4e86&#8211;skip_trx=on\u53c2\u6570\u540e\uff09\u3002\u56e0\u4e3aSysbench\u6ca1\u6709\u5bf9&#8221;Duplicate entry&#8221;\u8be5\u9519\u8bef\u8fdb\u884c\u5904\u7406\uff0c\u6d4b\u8bd5\u4f1a\u9000\u51fa\uff0c\u4e5f\u5c31\u65e0\u6cd5\u6b63\u5e38\u5b8c\u6210\u6d4b\u8bd5\u3002 \u539f\u56e0\u5206\u6790 \u5f53\u591a\u4e2a\u7ebf\u7a0b\u5e76\u53d1\u65f6\uff0c\u540c\u65f6\u6ca1\u6709\u4f7f\u7528&#8211;skip_trx=on\uff0c\u800c\u662f\u4f7f\u7528MySQL\u9ed8\u8ba4\u7684auto commit\u6a21\u5f0f\uff0c\u90a3\u4e48\u5728oltp_read_write\u6a21\u578b\u4e0b\u5219\u4e00\u5b9a\u7684\u6982\u7387\uff08\u5c0f\u6982\u7387\uff09\u4f1a\u51fa\u73b0\u5982\u4e0b\u7684\u573a\u666f\uff1a \u65f6\u95f4\u7ebf \u7ebf\u7a0bA \u7ebf\u7a0bB 1 \u751f\u6210\u968f\u673aID X 2 \u751f\u6210\u968f\u673aID X 3 \u5220\u9664 id \u4e3a X \u7684\u8bb0\u5f55 delete from sbtest where id = X 4 \u5220\u9664 id \u4e3a X \u7684\u8bb0\u5f55delete from sbtest where id = X 5 \u5199\u5165 id \u4e3a X \u7684\u8bb0\u5f55insert into sbtest (id&#8230;) values ( X &#8230;) 6 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"a-temp-temp","format":"standard","meta":{"_eb_attr":"","inline_featured_image":false,"_tocer_settings":[],"footnotes":""},"categories":[6],"tags":[],"class_list":["post-10921","post","type-post","status-publish","format-standard","hentry","category-mysql"],"_links":{"self":[{"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/posts\/10921","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/comments?post=10921"}],"version-history":[{"count":13,"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/posts\/10921\/revisions"}],"predecessor-version":[{"id":10941,"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/posts\/10921\/revisions\/10941"}],"wp:attachment":[{"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/media?parent=10921"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/categories?post=10921"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/tags?post=10921"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}