snippetsqlMinor
How can I optimize this SQL ranking generator query?
Viewed 0 times
thiscansqlquerygeneratoroptimizehowranking
Problem
I have a World Cup fixture app where people can guess the results of the matches. Everything is working great, but each time I update the ranking, it takes 20 minutes for just 3000 users. So there must be something really wrong with this.
These are the tables being used:
Users
Matches
Predictions
``
These are the tables being used:
Users
CREATE TABLE `users` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`id_facebook` varchar(255) COLLATE utf8_bin NOT NULL DEFAULT '',
`first_name` varchar(255) COLLATE utf8_bin NOT NULL DEFAULT '',
`last_name` varchar(255) COLLATE utf8_bin NOT NULL DEFAULT '',
`name` varchar(255) COLLATE utf8_bin NOT NULL DEFAULT '',
`gender` varchar(20) COLLATE utf8_bin NOT NULL DEFAULT '',
`locale` varchar(10) COLLATE utf8_bin NOT NULL DEFAULT '',
`country` varchar(255) COLLATE utf8_bin DEFAULT NULL,
`location` varchar(255) COLLATE utf8_bin DEFAULT NULL,
`timezone` int(11) NOT NULL,
`created_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`fb_token` varchar(255) COLLATE utf8_bin DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `name` (`name`),
KEY `id_facebook` (`id_facebook`)
) ENGINE=InnoDB AUTO_INCREMENT=3478 DEFAULT CHARSET=utf8 COLLATE=utf8_bin;Matches
CREATE TABLE `matches` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`datetime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`venue` varchar(100) COLLATE utf8_bin NOT NULL,
`stage` varchar(100) COLLATE utf8_bin NOT NULL,
`teamA` varchar(3) COLLATE utf8_bin NOT NULL DEFAULT '',
`teamB` varchar(3) COLLATE utf8_bin NOT NULL DEFAULT '',
`goalsAinit` int(11) DEFAULT NULL,
`goalsBinit` int(11) DEFAULT NULL,
`goalsAadded` int(11) DEFAULT NULL,
`goalsBadded` int(11) DEFAULT NULL,
`penaltiesA` int(11) DEFAULT NULL,
`penaltiesB` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=65 DEFAULT CHARSET=utf8 COLLATE=utf8_bin;Predictions
``
CREATE TABLE pSolution
You are doing a huge amount of work in the inner rankings table.
The most important observations here are:
Also, I presume the 'iso' column comes from the 'flags' table?
I messed with your query, you will see what I have done. I believe it will work, but it may just return null-values for users with no predictions... but, this allows me to use an inner-join between predictions and matches. If there's a problem with nulls in the data column, then you can solve that with judicious use of IsNull conditions on the outer selects. I also reformatted the if-conditions so I could make sense of them. You can collapse it all down again if you need.
Without further ado....
Edit revised my query to make the ranking system better.....
Edit: previous version of answer code
The most important observations here are:
- you are doing a 'group by' on only the user_id column, but the actual columns that are not aggregated are many. This is a bad practice... each column that is not aggregated, should be part of the group-by clause.
- you are doing multiple left-joins in there too, and left-joins have a significant impact on the possible optimizations the database can do.
- you are joining to the user table when calculating the scores, but all you need is the
id_userfrom thepredictionstable.
Also, I presume the 'iso' column comes from the 'flags' table?
I messed with your query, you will see what I have done. I believe it will work, but it may just return null-values for users with no predictions... but, this allows me to use an inner-join between predictions and matches. If there's a problem with nulls in the data column, then you can solve that with judicious use of IsNull conditions on the outer selects. I also reformatted the if-conditions so I could make sense of them. You can collapse it all down again if you need.
Without further ado....
Edit revised my query to make the ranking system better.....
INSERT INTO ranking
SELECT @rank:=IFNULL(@rank,0)+1 AS rank,
id as id_user,
id_facebook,
first_name,
last_name,
name,
iso,
country,
exact,
correct,
points,
total_predictions
FROM users u
LEFT JOIN flags f ON u.country = f.country
LEFT JOIN (
SELECT p.id_user as id_user,
SUM( IF((p.goalsAinit = m.goalsAinit AND p.goalsBinit = m.goalsBinit), 1, 0)) AS exact,
SUM( IF( ( (p.goalsAinit > p.goalsBinit AND m.goalsAinit > m.goalsBinit)
OR (p.goalsAinit p.goalsBinit AND m.goalsAinit > m.goalsBinit)
OR (p.goalsAinit < p.goalsBinit AND m.goalsAinit < m.goalsBinit)
OR (p.goalsAinit = p.goalsBinit AND m.goalsAinit = m.goalsBinit),
ROUND(20*1.5),
IF(p.goalsAinit IS NOT NULL AND p.goalsBinit IS NOT NULL AND m.goalsAinit IS NOT NULL AND m.goalsBinit IS NOT NULL,10,0)))) AS points,
SUM(p.goalsAinit IS NOT NULL AND p.goalsBinit IS NOT NULL) AS total_predictions
FROM predictions p
INNER JOIN matches m ON m.id = p.id_match
GROUP BY p.id_user) as userstats on u.id = userstats.id_user
ORDER BY points DESC, exact DESC, correct DESC, total_predictions DESC, idEdit: previous version of answer code
INSERT INTO ranking
SELECT rank, id_user, id_facebook, iso, country, first_name, last_name, name, exact, correct, points, total_predictions
FROM (SELECT @rank:=0) AS r,
(
SELECT @rank:=@rank+1 AS rank,
id as id_user,
id_facebook,
first_name,
last_name,
name,
iso,
country,
exact,
correct,
points,
total_predictions
FROM users u
LEFT JOIN flags f ON u.country = f.country
LEFT JOIN (
SELECT p.id_user as id_user,
SUM( IF((p.goalsAinit = m.goalsAinit AND p.goalsBinit = m.goalsBinit), 1, 0)) AS exact,
SUM( IF( ( (p.goalsAinit > p.goalsBinit AND m.goalsAinit > m.goalsBinit)
OR (p.goalsAinit p.goalsBinit AND m.goalsAinit > m.goalsBinit)
OR (p.goalsAinit < p.goalsBinit AND m.goalsAinit < m.goalsBinit)
OR (p.goalsAinit = p.goalsBinit AND m.goalsAinit = m.goalsBinit),
ROUND(20*1.5),
IF(p.goalsAinit IS NOT NULL AND p.goalsBinit IS NOT NULL AND m.goalsAinit IS NOT NULL AND m.goalsBinit IS NOT NULL,10,0)))) AS points,
SUM(p.goalsAinit IS NOT NULL AND p.goalsBinit IS NOT NULL) AS total_predictions
FROM predictions p
INNER JOIN matches m ON m.id = p.id_match
GROUP BY p.id_user) as userstats on u.id = userstats.id_user
ORDER BY points DESC, exact DESC, correct DESC, total_predictions DESC, id
) AS overall_rankings;Code Snippets
INSERT INTO ranking
SELECT @rank:=IFNULL(@rank,0)+1 AS rank,
id as id_user,
id_facebook,
first_name,
last_name,
name,
iso,
country,
exact,
correct,
points,
total_predictions
FROM users u
LEFT JOIN flags f ON u.country = f.country
LEFT JOIN (
SELECT p.id_user as id_user,
SUM( IF((p.goalsAinit = m.goalsAinit AND p.goalsBinit = m.goalsBinit), 1, 0)) AS exact,
SUM( IF( ( (p.goalsAinit > p.goalsBinit AND m.goalsAinit > m.goalsBinit)
OR (p.goalsAinit < p.goalsBinit AND m.goalsAinit < m.goalsBinit)
OR (p.goalsAinit = p.goalsBinit AND m.goalsAinit = m.goalsBinit))
AND !(p.goalsAinit = m.goalsAinit AND p.goalsBinit = m.goalsBinit), 1, 0)) AS correct,
SUM(IF( (p.goalsAinit = m.goalsAinit AND p.goalsBinit = m.goalsBinit),
(20*3),
IF( (p.goalsAinit > p.goalsBinit AND m.goalsAinit > m.goalsBinit)
OR (p.goalsAinit < p.goalsBinit AND m.goalsAinit < m.goalsBinit)
OR (p.goalsAinit = p.goalsBinit AND m.goalsAinit = m.goalsBinit),
ROUND(20*1.5),
IF(p.goalsAinit IS NOT NULL AND p.goalsBinit IS NOT NULL AND m.goalsAinit IS NOT NULL AND m.goalsBinit IS NOT NULL,10,0)))) AS points,
SUM(p.goalsAinit IS NOT NULL AND p.goalsBinit IS NOT NULL) AS total_predictions
FROM predictions p
INNER JOIN matches m ON m.id = p.id_match
GROUP BY p.id_user) as userstats on u.id = userstats.id_user
ORDER BY points DESC, exact DESC, correct DESC, total_predictions DESC, idINSERT INTO ranking
SELECT rank, id_user, id_facebook, iso, country, first_name, last_name, name, exact, correct, points, total_predictions
FROM (SELECT @rank:=0) AS r,
(
SELECT @rank:=@rank+1 AS rank,
id as id_user,
id_facebook,
first_name,
last_name,
name,
iso,
country,
exact,
correct,
points,
total_predictions
FROM users u
LEFT JOIN flags f ON u.country = f.country
LEFT JOIN (
SELECT p.id_user as id_user,
SUM( IF((p.goalsAinit = m.goalsAinit AND p.goalsBinit = m.goalsBinit), 1, 0)) AS exact,
SUM( IF( ( (p.goalsAinit > p.goalsBinit AND m.goalsAinit > m.goalsBinit)
OR (p.goalsAinit < p.goalsBinit AND m.goalsAinit < m.goalsBinit)
OR (p.goalsAinit = p.goalsBinit AND m.goalsAinit = m.goalsBinit))
AND !(p.goalsAinit = m.goalsAinit AND p.goalsBinit = m.goalsBinit), 1, 0)) AS correct,
SUM(IF( (p.goalsAinit = m.goalsAinit AND p.goalsBinit = m.goalsBinit),
(20*3),
IF( (p.goalsAinit > p.goalsBinit AND m.goalsAinit > m.goalsBinit)
OR (p.goalsAinit < p.goalsBinit AND m.goalsAinit < m.goalsBinit)
OR (p.goalsAinit = p.goalsBinit AND m.goalsAinit = m.goalsBinit),
ROUND(20*1.5),
IF(p.goalsAinit IS NOT NULL AND p.goalsBinit IS NOT NULL AND m.goalsAinit IS NOT NULL AND m.goalsBinit IS NOT NULL,10,0)))) AS points,
SUM(p.goalsAinit IS NOT NULL AND p.goalsBinit IS NOT NULL) AS total_predictions
FROM predictions p
INNER JOIN matches m ON m.id = p.id_match
GROUP BY p.id_user) as userstats on u.id = userstats.id_user
ORDER BY points DESC, exact DESC, correct DESC, total_predictions DESC, id
) AS overall_rankings;Context
StackExchange Code Review Q#54207, answer score: 4
Revisions (0)
No revisions yet.