简体   繁体   English

MYSQL 成员日志查询慢 - 性能问题

[英]MYSQL MEMBER LOG QUERY SLOW - PERFOMANCE PROBLEM

I have a table where I log members.我有一张表,用于记录成员。

There are 1,486,044 records here.这里有 1,486,044 条记录。

SELECT * FROM `user_log` WHERE user = '1554143' order by id desc

However, this query takes 5 seconds.但是,此查询需要 5 秒。 What do you recommend ?你有什么建议吗 ?

Table construction below;下面的表结构;

CREATE TABLE IF NOT EXISTS `user_log` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `user` int(11) NOT NULL,
  `operation_detail` varchar(100) NOT NULL,
  `ip_adress` varchar(50) NOT NULL,
  `l_date` datetime NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
COMMIT;

For this query:对于此查询:

SELECT * FROM `user_log` WHERE user = 1554143 order by id desc

You want an index on (user, id desc) .您需要(user, id desc)上的索引。

Note that I removed the single quotes around the filtering value for user , since this column is a number.请注意,我删除了user过滤值周围的单引号,因为该列是一个数字。 This does not necessarily speeds things up, but is cleaner.这不一定会加快速度,但更干净。

Also: select * is not a good practice, and not good for performance.另外: select *不是一个好习惯,也不利于性能。 You should enumerate the columns you want in the resultset (if you don't need them all, do not select them all).您应该在结果集中枚举您想要的列(如果您不需要它们,请不要全部选择它们)。 If you want all columns, since your table has not a lot of columns, you might want to try a covering index on all 5 columns, like: (user, id desc, operation_detail, ip_adress, l_date) .如果您想要所有列,因为您的表没有很多列,您可能想要尝试对所有 5 列进行覆盖索引,例如: (user, id desc, operation_detail, ip_adress, l_date)

除了已经提到的在 (user, id) 上创建索引的选项之外,可能更好的选择是将表转换为 InnoDB,因为仅在 (user) 上创建索引。

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

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