<?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>Wed, 01 Feb 2012 20:47:51 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
	<item>
		<title>By: rsandwick3</title>
		<link>http://code.openark.org/blog/mysql/replace-into-think-twice/comment-page-1#comment-62082</link>
		<dc:creator>rsandwick3</dc:creator>
		<pubDate>Wed, 07 Dec 2011 21:22:40 +0000</pubDate>
		<guid isPermaLink="false">http://code.openark.org/blog/?p=397#comment-62082</guid>
		<description>One of the most useful features of ON DUPLICATE KEY UPDATE is that not all columns need to be specified in the UPDATE clause.

This allows adding new rows which either are fully qualified or use some or all column defaults while only modifying the desired subset of columns when the key is already present.

REPLACE INTO fails in this respect -- once the row is deleted, only the values in the new definition will be populated, so you&#039;d better have somehow managed to define everything you don&#039;t want unset/defaulted for existing rows, in which case you&#039;re basically reinserting the entire table anyway.

This could well be another source of the data loss @Tony happened upon.</description>
		<content:encoded><![CDATA[<p>One of the most useful features of ON DUPLICATE KEY UPDATE is that not all columns need to be specified in the UPDATE clause.</p>
<p>This allows adding new rows which either are fully qualified or use some or all column defaults while only modifying the desired subset of columns when the key is already present.</p>
<p>REPLACE INTO fails in this respect -- once the row is deleted, only the values in the new definition will be populated, so you'd better have somehow managed to define everything you don't want unset/defaulted for existing rows, in which case you're basically reinserting the entire table anyway.</p>
<p>This could well be another source of the data loss @Tony happened upon.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: shlomi</title>
		<link>http://code.openark.org/blog/mysql/replace-into-think-twice/comment-page-1#comment-57641</link>
		<dc:creator>shlomi</dc:creator>
		<pubDate>Fri, 04 Nov 2011 18:03:09 +0000</pubDate>
		<guid isPermaLink="false">http://code.openark.org/blog/?p=397#comment-57641</guid>
		<description>@Tony,
Thanks for sharing this story. You are right about the overhead of foreign keys. This is to some extent due to the indexes on which the foreign keys rely, and, well, the deletion of child tables with &quot;random loss of data&quot;...</description>
		<content:encoded><![CDATA[<p>@Tony,<br />
Thanks for sharing this story. You are right about the overhead of foreign keys. This is to some extent due to the indexes on which the foreign keys rely, and, well, the deletion of child tables with "random loss of data"...</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Tony</title>
		<link>http://code.openark.org/blog/mysql/replace-into-think-twice/comment-page-1#comment-57626</link>
		<dc:creator>Tony</dc:creator>
		<pubDate>Fri, 04 Nov 2011 14:53:42 +0000</pubDate>
		<guid isPermaLink="false">http://code.openark.org/blog/?p=397#comment-57626</guid>
		<description>Newly onboard as the lead developer for my company, I have already run into this issue BIGTIME.

The previous developer(s) were quite fond of the &quot;REPLACE INTO&quot; syntax; the company&#039;s web app was littered with it. Many problems plagued the system, the most common was the &quot;random&quot; loss of data.

As it turned out, the database has been in use for many years; some tables holding more than 1M records, as well as keyed with composites. Inevitably, someone would complain to the mother company&#039;s I.T. dept about the app being slow. Mother company&#039;s I.T. would just simply restart the MySQL instance in order to get performance back up. This occurred many times per day.

Of course, the sluggishness was due to the fact that the db engine was rebuilding the indexes for large tables. Restarting the server simply resulted in nightmarish situations.

Long story short: a good db programmer SHOULD understand the semantics of &quot;REPLACE INTO&quot; and carefully consider whether it is necessary.

One thing that was not mentioned in this article was the additional overhead of foreign-key relationships and how those tables are impacted.</description>
		<content:encoded><![CDATA[<p>Newly onboard as the lead developer for my company, I have already run into this issue BIGTIME.</p>
<p>The previous developer(s) were quite fond of the "REPLACE INTO" syntax; the company's web app was littered with it. Many problems plagued the system, the most common was the "random" loss of data.</p>
<p>As it turned out, the database has been in use for many years; some tables holding more than 1M records, as well as keyed with composites. Inevitably, someone would complain to the mother company's I.T. dept about the app being slow. Mother company's I.T. would just simply restart the MySQL instance in order to get performance back up. This occurred many times per day.</p>
<p>Of course, the sluggishness was due to the fact that the db engine was rebuilding the indexes for large tables. Restarting the server simply resulted in nightmarish situations.</p>
<p>Long story short: a good db programmer SHOULD understand the semantics of "REPLACE INTO" and carefully consider whether it is necessary.</p>
<p>One thing that was not mentioned in this article was the additional overhead of foreign-key relationships and how those tables are impacted.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Renowned Media</title>
		<link>http://code.openark.org/blog/mysql/replace-into-think-twice/comment-page-1#comment-31911</link>
		<dc:creator>Renowned Media</dc:creator>
		<pubDate>Fri, 25 Feb 2011 18:08:37 +0000</pubDate>
		<guid isPermaLink="false">http://code.openark.org/blog/?p=397#comment-31911</guid>
		<description>I vote for MySQL implementing this UPSERT command... There are a lot of things in MySQL which don&#039;t follow the ANSI SQL standard!</description>
		<content:encoded><![CDATA[<p>I vote for MySQL implementing this UPSERT command... There are a lot of things in MySQL which don't follow the ANSI SQL standard!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: shlomi</title>
		<link>http://code.openark.org/blog/mysql/replace-into-think-twice/comment-page-1#comment-29171</link>
		<dc:creator>shlomi</dc:creator>
		<pubDate>Sun, 30 Jan 2011 06:29:11 +0000</pubDate>
		<guid isPermaLink="false">http://code.openark.org/blog/?p=397#comment-29171</guid>
		<description>@tandu,
see Arjen&#039;s comment #1 on REPLACE deleting more than one row when more than one contraint matches.

Anyway I wouldn&#039;t go as far as to fear using wither REPLACE or ON DUPLICATE KEY. It&#039;s like copy_pasting the entire tabel for backup befroe doing a DELETE. You should have perfect understanding on how both work, and based on that decide whether you want to use them...</description>
		<content:encoded><![CDATA[<p>@tandu,<br />
see Arjen's comment #1 on REPLACE deleting more than one row when more than one contraint matches.</p>
<p>Anyway I wouldn't go as far as to fear using wither REPLACE or ON DUPLICATE KEY. It's like copy_pasting the entire tabel for backup befroe doing a DELETE. You should have perfect understanding on how both work, and based on that decide whether you want to use them...</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: tandu</title>
		<link>http://code.openark.org/blog/mysql/replace-into-think-twice/comment-page-1#comment-29136</link>
		<dc:creator>tandu</dc:creator>
		<pubDate>Sun, 30 Jan 2011 00:39:15 +0000</pubDate>
		<guid isPermaLink="false">http://code.openark.org/blog/?p=397#comment-29136</guid>
		<description>Nice article.  REPLACE INTO is dangerous, as it can actually cause you to lose data unpredictably.  This has happened to me before.  Didn&#039;t know about INSERT INTO .. ON DUPLICATE KEY UPDATE before this article.  Seems okay, but I don&#039;t know.  I like to update and then do an insert if no rows were affected.  Silly, but not dangerous.</description>
		<content:encoded><![CDATA[<p>Nice article.  REPLACE INTO is dangerous, as it can actually cause you to lose data unpredictably.  This has happened to me before.  Didn't know about INSERT INTO .. ON DUPLICATE KEY UPDATE before this article.  Seems okay, but I don't know.  I like to update and then do an insert if no rows were affected.  Silly, but not dangerous.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jeremy</title>
		<link>http://code.openark.org/blog/mysql/replace-into-think-twice/comment-page-1#comment-23536</link>
		<dc:creator>Jeremy</dc:creator>
		<pubDate>Thu, 16 Dec 2010 04:37:43 +0000</pubDate>
		<guid isPermaLink="false">http://code.openark.org/blog/?p=397#comment-23536</guid>
		<description>@Z,

I know about the val1=VALUES(val1), val2=VALUES(val2), val3=VALUES(val3), val4=VALUES(val4); option, however this increases the size of the SQL dramatically.  It&#039;s not uncommon for there to be tables with 100+ columns, updating all of those columns would require my application to generate SQL statements that are many times longer, which slows down the query generation, and increases the time it takes to parse the query on the other end.  It&#039;s not necessarily extra &#039;typing&#039;, but it&#039;s just inefficient all the way around, especially when you need to update many rows at a time.</description>
		<content:encoded><![CDATA[<p>@Z,</p>
<p>I know about the val1=VALUES(val1), val2=VALUES(val2), val3=VALUES(val3), val4=VALUES(val4); option, however this increases the size of the SQL dramatically.  It's not uncommon for there to be tables with 100+ columns, updating all of those columns would require my application to generate SQL statements that are many times longer, which slows down the query generation, and increases the time it takes to parse the query on the other end.  It's not necessarily extra 'typing', but it's just inefficient all the way around, especially when you need to update many rows at a time.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Some Dude</title>
		<link>http://code.openark.org/blog/mysql/replace-into-think-twice/comment-page-1#comment-23503</link>
		<dc:creator>Some Dude</dc:creator>
		<pubDate>Wed, 15 Dec 2010 20:48:17 +0000</pubDate>
		<guid isPermaLink="false">http://code.openark.org/blog/?p=397#comment-23503</guid>
		<description>@Z

How is that any better? It&#039;s still a lot of extra typing.</description>
		<content:encoded><![CDATA[<p>@Z</p>
<p>How is that any better? It's still a lot of extra typing.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Z</title>
		<link>http://code.openark.org/blog/mysql/replace-into-think-twice/comment-page-1#comment-22568</link>
		<dc:creator>Z</dc:creator>
		<pubDate>Wed, 08 Dec 2010 14:49:20 +0000</pubDate>
		<guid isPermaLink="false">http://code.openark.org/blog/?p=397#comment-22568</guid>
		<description>Jeremy: You can easy reuse values in the UPDATE part from the INSERT.

INSERT INTO objects (val1, val2, val3, val4) VALUES (1, 2, 3, 4) ON DUPLICATE KEY UPDATE val1=VALUES(val1), val2=VALUES(val2), val3=VALUES(val3), val4=VALUES(val4);

http://dev.mysql.com/doc/refman/5.1/en/miscellaneous-functions.html#function_values</description>
		<content:encoded><![CDATA[<p>Jeremy: You can easy reuse values in the UPDATE part from the INSERT.</p>
<p>INSERT INTO objects (val1, val2, val3, val4) VALUES (1, 2, 3, 4) ON DUPLICATE KEY UPDATE val1=VALUES(val1), val2=VALUES(val2), val3=VALUES(val3), val4=VALUES(val4);</p>
<p><a href="http://dev.mysql.com/doc/refman/5.1/en/miscellaneous-functions.html#function_values" rel="nofollow">http://dev.mysql.com/doc/refman/5.1/en/miscellaneous-functions.html#function_values</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: shlomi</title>
		<link>http://code.openark.org/blog/mysql/replace-into-think-twice/comment-page-1#comment-20118</link>
		<dc:creator>shlomi</dc:creator>
		<pubDate>Tue, 02 Nov 2010 03:57:20 +0000</pubDate>
		<guid isPermaLink="false">http://code.openark.org/blog/?p=397#comment-20118</guid>
		<description>@Jeremy,
I think you&#039;re looking for what&#039;s &quot;known&quot; as the &lt;a href=&quot;http://en.wikipedia.org/wiki/Upsert&quot; rel=&quot;nofollow&quot;&gt;UPSERT&lt;/a&gt; statement. There isn&#039;t one.
Really, the SQL syntax, as defined by ANSI SQL, is far from being friendly.</description>
		<content:encoded><![CDATA[<p>@Jeremy,<br />
I think you're looking for what's "known" as the <a href="http://en.wikipedia.org/wiki/Upsert" rel="nofollow">UPSERT</a> statement. There isn't one.<br />
Really, the SQL syntax, as defined by ANSI SQL, is far from being friendly.</p>
]]></content:encoded>
	</item>
</channel>
</rss>

