<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: REPLACE INTO: think twice</title>
	<atom:link href="http://code.openark.org/blog/mysql/replace-into-think-twice/feed" rel="self" type="application/rss+xml" />
	<link>http://code.openark.org/blog/mysql/replace-into-think-twice</link>
	<description>Blog by Shlomi Noach</description>
	<lastBuildDate>Thu, 09 Sep 2010 18:52:58 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
	<item>
		<title>By: shlomi</title>
		<link>http://code.openark.org/blog/mysql/replace-into-think-twice/comment-page-1#comment-16350</link>
		<dc:creator>shlomi</dc:creator>
		<pubDate>Wed, 04 Aug 2010 11:00:19 +0000</pubDate>
		<guid isPermaLink="false">http://code.openark.org/blog/?p=397#comment-16350</guid>
		<description>@ricardo,

Agreed on bulk updates.

Nevertheless it is common to see people using REPLACE INTO when they could have used INSERT INTO ... ON DUPLICATE KEY UPDATE for single rows.</description>
		<content:encoded><![CDATA[<p>@ricardo,</p>
<p>Agreed on bulk updates.</p>
<p>Nevertheless it is common to see people using REPLACE INTO when they could have used INSERT INTO &#8230; ON DUPLICATE KEY UPDATE for single rows.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: ricardo</title>
		<link>http://code.openark.org/blog/mysql/replace-into-think-twice/comment-page-1#comment-16346</link>
		<dc:creator>ricardo</dc:creator>
		<pubDate>Wed, 04 Aug 2010 09:11:10 +0000</pubDate>
		<guid isPermaLink="false">http://code.openark.org/blog/?p=397#comment-16346</guid>
		<description>i think you&#039;re missing the point of the replace query. It is not meant to substitute insert on duplicate key update, but it is the only solution if you want to do bulk updates coming from a subquery. insert on duplicate key update works well if you want to update a few rows at a time, not sure how u can use it to update thousands of rows w/  a single subquery.</description>
		<content:encoded><![CDATA[<p>i think you&#8217;re missing the point of the replace query. It is not meant to substitute insert on duplicate key update, but it is the only solution if you want to do bulk updates coming from a subquery. insert on duplicate key update works well if you want to update a few rows at a time, not sure how u can use it to update thousands of rows w/  a single subquery.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: saviola</title>
		<link>http://code.openark.org/blog/mysql/replace-into-think-twice/comment-page-1#comment-15063</link>
		<dc:creator>saviola</dc:creator>
		<pubDate>Wed, 23 Jun 2010 12:31:33 +0000</pubDate>
		<guid isPermaLink="false">http://code.openark.org/blog/?p=397#comment-15063</guid>
		<description>The two statements have different impact for INNODb table.
The REPLACE INTO acts as DELETE/INSERT for duplicates.
The INSERT ON DUPLIACTE UPDATE is true update.

If you have a child table defined with “on delete CASCADE”, the REPLACE INTO will delete the child record too.

I will use INSERT ON DUPLICATE rather than the REPLACE INTO for the above reason.</description>
		<content:encoded><![CDATA[<p>The two statements have different impact for INNODb table.<br />
The REPLACE INTO acts as DELETE/INSERT for duplicates.<br />
The INSERT ON DUPLIACTE UPDATE is true update.</p>
<p>If you have a child table defined with “on delete CASCADE”, the REPLACE INTO will delete the child record too.</p>
<p>I will use INSERT ON DUPLICATE rather than the REPLACE INTO for the above reason.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Brian</title>
		<link>http://code.openark.org/blog/mysql/replace-into-think-twice/comment-page-1#comment-1739</link>
		<dc:creator>Brian</dc:creator>
		<pubDate>Wed, 29 Apr 2009 20:56:25 +0000</pubDate>
		<guid isPermaLink="false">http://code.openark.org/blog/?p=397#comment-1739</guid>
		<description>It is worth noting, however, that performing an INSERT/ON DUPLICATE KEY UPDATE, because of its dual nature, will not give predictable results when requesting the &quot;last insert ID&quot; from the DB engine. The last insert ID is only updated when the engine performs an update.</description>
		<content:encoded><![CDATA[<p>It is worth noting, however, that performing an INSERT/ON DUPLICATE KEY UPDATE, because of its dual nature, will not give predictable results when requesting the &#8220;last insert ID&#8221; from the DB engine. The last insert ID is only updated when the engine performs an update.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: shlomi</title>
		<link>http://code.openark.org/blog/mysql/replace-into-think-twice/comment-page-1#comment-146</link>
		<dc:creator>shlomi</dc:creator>
		<pubDate>Thu, 18 Dec 2008 05:46:59 +0000</pubDate>
		<guid isPermaLink="false">http://code.openark.org/blog/?p=397#comment-146</guid>
		<description>Mike - exactly my point. I have the (unconfirmed) feeling most people are just using REPLACE INTO to do an &quot;insert if not exists or update if exists&quot;, in which case it is not the optimal solution.

Steve - thanks for sharing</description>
		<content:encoded><![CDATA[<p>Mike &#8211; exactly my point. I have the (unconfirmed) feeling most people are just using REPLACE INTO to do an &#8220;insert if not exists or update if exists&#8221;, in which case it is not the optimal solution.</p>
<p>Steve &#8211; thanks for sharing</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Steve W</title>
		<link>http://code.openark.org/blog/mysql/replace-into-think-twice/comment-page-1#comment-132</link>
		<dc:creator>Steve W</dc:creator>
		<pubDate>Wed, 17 Dec 2008 16:19:09 +0000</pubDate>
		<guid isPermaLink="false">http://code.openark.org/blog/?p=397#comment-132</guid>
		<description>I would add that the ON DUPLICATE KEY performs the operation much faster then a REPLACE INTO.  It&#039;s probably obvious that this true, but it can cut the processing time more then half if you don&#039;t have that many updates to run. 

On just a 256 row database, my page execution times went from 1.2 seconds to .43 seconds.  This was doing a REPLACE into on all the rows (running the same sql twice).  By updating it to do the ON DUPLICATE, the same script is much faster. 

The database is running some triggers and stored procedures to update a random row scheme.  By not having the DELETE/INSERT these triggers are never called.  YMMV on the actual speed increase. 

It takes a bit more planning with PHP scripts to account for the unique keys and not include them in the ON DUPLICATE portion, but it&#039;s worth it.</description>
		<content:encoded><![CDATA[<p>I would add that the ON DUPLICATE KEY performs the operation much faster then a REPLACE INTO.  It&#8217;s probably obvious that this true, but it can cut the processing time more then half if you don&#8217;t have that many updates to run. </p>
<p>On just a 256 row database, my page execution times went from 1.2 seconds to .43 seconds.  This was doing a REPLACE into on all the rows (running the same sql twice).  By updating it to do the ON DUPLICATE, the same script is much faster. </p>
<p>The database is running some triggers and stored procedures to update a random row scheme.  By not having the DELETE/INSERT these triggers are never called.  YMMV on the actual speed increase. </p>
<p>It takes a bit more planning with PHP scripts to account for the unique keys and not include them in the ON DUPLICATE portion, but it&#8217;s worth it.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: mike</title>
		<link>http://code.openark.org/blog/mysql/replace-into-think-twice/comment-page-1#comment-128</link>
		<dc:creator>mike</dc:creator>
		<pubDate>Wed, 17 Dec 2008 09:36:21 +0000</pubDate>
		<guid isPermaLink="false">http://code.openark.org/blog/?p=397#comment-128</guid>
		<description>This has bitten me a couple times, and usually what I really want is the ON DUPLICATE KEY, not the REPLACE INTO. Not for space reusage but for the row with the unique key I wanted to update if it existed.</description>
		<content:encoded><![CDATA[<p>This has bitten me a couple times, and usually what I really want is the ON DUPLICATE KEY, not the REPLACE INTO. Not for space reusage but for the row with the unique key I wanted to update if it existed.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: shlomi</title>
		<link>http://code.openark.org/blog/mysql/replace-into-think-twice/comment-page-1#comment-126</link>
		<dc:creator>shlomi</dc:creator>
		<pubDate>Wed, 17 Dec 2008 08:51:49 +0000</pubDate>
		<guid isPermaLink="false">http://code.openark.org/blog/?p=397#comment-126</guid>
		<description>Arjen, thanks,

 With regard to re-using the index slot, consider the following:

DROP TABLE IF EXISTS `person`;
CREATE TABLE  `person` (
  `person_id` int(11) NOT NULL auto_increment,
  `name` varchar(60) character set utf8 NOT NULL,
  `social_security_number` bigint(20) NOT NULL,
  PRIMARY KEY  (`person_id`),
  UNIQUE KEY `social_security_number` (`social_security_number`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

TRUNCATE TABLE person;
INSERT INTO person (name, social_security_number) VALUES (&#039;Alice&#039;, 123456);
INSERT INTO person (name, social_security_number) VALUES (&#039;Bob&#039;, 787878);
REPLACE INTO person (name, social_security_number) VALUES (&#039;Bob&#039;, 787878);

Both on InnoDB and on MyISAM, the results are:
&lt;code&gt;
+-----------+-------+------------------------+
&#124; person_id &#124; name  &#124; social_security_number &#124;
+-----------+-------+------------------------+
&#124;         1 &#124; Alice &#124;                 123456 &#124;
&#124;         3 &#124; Bob   &#124;                 787878 &#124;
+-----------+-------+------------------------+
&lt;/code&gt;

If the primary key value changes, I don&#039;t think the slot can be reused. What do you think?

With regard to having more than one row removed - nice! I&#039;ve never encountered such a situation. But still it seems to me more a hack. I agree that it must be used appropriately.</description>
		<content:encoded><![CDATA[<p>Arjen, thanks,</p>
<p> With regard to re-using the index slot, consider the following:</p>
<p>DROP TABLE IF EXISTS `person`;<br />
CREATE TABLE  `person` (<br />
  `person_id` int(11) NOT NULL auto_increment,<br />
  `name` varchar(60) character set utf8 NOT NULL,<br />
  `social_security_number` bigint(20) NOT NULL,<br />
  PRIMARY KEY  (`person_id`),<br />
  UNIQUE KEY `social_security_number` (`social_security_number`)<br />
) ENGINE=InnoDB DEFAULT CHARSET=latin1;</p>
<p>TRUNCATE TABLE person;<br />
INSERT INTO person (name, social_security_number) VALUES (&#8216;Alice&#8217;, 123456);<br />
INSERT INTO person (name, social_security_number) VALUES (&#8216;Bob&#8217;, 787878);<br />
REPLACE INTO person (name, social_security_number) VALUES (&#8216;Bob&#8217;, 787878);</p>
<p>Both on InnoDB and on MyISAM, the results are:<br />
<code><br />
+-----------+-------+------------------------+<br />
| person_id | name  | social_security_number |<br />
+-----------+-------+------------------------+<br />
|         1 | Alice |                 123456 |<br />
|         3 | Bob   |                 787878 |<br />
+-----------+-------+------------------------+<br />
</code></p>
<p>If the primary key value changes, I don&#8217;t think the slot can be reused. What do you think?</p>
<p>With regard to having more than one row removed &#8211; nice! I&#8217;ve never encountered such a situation. But still it seems to me more a hack. I agree that it must be used appropriately.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Arjen Lentz</title>
		<link>http://code.openark.org/blog/mysql/replace-into-think-twice/comment-page-1#comment-125</link>
		<dc:creator>Arjen Lentz</dc:creator>
		<pubDate>Wed, 17 Dec 2008 08:32:13 +0000</pubDate>
		<guid isPermaLink="false">http://code.openark.org/blog/?p=397#comment-125</guid>
		<description>Two things...

REPLACE is completely different from the INSERT ... ON DUPLICATE KEY UPDATE ... because replace removes and replaces rows complete whereas the no dup key update construct allows you to update values in the existing row.

If I remember correctly, MySQL will actually re-use the index slot, if possible, so it&#039;s not as inefficient as you might think. In any case, the alternative equivalent to REPLACE is DELETE+INSERT, which is definitely no faster and quite likely slower.

By the way, REPLACE can actually delete more than one row, this can happen if you also have other UNIQUE constraints. Again, this can be a very useful feature, when used appropriately.</description>
		<content:encoded><![CDATA[<p>Two things&#8230;</p>
<p>REPLACE is completely different from the INSERT &#8230; ON DUPLICATE KEY UPDATE &#8230; because replace removes and replaces rows complete whereas the no dup key update construct allows you to update values in the existing row.</p>
<p>If I remember correctly, MySQL will actually re-use the index slot, if possible, so it&#8217;s not as inefficient as you might think. In any case, the alternative equivalent to REPLACE is DELETE+INSERT, which is definitely no faster and quite likely slower.</p>
<p>By the way, REPLACE can actually delete more than one row, this can happen if you also have other UNIQUE constraints. Again, this can be a very useful feature, when used appropriately.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
