{"id":2315,"date":"2011-10-25T01:00:00","date_gmt":"2011-10-24T23:00:00","guid":{"rendered":"https:\/\/www.dbi-services.com\/blog\/partition-exchange-preview-in-mysql-56\/"},"modified":"2011-10-25T01:00:00","modified_gmt":"2011-10-24T23:00:00","slug":"partition-exchange-preview-in-mysql-56","status":"publish","type":"post","link":"https:\/\/www.dbi-services.com\/blog\/partition-exchange-preview-in-mysql-56\/","title":{"rendered":"Partition exchange preview in MySQL 5.6"},"content":{"rendered":"<p>MySQL 5.6 offers improved partition maintenance through &#8220;partition exchange&#8221;. Even if not yet available, dbi services performed some tests on the MySQL Community Edition 5.6.2 m5 release (still under development).<\/p>\n<p>Starting with release 5.1, MySQL offers table partitioning. As for any new feature, partitioning has been improved over the last years and now comes with quite a complete scope of functionalities:<\/p>\n<ul>\n<li>Range, list, hash, composite partition types are supported<\/li>\n<li>Advanced partitioning maintenance operations are also supported: merge, split (called reorganize), repair, optimize, etc.<\/li>\n<\/ul>\n<p>Among several missing features, one of them was quite &#8220;critical&#8221;. Frequently used in many data warehouse environments, &#8220;partition exchange&#8221; allows to exchange an empty partition with an existing table. This was a pity since we know how easy it is to quickly load data from a external\/legacy system on a MySQL server. This new feature has been introduced in MySQL 5.6 (still under development and not yet &#8220;GA&#8221;: generally available) and tested by dbi services.<\/p>\n<p>Let&#8217;s illustrate the feature through an example. At the end of the year, we receive a csv file with the sales information to be loaded on the data warehouse. This sales information contains the salesman_id, the product_id, the amount sold and the date of the sale.<\/p>\n<p>To load this file in a temporary (staging) table is very easy with MySQL. First of all let&#8217;s have a look at the file:<\/p>\n<p><span style=\"font-family: courier new,courier;\">head -5 \/tmp\/sales_range_2012.lst<br \/>\n2,43,193,2012-09-09<br \/>\n5,23,167,2012-11-05<br \/>\n3,29,182,2012-11-25<br \/>\n3,23,185,2012-11-03<br \/>\n1,18,180,2012-11-24<\/span><\/p>\n<p>To load this data, there is nothing easier. Loading this data is a straightforward process, we first create the staging table, then load the data with one MySQL statement:<\/p>\n<pre class=\"brush: sql; gutter: true; first-line: 1\">mysql&gt; CREATE TABLE temp_sales ( salesman_id INT, product_id\u00a0 INT, sales_amount INT,\u00a0 sales_date date) ENGINE=MyISAM;\n Query OK, 0 rows affected (0.00 sec)\nmysql&gt; LOAD DATA INFILE '\/tmp\/sales_range_2012.lst' INTO TABLE temp_sales FIELDS TERMINATED BY ',' LINES TERMINATED BY 'n';\n Query OK, 37787 rows affected (0.16 sec)\n Records: 37787\u00a0 Deleted: 0\u00a0 Skipped: 0\u00a0 Warnings: 0<\/pre>\n<p>Our sales table (partitioned by range) looks like this:<\/p>\n<pre class=\"brush: sql; gutter: true; first-line: 1\">mysql&gt; show create table sales_range G\n *************************** 1. row ***************************\n Table: sales_range\n Create Table: CREATE TABLE `sales_range` (\n `salesman_id` int(11) DEFAULT NULL,\n `product_id` int(11) DEFAULT NULL,\n `sales_amount` int(11) DEFAULT NULL,\n `sales_date` date DEFAULT NULL\n ) ENGINE=MyISAM DEFAULT CHARSET=latin1\n \/*!50100 PARTITION BY RANGE (YEAR(sales_date))\n (PARTITION sales_year_2008 VALUES LESS THAN (2009) ENGINE = MyISAM,\n PARTITION sales_year_2009 VALUES LESS THAN (2010) ENGINE = MyISAM,\n PARTITION sales_year_2010 VALUES LESS THAN (2011) ENGINE = MyISAM,\n PARTITION sales_year_2011 VALUES LESS THAN (2012) ENGINE = MyISAM) *\/\n 1 row in set (0.00 sec)<\/pre>\n<p>At this stage some data &#8220;transformation&#8221; might occur based on MySQL PL-SQL in the sales_temp table (if needed).<\/p>\n<p>Once the data is ready, MySQL 5.6 &#8220;partition exchange&#8221; might be used to load the sales data from 2012 in the 2012 partition.<\/p>\n<p>First of all, the partition &#8220;sales_year_2012&#8221; should be created within the sales_range table:<\/p>\n<pre class=\"brush: sql; gutter: true; first-line: 1\">mysql&gt; ALTER TABLE sales_range ADD PARTITION (PARTITION sales_year_2012 VALUES LESS THAN (2013) ENGINE = MyISAM);\n Query OK, 0 rows affected (0.01 sec)\n Records: 0\u00a0 Duplicates: 0\u00a0 Warnings: 0<\/pre>\n<p>Since the data is already loaded and transformed in sales_temp, we just have to exchange sales_year_2012 and temp_sales:<\/p>\n<pre class=\"brush: sql; gutter: true; first-line: 1\">mysql&gt; ALTER TABLE sales_range EXCHANGE PARTITION sales_year_2012 WITH TABLE temp_sales;\n Query OK, 0 rows affected (0.11 sec)<\/pre>\n<p>That&#8217;s it! All data contained in the sales_temp table has been anchored to the sales_range partitioned table. Let&#8217;s have a look at this with the &#8220;explain&#8221; feature, selecting the 2012 sales, only the sales_year_2012 partition is scanned:<\/p>\n<pre class=\"brush: sql; gutter: true; first-line: 1\">mysql&gt; explain partitions select count(*) from sales_range where sales_date&gt;= '2012-01-01';\n +----+-------------+-------------+-----------------+------+---------------+------+---------+------+\n | id | select_type | table\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 | partitions\u00a0\u00a0\u00a0\u00a0\u00a0 | type | possible_keys | key\u00a0 | key_len | ref\u00a0 +----+-------------+-------------+-----------------+------+---------------+------+---------+------+\n |\u00a0 1 | SIMPLE\u00a0\u00a0\u00a0\u00a0\u00a0 | sales_range | sales_year_2012 | ALL\u00a0 | NULL\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 | NULL | NULL\u00a0\u00a0\u00a0 | NULL +----+-------------+-------------+-----------------+------+---------------+------+---------+------+\n 1 row in set (0.00 sec)<\/pre>\n<h4>What&#8217;s next?<\/h4>\n<p>As mentioned in the MySQL documentation the next important feature for partitioning usage will be the possibility to perform parallel queries while computing SQL aggregations &#8211; i.e sum() &#8211; over several partitions. Several threads in parallel should read the several partitions to provide the whole result faster. MySQL is working on this topic.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>MySQL 5.6 proposes improved partition maintenance through &#8220;partition  exchange&#8221;. Even if not yet available, dbi services performed some tests  on the MySQL Community Edition5.6.2 m5 release (still under  Development).<\/p>\n","protected":false},"author":9,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[197],"tags":[23,45,144,253,67],"type_dbi":[],"class_list":["post-2315","post","type-post","status-publish","format-standard","hentry","category-application-integration-middleware","tag-dba","tag-documentation","tag-mysql","tag-mysql-administrator","tag-performance"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v27.2 (Yoast SEO v27.6) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>Partition exchange preview in MySQL 5.6 - dbi Blog<\/title>\n<meta name=\"description\" content=\"MySQL 5.6 offers improved partition maintenance through &quot;partition exchange&quot;. Even if not yet available, dbi services performed some tests on the MySQL Community Edition 5.6.2 m5 release (still under development).\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.dbi-services.com\/blog\/partition-exchange-preview-in-mysql-56\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Partition exchange preview in MySQL 5.6\" \/>\n<meta property=\"og:description\" content=\"MySQL 5.6 offers improved partition maintenance through &quot;partition exchange&quot;. Even if not yet available, dbi services performed some tests on the MySQL Community Edition 5.6.2 m5 release (still under development).\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.dbi-services.com\/blog\/partition-exchange-preview-in-mysql-56\/\" \/>\n<meta property=\"og:site_name\" content=\"dbi Blog\" \/>\n<meta property=\"article:published_time\" content=\"2011-10-24T23:00:00+00:00\" \/>\n<meta name=\"author\" content=\"Yann Neuhaus\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Yann Neuhaus\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/partition-exchange-preview-in-mysql-56\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/partition-exchange-preview-in-mysql-56\\\/\"},\"author\":{\"name\":\"Yann Neuhaus\",\"@id\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/#\\\/schema\\\/person\\\/5bfc203607127a4915b7950c4a108681\"},\"headline\":\"Partition exchange preview in MySQL 5.6\",\"datePublished\":\"2011-10-24T23:00:00+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/partition-exchange-preview-in-mysql-56\\\/\"},\"wordCount\":461,\"commentCount\":0,\"keywords\":[\"DBA\",\"Documentation\",\"MySQL\",\"MySQL administrator\",\"Performance\"],\"articleSection\":[\"Application integration &amp; Middleware\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/partition-exchange-preview-in-mysql-56\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/partition-exchange-preview-in-mysql-56\\\/\",\"url\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/partition-exchange-preview-in-mysql-56\\\/\",\"name\":\"Partition exchange preview in MySQL 5.6 - dbi Blog\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/#website\"},\"datePublished\":\"2011-10-24T23:00:00+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/#\\\/schema\\\/person\\\/5bfc203607127a4915b7950c4a108681\"},\"description\":\"MySQL 5.6 offers improved partition maintenance through \\\"partition exchange\\\". Even if not yet available, dbi services performed some tests on the MySQL Community Edition 5.6.2 m5 release (still under development).\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/partition-exchange-preview-in-mysql-56\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/partition-exchange-preview-in-mysql-56\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/partition-exchange-preview-in-mysql-56\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Accueil\",\"item\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Partition exchange preview in MySQL 5.6\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/\",\"name\":\"dbi Blog\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/#\\\/schema\\\/person\\\/5bfc203607127a4915b7950c4a108681\",\"name\":\"Yann Neuhaus\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/d2729b4bbf77b0fe6f6d2b9fb1b2686404c89a16ce2701e860bfd1406212f796?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/d2729b4bbf77b0fe6f6d2b9fb1b2686404c89a16ce2701e860bfd1406212f796?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/d2729b4bbf77b0fe6f6d2b9fb1b2686404c89a16ce2701e860bfd1406212f796?s=96&d=mm&r=g\",\"caption\":\"Yann Neuhaus\"},\"url\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/author\\\/yann-neuhaus\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Partition exchange preview in MySQL 5.6 - dbi Blog","description":"MySQL 5.6 offers improved partition maintenance through \"partition exchange\". Even if not yet available, dbi services performed some tests on the MySQL Community Edition 5.6.2 m5 release (still under development).","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.dbi-services.com\/blog\/partition-exchange-preview-in-mysql-56\/","og_locale":"en_US","og_type":"article","og_title":"Partition exchange preview in MySQL 5.6","og_description":"MySQL 5.6 offers improved partition maintenance through \"partition exchange\". Even if not yet available, dbi services performed some tests on the MySQL Community Edition 5.6.2 m5 release (still under development).","og_url":"https:\/\/www.dbi-services.com\/blog\/partition-exchange-preview-in-mysql-56\/","og_site_name":"dbi Blog","article_published_time":"2011-10-24T23:00:00+00:00","author":"Yann Neuhaus","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Yann Neuhaus","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.dbi-services.com\/blog\/partition-exchange-preview-in-mysql-56\/#article","isPartOf":{"@id":"https:\/\/www.dbi-services.com\/blog\/partition-exchange-preview-in-mysql-56\/"},"author":{"name":"Yann Neuhaus","@id":"https:\/\/www.dbi-services.com\/blog\/#\/schema\/person\/5bfc203607127a4915b7950c4a108681"},"headline":"Partition exchange preview in MySQL 5.6","datePublished":"2011-10-24T23:00:00+00:00","mainEntityOfPage":{"@id":"https:\/\/www.dbi-services.com\/blog\/partition-exchange-preview-in-mysql-56\/"},"wordCount":461,"commentCount":0,"keywords":["DBA","Documentation","MySQL","MySQL administrator","Performance"],"articleSection":["Application integration &amp; Middleware"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.dbi-services.com\/blog\/partition-exchange-preview-in-mysql-56\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.dbi-services.com\/blog\/partition-exchange-preview-in-mysql-56\/","url":"https:\/\/www.dbi-services.com\/blog\/partition-exchange-preview-in-mysql-56\/","name":"Partition exchange preview in MySQL 5.6 - dbi Blog","isPartOf":{"@id":"https:\/\/www.dbi-services.com\/blog\/#website"},"datePublished":"2011-10-24T23:00:00+00:00","author":{"@id":"https:\/\/www.dbi-services.com\/blog\/#\/schema\/person\/5bfc203607127a4915b7950c4a108681"},"description":"MySQL 5.6 offers improved partition maintenance through \"partition exchange\". Even if not yet available, dbi services performed some tests on the MySQL Community Edition 5.6.2 m5 release (still under development).","breadcrumb":{"@id":"https:\/\/www.dbi-services.com\/blog\/partition-exchange-preview-in-mysql-56\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.dbi-services.com\/blog\/partition-exchange-preview-in-mysql-56\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.dbi-services.com\/blog\/partition-exchange-preview-in-mysql-56\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Accueil","item":"https:\/\/www.dbi-services.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Partition exchange preview in MySQL 5.6"}]},{"@type":"WebSite","@id":"https:\/\/www.dbi-services.com\/blog\/#website","url":"https:\/\/www.dbi-services.com\/blog\/","name":"dbi Blog","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.dbi-services.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.dbi-services.com\/blog\/#\/schema\/person\/5bfc203607127a4915b7950c4a108681","name":"Yann Neuhaus","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/d2729b4bbf77b0fe6f6d2b9fb1b2686404c89a16ce2701e860bfd1406212f796?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/d2729b4bbf77b0fe6f6d2b9fb1b2686404c89a16ce2701e860bfd1406212f796?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/d2729b4bbf77b0fe6f6d2b9fb1b2686404c89a16ce2701e860bfd1406212f796?s=96&d=mm&r=g","caption":"Yann Neuhaus"},"url":"https:\/\/www.dbi-services.com\/blog\/author\/yann-neuhaus\/"}]}},"_links":{"self":[{"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/posts\/2315","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/users\/9"}],"replies":[{"embeddable":true,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/comments?post=2315"}],"version-history":[{"count":0,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/posts\/2315\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/media?parent=2315"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/categories?post=2315"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/tags?post=2315"},{"taxonomy":"type","embeddable":true,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/type_dbi?post=2315"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}