[ZZ]INSERT ON DUPLICATE KEY UPDATE and summary counters
2009-01-05 15:37
555 查看
http://www.mysqlperformanceblog.com/2006/05/29/insert-on-duplicate-key-update-and-summary-counters/
INSERT
... ON DUPLICATE KEY UPDATE is very powerful but often forgotten MySQL
feature. It was introduced in MySQL 4.1 but I still constantly see
people unaware of it.
Myself I like this feature big deal because it is designed in truly
MySQL style - very efficient solution for freqent task while keeping it
beautiful and easy to use.
So what is this feature great for ? Well any kind of maintaining
counters. If you're writing traffic accounting it could be traffic and
number of packet passed for given port or IP address. For Web
Applications it could be counting number of visits per page or IP
address, number of times particular keyword was searched etc.
This functionality also makes it very easy to do incremental single pass log file processing and building summary tables.
Here is example:
PLAIN TEXT
SQL:
CREATE TABLE ipstat(ip int UNSIGNED NOT NULL PRIMARY KEY,
hits int UNSIGNED NOT NULL,
last_hit timestamp);
INSERT INTO ipstat VALUES(inet_aton('192.168.0.1'),1,now())
ON duplicate KEY UPDATE hits=hits+1;
This example actually shows one more neat feature of MySQL - inet_aton and inet_ntoa
functions which can convert IP address strings to integers and back.
This allows to save on field length significantly by using 4 bytes
instead of 15
The third feature this example takes advantage of is TIMESTAMP field.
By default first TIMESTAMP column will have its value automatically
updated to current timestamp on insert and update. We actually could
have omitted now() in insert clause but this would require to specify list of columns which we skipped for sake of example.
So how would this example work ? Well just you would expect it. If
there is no such IP address in the table it will be added with hits=1
if it is already where (note ip is PRIMARY KEY) it would be just incremented and last visit timestamp updated.
The benefit of using this feature insted of INSERT + UPDATE
could be different, depending on number of new rows and data set size.
30% speedup should be typical. Performance increase is not the only
benefit - what is even more important the application code becomes
simplier - less error prone and easy to read.
INSERT ON DUPLICATE KEY UPDATE and summary counters.
Posted by peterINSERT
... ON DUPLICATE KEY UPDATE is very powerful but often forgotten MySQL
feature. It was introduced in MySQL 4.1 but I still constantly see
people unaware of it.
Myself I like this feature big deal because it is designed in truly
MySQL style - very efficient solution for freqent task while keeping it
beautiful and easy to use.
So what is this feature great for ? Well any kind of maintaining
counters. If you're writing traffic accounting it could be traffic and
number of packet passed for given port or IP address. For Web
Applications it could be counting number of visits per page or IP
address, number of times particular keyword was searched etc.
This functionality also makes it very easy to do incremental single pass log file processing and building summary tables.
Here is example:
PLAIN TEXT
SQL:
CREATE TABLE ipstat(ip int UNSIGNED NOT NULL PRIMARY KEY,
hits int UNSIGNED NOT NULL,
last_hit timestamp);
INSERT INTO ipstat VALUES(inet_aton('192.168.0.1'),1,now())
ON duplicate KEY UPDATE hits=hits+1;
This example actually shows one more neat feature of MySQL - inet_aton and inet_ntoa
functions which can convert IP address strings to integers and back.
This allows to save on field length significantly by using 4 bytes
instead of 15
The third feature this example takes advantage of is TIMESTAMP field.
By default first TIMESTAMP column will have its value automatically
updated to current timestamp on insert and update. We actually could
have omitted now() in insert clause but this would require to specify list of columns which we skipped for sake of example.
So how would this example work ? Well just you would expect it. If
there is no such IP address in the table it will be added with hits=1
if it is already where (note ip is PRIMARY KEY) it would be just incremented and last visit timestamp updated.
The benefit of using this feature insted of INSERT + UPDATE
could be different, depending on number of new rows and data set size.
30% speedup should be typical. Performance increase is not the only
benefit - what is even more important the application code becomes
simplier - less error prone and easy to read.
相关文章推荐
- Insert into ..... on duplicate key update ...与Replace into的区别
- MySql中4种批量更新的方法update table2,table1,批量更新用insert into ...on duplicate key update, 慎用replace into.
- mysql中INSERT INTO… ON DUPLICATE KEY UPDATE用法
- INSERT ... ON DUPLICATE KEY UPDATE
- mysql 中 replace into 与 insert into on duplicate key update 的使用和不同点
- mysql insert into on duplicate key update timestamp类型 (近期遇到问题)
- INSERT ... ON DUPLICATE KEY UPDATE Syntax
- MySQL中insert ignore into, on duplicate key update,replace into,insert … select … where not exist的一些用法总结
- [MySQL技巧]INSERT … ON DUPLICATE KEY UPDATE(转)
- [mysql 语法]INSERT ... ON DUPLICATE KEY UPDATE
- insert into on DUPLICATE KEY UPDATE使用
- Mysql中Insert into xxx on duplicate key update问题
- INSERT ... ON DUPLICATE KEY UPDATE
- INSERT ... ON DUPLICATE KEY UPDATE
- Mysql中Insert into xxx on duplicate key update问题
- 转-mysql insert的几点操作(DELAYED 、IGNORE、ON DUPLICATE KEY UPDATE )
- MySQL的Replace into 与Insert into on duplicate key update真正的不同之处
- Mysql中Insert into xxx on duplicate key update和REPLACE INTO使用
- INSERT … ON DUPLICATE KEY UPDATE 小析
- INSERT ... ON DUPLICATE KEY UPDATE Syntax