mysql优化学习on DUPLICATE key Update

场景:sql

有张表,里面的记录不能存在重复记录,记录存在就更新,若是不存在就插入并发

传统作法:性能

先查询select,若是存在就update,不存在就insert,这样就存大两条sql语句了。对于大并发来讲,存在性能问题。spa

忧化方法:以下code

建立表,注意要有一个惟一索引 new_code_index, 插入或者更新时,以此为标准blog

CREATE TABLE `news_test` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `new_title` varchar(255) DEFAULT NULL,
  `new_abstr` varchar(255) DEFAULT NULL,
  `new_code` varchar(255) DEFAULT NULL,
  `update_time` datetime DEFAULT NULL,
  `create_time` datetime DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `new_code_index` (`new_code`) USING BTREE
) ENGINE=MyISAM AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;

sql语句索引

insert into news_test(new_title, new_abstr, new_code, update_time, create_time) values('马刺','湖人',MD5(CONCAT('马刺','湖人')), NOW(), NOW()) on DUPLICATE key Update update_time=now(), create_time=now();

第一次执行it

第二次执行class

相关文章
相关标签/搜索