MySQL:在小桌子上不断“搜索行进行更新”

时间:2016-09-02 09:29:02

标签: mysql innodb amazon-rds

我的MySQL架构中有以下表格:

CREATE TABLE `security_token` (
 `id` int(11) NOT NULL AUTO_INCREMENT,
 `customer_id` int(11) DEFAULT NULL,
 `expiration_date` datetime NOT NULL,
 `token_string` varchar(50) NOT NULL,
 `user_name` varchar(100) NOT NULL,
 PRIMARY KEY (`id`),
 KEY `security_token_token_string` (`token_string`)
) ENGINE=InnoDB AUTO_INCREMENT=7054 DEFAULT CHARSET=latin1;

经常执行以下查询(每分钟多次),执行时间有时超过3分钟:

UPDATE security_token
SET expiration_date = '2016-09-01 18:20:37'
WHERE user_name = 'john.smith' AND token_string = 'mLBgrOXfVsXewCV333dVdvvDdxghJqQRU' AND customer_id = 3

如果我执行SHOW PROCESSLIST状态为:

| 33515563 | db_user | 192.168.0.7:41058 | application_schema | Query | 116 | Searching rows for update | UPDATE security_token SET expiration_date = '2016-09-02 18:12:28' WHERE user_name = 'john.smith' AND ... |
| 33515576 | db_user | 192.168.0.7:41062 | application_schema | Query | 113 | Searching rows for update | UPDATE security_token SET expiration_date = '2016-09-02 18:12:28' WHERE user_name = 'john.smith' AND ... |
| 33515587 | db_user | 192.168.0.7:41064 | application_schema | Query | 110 | Searching rows for update | UPDATE security_token SET expiration_date = '2016-09-02 18:12:29' WHERE user_name = 'john.smith' AND ... |
| 33515609 | db_user | 192.168.0.7:41067 | application_schema | Query | 106 | Searching rows for update | UPDATE security_token SET expiration_date = '2016-09-02 18:12:30' WHERE user_name = 'john.smith' AND ... |
| 33515631 | db_user | 192.168.0.7:41071 | application_schema | Query | 104 | Searching rows for update | UPDATE security_token SET expiration_date = '2016-09-02 18:12:45' WHERE user_name = 'john.smith' AND ... |
| 33515665 | db_user | 192.168.0.7:41076 | application_schema | Query | 101 | Searching rows for update | UPDATE security_token SET expiration_date = '2016-09-02 18:13:12' WHERE user_name = 'john.smith' AND ... |
| 33515708 | db_user | 192.168.0.7:41081 | application_schema | Query | 98  | Searching rows for update | UPDATE security_token SET expiration_date = '2016-09-02 18:13:28' WHERE user_name = 'john.smith' AND ... |
| 33515710 | db_user | 192.168.0.7:41086 | application_schema | Query | 93  | Searching rows for update | UPDATE security_token SET expiration_date = '2016-09-02 18:13:29' WHERE user_name = 'john.smith' AND ... |
| 33515711 | db_user | 192.168.0.7:41090 | application_schema | Query | 88  | Searching rows for update | UPDATE security_token SET expiration_date = '2016-09-02 18:13:36' WHERE user_name = 'john.smith' AND ... |
| 33515712 | db_user | 192.168.0.7:41092 | application_schema | Query | 85  | Searching rows for update | UPDATE security_token SET expiration_date = '2016-09-02 18:13:58' WHERE user_name = 'john.smith' AND ... |

该表只有6k行,大小为1.2MB。整个架构的大小为300MB,目前正在Amazon RDS db.m4.xlarge实例上运行。

我不确定为什么这个查询导致了这么多麻烦,有人能提供任何指向哪里查看/调整?不幸的是,我无法启用性能模式,因为这是一个生产数据库。

使用更多信息进行更新

我正在运行版本5.6.19-log,此(或任何)表上没有触发器。此表的主键未在任何其他表中引用。

我将以下查询列为阻止:

