<?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"
	>
<channel>
	<title>Comments on: INSERT ON DUPLICATE KEY UPDATE   and summary counters.</title>
	<atom:link href="http://www.mysqlperformanceblog.com/2006/05/29/insert-on-duplicate-key-update-and-summary-counters/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.mysqlperformanceblog.com/2006/05/29/insert-on-duplicate-key-update-and-summary-counters/</link>
	<description>Everything about MySQL Performance</description>
	<pubDate>Tue, 14 Oct 2008 06:57:35 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.5.1</generator>
		<item>
		<title>By: pravin</title>
		<link>http://www.mysqlperformanceblog.com/2006/05/29/insert-on-duplicate-key-update-and-summary-counters/#comment-339929</link>
		<dc:creator>pravin</dc:creator>
		<pubDate>Mon, 04 Aug 2008 08:45:58 +0000</pubDate>
		<guid isPermaLink="false">http://www.mysqlperformanceblog.com/2006/05/29/insert-on-duplicate-key-update-and-summary-counters/#comment-339929</guid>
		<description>I have a query in which I'm checking whether LAN(my unique key) is duplicate or not,if it duplicate then update the record else insert that LAN No.

I did it in the following way.

insert into romappingfinal(LAN,romappingfinalid,branch_code,
RO_CODE,main_ro_code,loan_end_date,ro_status,main_ro_status,
ro_transfer_status,main_ro_transfer_status,ro_name,main_ro_name)
select LAN,romappingstagingid as romappingfinalid,branch_code,
RO_CODE,main_ro_code,loan_end_date,ro_status,main_ro_status,
ro_transfer_status,main_ro_transfer_status,ro_name,main_ro_name
from viewromappingfinal
on duplicate key update LAN=values(LAN)

it's working fine if i won't use romappingfinalid(table primary key).

