简体   繁体   中英

MYSQL MEMBER LOG QUERY SLOW - PERFOMANCE PROBLEM

I have a table where I log members.

There are 1,486,044 records here.

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

However, this query takes 5 seconds. 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) .

Note that I removed the single quotes around the filtering value for user , since this column is a number. This does not necessarily speeds things up, but is cleaner.

Also: select * is not a good practice, and not good for performance. 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) .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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