SELECT * FROM INNODB_LOCKS WHERE LOCK_TRX_ID IN (SELECT BLOCKING_TRX_ID FROM INNODB_LOCK_WAITS);
+----------------------+-------------+-----------+-----------+---------------------------------------+--------------------+------------+-----------+----------+-------------------------------------------------------------+
| lock_id              | lock_trx_id | lock_mode | lock_type | lock_table                            | lock_index         | lock_space | lock_page | lock_rec | lock_data                                                   |
+----------------------+-------------+-----------+-----------+---------------------------------------+--------------------+------------+-----------+----------+-------------------------------------------------------------+
| 535259585:349:43:132 | 535259585   | X         | RECORD    | `application_schema`.`security_token` | security_token_idx |        349 |        43 |      132 | 'john.smith', 'mLBgrOXfVsXewCV333dVdvvDdxghJqQRU', 3, 70661 |
| 535259584:349:43:132 | 535259584   | X         | RECORD    | `application_schema`.`security_token` | security_token_idx |        349 |        43 |      132 | 'john.smith', 'mLBgrOXfVsXewCV333dVdvvDdxghJqQRU', 3, 70661 |
| 535259583:349:43:132 | 535259583   | X         | RECORD    | `application_schema`.`security_token` | security_token_idx |        349 |        43 |      132 | 'john.smith', 'mLBgrOXfVsXewCV333dVdvvDdxghJqQRU', 3, 70661 |
| 535259581:349:43:132 | 535259581   | X         | RECORD    | `application_schema`.`security_token` | security_token_idx |        349 |        43 |      132 | 'john.smith', 'mLBgrOXfVsXewCV333dVdvvDdxghJqQRU', 3, 70661 |
| 535259575:349:43:132 | 535259575   | X         | RECORD    | `application_schema`.`security_token` | security_token_idx |        349 |        43 |      132 | 'john.smith', 'mLBgrOXfVsXewCV333dVdvvDdxghJqQRU', 3, 70661 |
| 535259574:349:43:132 | 535259574   | X         | RECORD    | `application_schema`.`security_token` | security_token_idx |        349 |        43 |      132 | 'john.smith', 'mLBgrOXfVsXewCV333dVdvvDdxghJqQRU', 3, 70661 |
| 535259573:349:43:132 | 535259573   | X         | RECORD    | `application_schema`.`security_token` | security_token_idx |        349 |        43 |      132 | 'john.smith', 'mLBgrOXfVsXewCV333dVdvvDdxghJqQRU', 3, 70661 |
| 535259570:349:43:132 | 535259570   | X         | RECORD    | `application_schema`.`security_token` | security_token_idx |        349 |        43 |      132 | 'john.smith', 'mLBgrOXfVsXewCV333dVdvvDdxghJqQRU', 3, 70661 |
| 535259554:349:43:132 | 535259554   | X         | RECORD    | `application_schema`.`security_token` | security_token_idx |        349 |        43 |      132 | 'john.smith', 'mLBgrOXfVsXewCV333dVdvvDdxghJqQRU', 3, 70661 |
| 535259551:349:43:132 | 535259551   | X         | RECORD    | `application_schema`.`security_token` | security_token_idx |        349 |        43 |      132 | 'john.smith', 'mLBgrOXfVsXewCV333dVdvvDdxghJqQRU', 3, 70661 |
| 535259546:349:43:132 | 535259546   | X         | RECORD    | `application_schema`.`security_token` | security_token_idx |        349 |        43 |      132 | 'john.smith', 'mLBgrOXfVsXewCV333dVdvvDdxghJqQRU', 3, 70661 |
| 535259544:349:43:132 | 535259544   | X         | RECORD    | `application_schema`.`security_token` | security_token_idx |        349 |        43 |      132 | 'john.smith', 'mLBgrOXfVsXewCV333dVdvvDdxghJqQRU', 3, 70661 |
| 535259543:349:43:132 | 535259543   | X         | RECORD    | `application_schema`.`security_token` | security_token_idx |        349 |        43 |      132 | 'john.smith', 'mLBgrOXfVsXewCV333dVdvvDdxghJqQRU', 3, 70661 |
| 535259541:349:43:132 | 535259541   | X         | RECORD    | `application_schema`.`security_token` | security_token_idx |        349 |        43 |      132 | 'john.smith', 'mLBgrOXfVsXewCV333dVdvvDdxghJqQRU', 3, 70661 |
| 535259525:349:43:132 | 535259525   | X         | RECORD    | `application_schema`.`security_token` | security_token_idx |        349 |        43 |      132 | 'john.smith', 'mLBgrOXfVsXewCV333dVdvvDdxghJqQRU', 3, 70661 |
| 535259522:349:43:132 | 535259522   | X         | RECORD    | `application_schema`.`security_token` | security_token_idx |        349 |        43 |      132 | 'john.smith', 'mLBgrOXfVsXewCV333dVdvvDdxghJqQRU', 3, 70661 |
| 535259521:349:43:132 | 535259521   | X         | RECORD    | `application_schema`.`security_token` | security_token_idx |        349 |        43 |      132 | 'john.smith', 'mLBgrOXfVsXewCV333dVdvvDdxghJqQRU', 3, 70661 |
| 535259520:349:43:132 | 535259520   | X         | RECORD    | `application_schema`.`security_token` | security_token_idx |        349 |        43 |      132 | 'john.smith', 'mLBgrOXfVsXewCV333dVdvvDdxghJqQRU', 3, 70661 |
| 535259519:349:43:132 | 535259519   | X         | RECORD    | `application_schema`.`security_token` | security_token_idx |        349 |        43 |      132 | 'john.smith', 'mLBgrOXfVsXewCV333dVdvvDdxghJqQRU', 3, 70661 |
| 535259517:349:43:132 | 535259517   | X         | RECORD    | `application_schema`.`security_token` | security_token_idx |        349 |        43 |      132 | 'john.smith', 'mLBgrOXfVsXewCV333dVdvvDdxghJqQRU', 3, 70661 |
+----------------------+-------------+-----------+-----------+---------------------------------------+--------------------+------------+-----------+----------+-------------------------------------------------------------+

1 个答案:

答案 0 :(得分:0)

面临同样的问题..

如果你用mysqladmin刷新表/锁/线程,它应解决它:https://dev.mysql.com/doc/refman/5.5/en/mysqladmin.html