<?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: 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>
	<lastBuildDate>Sat, 21 Nov 2009 05:23:57 -0800</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Artis</title>
		<link>http://www.mysqlperformanceblog.com/2006/05/29/insert-on-duplicate-key-update-and-summary-counters/comment-page-1/#comment-678705</link>
		<dc:creator>Artis</dc:creator>
		<pubDate>Tue, 17 Nov 2009 14:32:51 +0000</pubDate>
		<guid isPermaLink="false">http://www.mysqlperformanceblog.com/2006/05/29/insert-on-duplicate-key-update-and-summary-counters/#comment-678705</guid>
		<description>If I am collecting banner display statistics for each day, then I have table `ad_stat` with fields:
id, banner_id, date, views
with (banner_id, date) as unique

I use insert into ad_stat (banner_id, date, views) values ($banner_id, &#039;$date&#039;, 1) ON DUPLICATE KEY UPDATE views=views+1

So this query firstly tries to insert and only then to Update - is there opposite way like:
UPDATE ... ON NO ROWS INSERT ???
Because there are 5 inserts daily and 50 000 updates - wouldn&#039;t it somehow increase performance?</description>
		<content:encoded><![CDATA[<p>If I am collecting banner display statistics for each day, then I have table `ad_stat` with fields:<br />
id, banner_id, date, views<br />
with (banner_id, date) as unique</p>
<p>I use insert into ad_stat (banner_id, date, views) values ($banner_id, &#8216;$date&#8217;, 1) ON DUPLICATE KEY UPDATE views=views+1</p>
<p>So this query firstly tries to insert and only then to Update &#8211; is there opposite way like:<br />
UPDATE &#8230; ON NO ROWS INSERT ???<br />
Because there are 5 inserts daily and 50 000 updates &#8211; wouldn&#8217;t it somehow increase performance?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Baron Schwartz</title>
		<link>http://www.mysqlperformanceblog.com/2006/05/29/insert-on-duplicate-key-update-and-summary-counters/comment-page-1/#comment-575505</link>
		<dc:creator>Baron Schwartz</dc:creator>
		<pubDate>Fri, 05 Jun 2009 14:04:47 +0000</pubDate>
		<guid isPermaLink="false">http://www.mysqlperformanceblog.com/2006/05/29/insert-on-duplicate-key-update-and-summary-counters/#comment-575505</guid>
		<description>VALUES(fld2) should go there.</description>
		<content:encoded><![CDATA[<p>VALUES(fld2) should go there.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jesse</title>
		<link>http://www.mysqlperformanceblog.com/2006/05/29/insert-on-duplicate-key-update-and-summary-counters/comment-page-1/#comment-575381</link>
		<dc:creator>Jesse</dc:creator>
		<pubDate>Fri, 05 Jun 2009 11:43:56 +0000</pubDate>
		<guid isPermaLink="false">http://www.mysqlperformanceblog.com/2006/05/29/insert-on-duplicate-key-update-and-summary-counters/#comment-575381</guid>
		<description>Hello,

I have a dilema with INSERT (fld1, fld2, fld3) VALUES(&#039;1&#039;,&#039;1a&#039;,&#039;1b&#039;),(&#039;2&#039;,&#039;2a&#039;,&#039;2b&#039;),(&#039;3&#039;,&#039;3a&#039;,&#039;3b&#039;) ON DUPLICATE KEY UPDATE fld2 = ??? ( I want this to be the value that would be inserted ie &#039;2a&#039;)  (fld1 is a unique index) 

What should go in the ??? spot?

Thanks.</description>
		<content:encoded><![CDATA[<p>Hello,</p>
<p>I have a dilema with INSERT (fld1, fld2, fld3) VALUES(&#8217;1&#8242;,&#8217;1a&#8217;,'1b&#8217;),(&#8217;2&#8242;,&#8217;2a&#8217;,'2b&#8217;),(&#8217;3&#8242;,&#8217;3a&#8217;,'3b&#8217;) ON DUPLICATE KEY UPDATE fld2 = ??? ( I want this to be the value that would be inserted ie &#8216;2a&#8217;)  (fld1 is a unique index) </p>
<p>What should go in the ??? spot?</p>
<p>Thanks.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Zdeněk Večeřa</title>
		<link>http://www.mysqlperformanceblog.com/2006/05/29/insert-on-duplicate-key-update-and-summary-counters/comment-page-1/#comment-556805</link>
		<dc:creator>Zdeněk Večeřa</dc:creator>
		<pubDate>Wed, 06 May 2009 11:45:22 +0000</pubDate>
		<guid isPermaLink="false">http://www.mysqlperformanceblog.com/2006/05/29/insert-on-duplicate-key-update-and-summary-counters/#comment-556805</guid>
		<description>28. Sergio:

Efficiency of version 1 and 2:
http://blog.zdenekvecera.cz/item/mysql-pokud-zaznam-neexistuje-vytvor-novy-jinak-puvodni-aktualizuj</description>
		<content:encoded><![CDATA[<p>28. Sergio:</p>
<p>Efficiency of version 1 and 2:<br />
<a href="http://blog.zdenekvecera.cz/item/mysql-pokud-zaznam-neexistuje-vytvor-novy-jinak-puvodni-aktualizuj" rel="nofollow">http://blog.zdenekvecera.cz/item/mysql-pokud-zaznam-neexistuje-vytvor-novy-jinak-puvodni-aktualizuj</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: zhilly</title>
		<link>http://www.mysqlperformanceblog.com/2006/05/29/insert-on-duplicate-key-update-and-summary-counters/comment-page-1/#comment-388128</link>
		<dc:creator>zhilly</dc:creator>
		<pubDate>Fri, 21 Nov 2008 03:15:09 +0000</pubDate>
		<guid isPermaLink="false">http://www.mysqlperformanceblog.com/2006/05/29/insert-on-duplicate-key-update-and-summary-counters/#comment-388128</guid>
		<description>i sinc i add duplicate key the insert not work
that not work now
someone know my little erreur?   

 //create and execute the query
    $sql = &quot;INSERT INTO yah_unlimited (data_key,data_value,agent_id,data_group,access_time) VALUES (&#039;$key&#039;, &#039;$value&#039;, &#039;$group&#039;, &#039;$owner_id&#039;, NOW()) ON DUPLICATE KEY UPDATE access_time = NOW()&quot;;
    $result = mysql_query($sql) or die(mysql_error());
    echo &#039;Store successful.&#039;;</description>
		<content:encoded><![CDATA[<p>i sinc i add duplicate key the insert not work<br />
that not work now<br />
someone know my little erreur?   </p>
<p> //create and execute the query<br />
    $sql = &#8220;INSERT INTO yah_unlimited (data_key,data_value,agent_id,data_group,access_time) VALUES (&#8217;$key&#8217;, &#8216;$value&#8217;, &#8216;$group&#8217;, &#8216;$owner_id&#8217;, NOW()) ON DUPLICATE KEY UPDATE access_time = NOW()&#8221;;<br />
    $result = mysql_query($sql) or die(mysql_error());<br />
    echo &#8216;Store successful.&#8217;;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: pravin</title>
		<link>http://www.mysqlperformanceblog.com/2006/05/29/insert-on-duplicate-key-update-and-summary-counters/comment-page-1/#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&#039;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&#039;s working fine if i won&#039;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-page-1/#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 &#8211; 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  &#8211; 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-page-1/#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 =&gt; 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&#039; 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 (&#039;a321587721fdb3a86a49d761fb4b1a2e&#039;, &#039;&#039;, &#039;1216230846&#039;, &#039;1216230846&#039;, &#039;127.0.0.2&#039;, &#039;main&#039;, &#039;0&#039;, &#039;Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9) Gecko/2008052906 Firefox/3.0&#039;, &#039;html&#039;)

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&#039;ve tried all versions on my public site too.. ). localhost, I just refresh the page multiple times &amp; read the smallest generation time.

Maybe I don&#039;t feel any difference because the tables are small (4,870 rows, 1.4MB &amp; 20-60 rows, &lt; 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-page-1/#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-page-1/#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&#039;ve tried this but I didn&#039;t notice any performance improvement.

The UPDATE rate is higher than the INSERT rate.

So.. peter, from your experience, what&#039;s the FASTEST WAY from the following:

1) This method I&#039;ve been using until found out about this article. First, we check if there is a record.. then.. see there.

mysql_query(&quot;SELECT COUNT(*) as count FROM table WHERE user_id = &#039;1&#039;&quot;);
$cnt = mysql_fetch_array();
$cnt = $cnt[&#039;count&#039;];

if ($cnt == 0) {
		mysql_query(&quot;INSERT INTO table VALUES (&#039;1&#039;, &#039;1&#039;)&quot;);
		}
else {
	mysql_query(&quot;UPDATE table SET user_hits = user_hits+1 WHERE user_id = &#039;1&#039;&quot;);
	}


2) With inspiration from Peter Romianowski&#039;s post. Haven&#039;t tested, but I guess I&#039;ve wrote the code ok.

mysql_query(&quot;UPDATE table SET user_hits = user_hits+1 WHERE user_id = &#039;1&#039;&quot;);
$affected_rows = mysql_affected_rows();

if ($affected_rows == 0) mysql_query(&quot;INSERT INTO table VALUES (&#039;1&#039;, &#039;1&#039;)&quot;);


3) Using INSERT .. ON duplicate KEY UPDATE ..

mysql_query(&quot;INSERT INTO table VALUES (&#039;1&#039;, &#039;1&#039;) ON duplicate KEY UPDATE user_hits = user_hits+1 &quot;);



So.. which one is recommended? I repeat.. my update rate is higher than the insert rate. I guess method 2. But I&#039;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>
</channel>
</rss>
