简体   繁体   中英

Optimizing simple MySQL query response time

I have two tables in MySQL. One contains users and the other contains transaction data.

I am trying to find out the top 50 users that made the most profit.

CREATE TABLE IF NOT EXISTS `t_logs` (
  `tid` int(11) NOT NULL AUTO_INCREMENT,
  `userid` int(11) NOT NULL,
  `amount` int(11) NOT NULL DEFAULT '0',
  PRIMARY KEY (`tid`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;


CREATE TABLE IF NOT EXISTS `t_users` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `login` varchar(100) NOT NULL,
  `name` varchar(100) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;

Query:

SELECT *, (SELECT SUM(amount) FROM `t_logs` WHERE userid=u.id LIMIT 0,1) AS profit
FROM `t_users` u
ORDER BY profit DESC
LIMIT 0,50

Now the problem is that if I have 1000 entries in the t_users table and 3000 transactions in the t_logs table, this query takes 25seconds on a VDS with Apache and 2GB of RAM, or 9seconds on my local computer using XAMPP(I have 16GB of RAM).

Question is: Is there anything more that I can do to optimize all this? Maybe change the table engine from MyISAM to something else? Or maybe my query is not effective? Or the only solution is to add more RAM to the VDS.

If we try to add 10000 users and 10000 logs, the query takes 250 seconds on the VDS. What are my options if I expect to have more than 50000 users and more than 1 million logs?

You should be using a LEFT JOIN with GROUP BY instead of a subquery, and you need an index on t_logs.userid :

SELECT u.*, SUM(l.amount) AS profit
FROM t_users u
LEFT JOIN t_logs l
  ON l.userid = u.id
GROUP BY u.id
ORDER BY profit DESC
LIMIT 50
SELECT u.* , p.profit
FROM `t_users` u
LEFT JOIN (
  SELECT userid, SUM(amount) as profit FROM `t_logs` GROUP BY userid) AS p
ON p.userid=u.id 
ORDER BY p.profit DESC
LIMIT 0,50

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