Suggest me how to use the INSERT ON UPDATE KEY using a table primary key but checking the condition on another primary key(LAN).</description>
		<content:encoded><![CDATA[<p>I have a query in which I&#8217;m checking whether LAN(my unique key) is duplicate or not,if it duplicate then update the record else insert that LAN No.</p>
<p>I did it in the following way.</p>
<p>insert into romappingfinal(LAN,romappingfinalid,branch_code,<br />
RO_CODE,main_ro_code,loan_end_date,ro_status,main_ro_status,<br />
ro_transfer_status,main_ro_transfer_status,ro_name,main_ro_name)<br />
select LAN,romappingstagingid as romappingfinalid,branch_code,<br />
RO_CODE,main_ro_code,loan_end_date,ro_status,main_ro_status,<br />
ro_transfer_status,main_ro_transfer_status,ro_name,main_ro_name<br />
from viewromappingfinal<br />
on duplicate key update LAN=values(LAN)</p>
<p>it&#8217;s working fine if i won&#8217;t use romappingfinalid(table primary key).</p>
<p>Suggest me how to use the INSERT ON UPDATE KEY using a table primary key but checking the condition on another primary key(LAN).</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: peter</title>
		<link>http://www.mysqlperformanceblog.com/2006/05/29/insert-on-duplicate-key-update-and-summary-counters/#comment-331980</link>
		<dc:creator>peter</dc:creator>
		<pubDate>Sun, 20 Jul 2008 16:14:42 +0000</pubDate>
		<guid isPermaLink="false">http://www.mysqlperformanceblog.com/2006/05/29/insert-on-duplicate-key-update-and-summary-counters/#comment-331980</guid>
		<description>Sergio,

This is why it is always mentioned in cases  count=count+1 and similar - in this case you always change the data.  Really MySQL returns both rows matched and rows updated and it is rows matched you need to wants.

Generally Insert on dup key update is the method which was designed to make it most optimal though the question if it always is remains open :)

Also note the difference in this cases comes from amount of queries you have to run mainly  - run benchmarks over network to see the difference better.</description>
		<content:encoded><![CDATA[<p>Sergio,</p>
<p>This is why it is always mentioned in cases  count=count+1 and similar - in this case you always change the data.  Really MySQL returns both rows matched and rows updated and it is rows matched you need to wants.</p>
<p>Generally Insert on dup key update is the method which was designed to make it most optimal though the question if it always is remains open <img src='http://www.mysqlperformanceblog.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Also note the difference in this cases comes from amount of queries you have to run mainly  - run benchmarks over network to see the difference better.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Sergio</title>
		<link>http://www.mysqlperformanceblog.com/2006/05/29/insert-on-duplicate-key-update-and-summary-counters/#comment-329674</link>
		<dc:creator>Sergio</dc:creator>
		<pubDate>Wed, 16 Jul 2008 18:11:43 +0000</pubDate>
		<guid isPermaLink="false">http://www.mysqlperformanceblog.com/2006/05/29/insert-on-duplicate-key-update-and-summary-counters/#comment-329674</guid>
		<description>Thanks for reply Peter.

However, version 2 has a small issue.
If you try to update a record with the same data as in table, mysql ignore the query (affected rows =&#62; 0), and insert query is executed, even if the record exist in table.

For example, if I refresh twice the page the following query will be the same (if I refresh the page in the same SECOND), so affected rows = 0, thus INSERT INTO query is executed and an error will occur (duplicate entry cos' of the primary index) 

INSERT INTO forum_sessions (session_id, session_user_id, session_start, session_time, session_ip, session_page, session_logged_in, session_browser, session_mode) VALUES ('a321587721fdb3a86a49d761fb4b1a2e', '', '1216230846', '1216230846', '127.0.0.2', 'main', '0', 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9) Gecko/2008052906 Firefox/3.0', 'html')

Of course errors can be masked for users.. 

Can you provide me some tips for benchmarking? The generation time of the pages is almost the same in all 3 situations (I've tried all versions on my public site too.. ). localhost, I just refresh the page multiple times &#38; read the smallest generation time.

Maybe I don't feel any difference because the tables are small (4,870 rows, 1.4MB &#38; 20-60 rows, &#60; 100KB)</description>
		<content:encoded><![CDATA[<p>Thanks for reply Peter.</p>
<p>However, version 2 has a small issue.<br />
If you try to update a record with the same data as in table, mysql ignore the query (affected rows =&gt; 0), and insert query is executed, even if the record exist in table.</p>
<p>For example, if I refresh twice the page the following query will be the same (if I refresh the page in the same SECOND), so affected rows = 0, thus INSERT INTO query is executed and an error will occur (duplicate entry cos&#8217; of the primary index) </p>
<p>INSERT INTO forum_sessions (session_id, session_user_id, session_start, session_time, session_ip, session_page, session_logged_in, session_browser, session_mode) VALUES (&#8217;a321587721fdb3a86a49d761fb4b1a2e&#8217;, &#8221;, &#8216;1216230846&#8242;, &#8216;1216230846&#8242;, &#8216;127.0.0.2&#8242;, &#8216;main&#8217;, &#8216;0&#8242;, &#8216;Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9) Gecko/2008052906 Firefox/3.0&#8242;, &#8216;html&#8217;)</p>
<p>Of course errors can be masked for users.. </p>
<p>Can you provide me some tips for benchmarking? The generation time of the pages is almost the same in all 3 situations (I&#8217;ve tried all versions on my public site too.. ). localhost, I just refresh the page multiple times &amp; read the smallest generation time.</p>
<p>Maybe I don&#8217;t feel any difference because the tables are small (4,870 rows, 1.4MB &amp; 20-60 rows, &lt; 100KB)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Peter Romianowski</title>
		<link>http://www.mysqlperformanceblog.com/2006/05/29/insert-on-duplicate-key-update-and-summary-counters/#comment-328842</link>
		<dc:creator>Peter Romianowski</dc:creator>
		<pubDate>Tue, 15 Jul 2008 12:19:59 +0000</pubDate>
		<guid isPermaLink="false">http://www.mysqlperformanceblog.com/2006/05/29/insert-on-duplicate-key-update-and-summary-counters/#comment-328842</guid>
		<description>Sergio, I would recommend version 2 for high update rates. But I would also recommend to benchmark your application in high concurrency environment. ;)

Cheers

Peter</description>
		<content:encoded><![CDATA[<p>Sergio, I would recommend version 2 for high update rates. But I would also recommend to benchmark your application in high concurrency environment. <img src='http://www.mysqlperformanceblog.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p>Cheers</p>
<p>Peter</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Sergio</title>
		<link>http://www.mysqlperformanceblog.com/2006/05/29/insert-on-duplicate-key-update-and-summary-counters/#comment-328794</link>
		<dc:creator>Sergio</dc:creator>
		<pubDate>Tue, 15 Jul 2008 09:43:19 +0000</pubDate>
		<guid isPermaLink="false">http://www.mysqlperformanceblog.com/2006/05/29/insert-on-duplicate-key-update-and-summary-counters/#comment-328794</guid>
		<description>I've tried this but I didn't notice any performance improvement.

The UPDATE rate is higher than the INSERT rate.

So.. peter, from your experience, what's the FASTEST WAY from the following:

1) This method I've been using until found out about this article. First, we check if there is a record.. then.. see there.

mysql_query("SELECT COUNT(*) as count FROM table WHERE user_id = '1'");
$cnt = mysql_fetch_array();
$cnt = $cnt['count'];

if ($cnt == 0) {
		mysql_query("INSERT INTO table VALUES ('1', '1')");
		}
else {
	mysql_query("UPDATE table SET user_hits = user_hits+1 WHERE user_id = '1'");
	}


2) With inspiration from Peter Romianowski's post. Haven't tested, but I guess I've wrote the code ok.

mysql_query("UPDATE table SET user_hits = user_hits+1 WHERE user_id = '1'");
$affected_rows = mysql_affected_rows();

if ($affected_rows == 0) mysql_query("INSERT INTO table VALUES ('1', '1')");


3) Using INSERT .. ON duplicate KEY UPDATE ..

mysql_query("INSERT INTO table VALUES ('1', '1') ON duplicate KEY UPDATE user_hits = user_hits+1 ");



So.. which one is recommended? I repeat.. my update rate is higher than the insert rate. I guess method 2. But I'm waiting other opinions.
Thanks.</description>
		<content:encoded><![CDATA[<p>I&#8217;ve tried this but I didn&#8217;t notice any performance improvement.</p>
<p>The UPDATE rate is higher than the INSERT rate.</p>
<p>So.. peter, from your experience, what&#8217;s the FASTEST WAY from the following:</p>
<p>1) This method I&#8217;ve been using until found out about this article. First, we check if there is a record.. then.. see there.</p>
<p>mysql_query(&#8221;SELECT COUNT(*) as count FROM table WHERE user_id = &#8216;1&#8242;&#8221;);<br />
$cnt = mysql_fetch_array();<br />
$cnt = $cnt['count'];</p>
<p>if ($cnt == 0) {<br />
		mysql_query(&#8221;INSERT INTO table VALUES (&#8217;1&#8242;, &#8216;1&#8242;)&#8221;);<br />
		}<br />
else {<br />
	mysql_query(&#8221;UPDATE table SET user_hits = user_hits+1 WHERE user_id = &#8216;1&#8242;&#8221;);<br />
	}</p>
<p>2) With inspiration from Peter Romianowski&#8217;s post. Haven&#8217;t tested, but I guess I&#8217;ve wrote the code ok.</p>
<p>mysql_query(&#8221;UPDATE table SET user_hits = user_hits+1 WHERE user_id = &#8216;1&#8242;&#8221;);<br />
$affected_rows = mysql_affected_rows();</p>
<p>if ($affected_rows == 0) mysql_query(&#8221;INSERT INTO table VALUES (&#8217;1&#8242;, &#8216;1&#8242;)&#8221;);</p>
<p>3) Using INSERT .. ON duplicate KEY UPDATE ..</p>
<p>mysql_query(&#8221;INSERT INTO table VALUES (&#8217;1&#8242;, &#8216;1&#8242;) ON duplicate KEY UPDATE user_hits = user_hits+1 &#8220;);</p>
<p>So.. which one is recommended? I repeat.. my update rate is higher than the insert rate. I guess method 2. But I&#8217;m waiting other opinions.<br />
Thanks.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: peter</title>
		<link>http://www.mysqlperformanceblog.com/2006/05/29/insert-on-duplicate-key-update-and-summary-counters/#comment-305547</link>
		<dc:creator>peter</dc:creator>
		<pubDate>Tue, 27 May 2008 03:40:13 +0000</pubDate>
		<guid isPermaLink="false">http://www.mysqlperformanceblog.com/2006/05/29/insert-on-duplicate-key-update-and-summary-counters/#comment-305547</guid>
		<description>Dreamluverz,

The best place to ask the question is forums.mysqlperformanceblog.com ; and it is also best to provide exact statement and error message :)</description>
		<content:encoded><![CDATA[<p>Dreamluverz,</p>
<p>The best place to ask the question is forums.mysqlperformanceblog.com ; and it is also best to provide exact statement and error message <img src='http://www.mysqlperformanceblog.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: dreamluverz</title>
		<link>http://www.mysqlperformanceblog.com/2006/05/29/insert-on-duplicate-key-update-and-summary-counters/#comment-305234</link>
		<dc:creator>dreamluverz</dc:creator>
		<pubDate>Mon, 26 May 2008 07:44:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.mysqlperformanceblog.com/2006/05/29/insert-on-duplicate-key-update-and-summary-counters/#comment-305234</guid>
		<description>I'm trying to use this on INSERT SELECT statement and im  getting an error. my sql is something like this

INSERT INTO table SELECT * FROM table WHERE id='2' ON DUPLICATE KEY UPDATE id=LAST_INSERT_ID() + 1
I use LAST_INSERT_ID() coz im using auto increment.</description>
		<content:encoded><![CDATA[<p>I&#8217;m trying to use this on INSERT SELECT statement and im  getting an error. my sql is something like this</p>
<p>INSERT INTO table SELECT * FROM table WHERE id=&#8217;2&#8242; ON DUPLICATE KEY UPDATE id=LAST_INSERT_ID() + 1<br />
I use LAST_INSERT_ID() coz im using auto increment.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: TM</title>
		<link>http://www.mysqlperformanceblog.com/2006/05/29/insert-on-duplicate-key-update-and-summary-counters/#comment-246934</link>
		<dc:creator>TM</dc:creator>
		<pubDate>Thu, 28 Feb 2008 22:57:27 +0000</pubDate>
		<guid isPermaLink="false">http://www.mysqlperformanceblog.com/2006/05/29/insert-on-duplicate-key-update-and-summary-counters/#comment-246934</guid>
		<description>Is there any way i can use a query similar to this but have some kind of where clause at the end? say if i wanted to do a whole bunch of inserts but update them if they exist already and update the primary key also? so for example 
insert into tmmember values ('STRES17N','SK','1725','ESTRES17','N','6','255','-1')
 on duplicate key  update  memb_memberid = 'STRES17N', memb_teamid = 'SK' only the original memb_member id might be 'SKRES17N' so as you can see id like to be able to say where memb_memberid = 'SKRES17N' for the cases when its true.</description>
		<content:encoded><![CDATA[<p>Is there any way i can use a query similar to this but have some kind of where clause at the end? say if i wanted to do a whole bunch of inserts but update them if they exist already and update the primary key also? so for example<br />
insert into tmmember values (&#8217;STRES17N&#8217;,'SK&#8217;,'1725&#8242;,&#8217;ESTRES17&#8242;,&#8217;N',&#8217;6&#8242;,&#8217;255&#8242;,&#8217;-1&#8242;)<br />
 on duplicate key  update  memb_memberid = &#8216;STRES17N&#8217;, memb_teamid = &#8216;SK&#8217; only the original memb_member id might be &#8216;SKRES17N&#8217; so as you can see id like to be able to say where memb_memberid = &#8216;SKRES17N&#8217; for the cases when its true.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: foo</title>
		<link>http://www.mysqlperformanceblog.com/2006/05/29/insert-on-duplicate-key-update-and-summary-counters/#comment-242836</link>
		<dc:creator>foo</dc:creator>
		<pubDate>Fri, 15 Feb 2008 10:30:55 +0000</pubDate>
		<guid isPermaLink="false">http://www.mysqlperformanceblog.com/2006/05/29/insert-on-duplicate-key-update-and-summary-counters/#comment-242836</guid>
		<description>also useful in combination with unique keys :)
see http://textsnippets.com/posts/show/975</description>
		<content:encoded><![CDATA[<p>also useful in combination with unique keys <img src='http://www.mysqlperformanceblog.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /><br />
see <a href="http://textsnippets.com/posts/show/975" rel="nofollow">http://textsnippets.com/posts/show/975</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Raven Riley Nude</title>
		<link>http://www.mysqlperformanceblog.com/2006/05/29/insert-on-duplicate-key-update-and-summary-counters/#comment-232953</link>
		<dc:creator>Raven Riley Nude</dc:creator>
		<pubDate>Wed, 23 Jan 2008 04:17:40 +0000</pubDate>
		<guid isPermaLink="false">http://www.mysqlperformanceblog.com/2006/05/29/insert-on-duplicate-key-update-and-summary-counters/#comment-232953</guid>
		<description>&lt;strong&gt;Raven Riley&lt;/strong&gt;

Raven Riley</description>
		<content:encoded><![CDATA[<p><strong>Raven Riley</strong></p>
<p>Raven Riley</p>
]]></content:encoded>
	</item>
</channel>
</rss>
