我们有一个表格方案,如下所示:
CREATE TABLE IF NOT EXISTS `offers` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`campaign_id` int(11) NOT NULL,
`user_id` int(11) NOT NULL,
`price` double NOT NULL,
`ip` varchar(15) NOT NULL,
`cdate` int(11) NOT NULL,
PRIMARY KEY (`id`),
KEY `campaign_id` (`campaign_id`,`price`)
) ENGINE=InnoDB DEFAULT CHARSET=latin5 AUTO_INCREMENT=190514 ;
对于用户的每个新报价,我们都会检查最后一个订单是否由同一用户给出:
"select user_id from offers where campaign_id='".$campaign['id']."' order by id desc limit 1"
如果 user_id 相同,我们会阻止新提议以保护用户免受意外双击。
如果报价没有任何问题,我们将报价插入:
"insert into offers(campaign_id,user_id,price,ip,cdate) values (".$campaign['id'].",".$user['id'].",'".$price."','".$_SERVER['REMOTE_ADDR']."',".time().")"
但问题是 select 仅在大约 1 秒后返回最后插入的行。这意味着如果用户点击按钮太快,他们可以插入多个优惠。
我们使用 5.5.30-30.2-log Percona Server 作为我们的数据库服务器。以下是我们的 my.cnf 文件:
[mysqld]
datadir = /var/lib/mysql
tmpdir = /var/lib/mysqltmp
socket = /var/lib/mysql/mysql.sock
skip-external-locking = 1
skip-name-resolve
open-files-limit = 40000
max_heap_table_size = 64M
tmp_table_size = 64M
log-error = /var/log/mysqld.log
thread-cache-size = 50
table-cache = 4096
table-open-cache = 4096
table-definition-cache = 512
query-cache-size = 0
query-cache-limit = 16M
query-cache-type = 0
sort-buffer-size = 1M
read-buffer-size = 1M
read-rnd-buffer-size = 8M
join-buffer-size = 1M
tmp-table-size = 64M
max-heap-table-size = 64M
back-log = 100
max-connections = 10000
max-connect-errors = 10000
max-allowed-packet = 256M
interactive-timeout = 360
wait-timeout = 360
innodb = FORCE
key-buffer-size = 32M
myisam-sort-buffer-size = 4M
innodb-buffer-pool-size = 60G
innodb-log-file-size = 256M
innodb_log_files_in_group = 2
innodb-log-buffer-size = 4M
innodb-file-per-table = 1
innodb-thread-concurrency = 8
innodb-flush-log-at-trx-commit =2
server-id = 1
slow-query-log = 1
slow-query-log-file = /var/lib/mysqllogs/slow-log