简体   繁体   English

为什么此查询速度慢? 我应该在这里使用MyISAM而不是InnoDB吗?

[英]Why is this query slow? Should I use MyISAM rather than InnoDB here?

I'm getting these about 5 times an hour in my slow query logs: 我在缓慢的查询日志中每小时得到大约5次这些信息:

# Query_time: 11.420629  Lock_time: 0.000033 Rows_sent: 0  Rows_examined: 0
SET timestamp=1267487708;
INSERT INTO record_lock (record_lock.module_id, record_lock.module_record_id, record_lock.site_id, record_lock.user_id, record_lock.expiration_date_time, record_lock.date_time_created) VALUES ('40', '12581', '940', '155254', '2010-03-02 00:24:57', '2010-03-01 23:54:57');

# Query_time: 2.095374  Lock_time: 0.000031 Rows_sent: 0  Rows_examined: 0
SET timestamp=1267488361;
DELETE
FROM record_lock
WHERE record_lock.user_id = 221659 AND record_lock.expiration_date_time IS NOT NULL;

The record_lock table currently uses InnoDB, and it has under a dozen records in it right now. record_lock表当前使用InnoDB,并且现在其中有十几条记录。

We have several thousand active users in our system. 我们的系统中有数千名活跃用户。 Each time they edit a record, we INSERT into this table. 每次他们编辑记录时,我们都会插入此表中。 And on each and every page load anywhere in the system, we 1) SELECT from the table to see if there are any locks for the current user and 2) run a DELETE query against that table if there are any records for the user, referencing the table's primary keys in the WHERE clause. 在系统上任何地方加载的每个页面上,我们1)从表中进行选择以查看当前用户是否有任何锁,以及2)如果该用户有任何记录,则对该表运行DELETE查询,引用该表的主键在WHERE子句中。

Here is the table's schema: 这是表的架构:

CREATE TABLE IF NOT EXISTS `record_lock` (
  `module_id` int(10) unsigned NOT NULL,
  `module_record_id` int(10) unsigned NOT NULL,
  `site_id` int(10) unsigned NOT NULL,
  `user_id` int(10) unsigned NOT NULL,
  `expiration_date_time` datetime NOT NULL,
  `date_time_created` datetime DEFAULT NULL,
  PRIMARY KEY (`module_id`,`module_record_id`),
  KEY `record_lock_site_id` (`site_id`),
  KEY `index_user_id` (`user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

How many queries are you doing per second? 您每秒要执行几个查询?

Could you not just put a locked field in the records themselves? 您是否可以仅在记录本身中放置一个锁定字段? I assume you are getting the record anyway. 我想您还是会得到记录的。 You could also use something like memcached for storing the locks. 您还可以使用诸如memcached之类的东西来存储锁。

I don't know the specifics off the top of my head, but my understanding is that InnoDB is great for concurrent reads, but sucks for concurrent writes. 我不知道具体细节,但我的理解是InnoDB对于并发读取非常有用,但对于并发写入很糟糕。 MyISAM might be better, but my gut tells me the current design is flawed. MyISAM可能更好,但是我的直觉告诉我当前的设计有缺陷。

您是否尝试过对查询运行EXPLAIN?

Is it possibly too many connections trying to hit the same table? 是否可能有太多连接试图打到同一张桌子? You could try segmenting the table on user_id to help with that. 您可以尝试对user_id上的表进行分段以帮助实现此目的。

Switching on the Innodb monitors can help narrow down the causes of poor performance: 打开Innodb监视器可以帮助缩小性能不佳的原因:

SHOW ENGINE INNODB STATUS and the InnoDB Monitors 显示引擎INNODB状态和InnoDB监视器

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM