How to speed up slow MySQL UPDATE queries with InnoDB tables

1.7k views Asked by At

I have a very simple MySQL update query on my InnoDB table.

UPDATE `players_teams` SET t_last_active=NOW() WHERE t_player_id=11225 AND t_team_id=6912 AND t_season_id=2002 LIMIT 1

My table is structured as so:

CREATE TABLE `players_teams` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `t_player_id` int(11) DEFAULT NULL,
  `t_team_id` int(11) DEFAULT NULL,
  `t_league_id` int(11) DEFAULT NULL,
  `t_season_id` int(11) DEFAULT NULL,
  `t_div` varchar(64) DEFAULT NULL,
  `t_player_number` varchar(3) DEFAULT NULL,
  `t_player_jersey_size` enum('UNKNOWN','XS','S','M','L','XL','XXL','XXXL') DEFAULT 'UNKNOWN',
  `t_player_registration_number` varchar(64) DEFAULT NULL,
  `t_player_class` enum('ROSTER','SPARE','COACH','INJURED','HOLIDAY','SUSPENDED','SCOREKEEPER') DEFAULT 'ROSTER',
  `t_access_level` enum('PLAYER','MANAGER','ASSISTANT') DEFAULT 'PLAYER',
  `t_player_position` enum('ANY','FORWARD','DEFENCE','GOALIE','PITCHER','CATCHER','FIRST BASE','SECOND BASE','THIRD BASE','SHORTSTOP','LEFT FIELD','CENTER FIELD','RIGHT FIELD') DEFAULT 'ANY',
  `t_spare_status` enum('INVITED','IN','OUT') DEFAULT NULL,
  `t_drink_next` int(1) DEFAULT '0',
  `t_no_fees` tinyint(1) DEFAULT '0',
  `t_no_drink` tinyint(1) DEFAULT '0',
  `t_auto_check_in` tinyint(1) DEFAULT '0',
  `t_print_reminder` tinyint(1) DEFAULT '0',
  `t_notes` text,
  `t_last_chatter_id` int(11) DEFAULT NULL,
  `t_last_history_id` int(11) DEFAULT NULL,
  `t_last_active` timestamp NULL DEFAULT NULL,
  `t_status` enum('ACTIVE','INACTIVE','ARCHIVED') DEFAULT 'ACTIVE',
  `t_added` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  KEY `t_player_id` (`t_player_id`),
  KEY `t_team_id` (`t_team_id`),
  KEY `t_season_id` (`t_season_id`),
  KEY `t_player_id_2` (`t_player_id`,`t_team_id`,`t_season_id`),
  KEY `Team/Player ID` (`t_team_id`,`t_player_id`),
  KEY `UpdatePlayersDiv` (`t_team_id`,`t_season_id`)
) ENGINE=InnoDB AUTO_INCREMENT=23454 DEFAULT CHARSET=latin1;

This simple update query takes on average, 3.5 seconds? I'm running this on a MediaTemple MySQL GRID container.

Here is the result of an EXPLAIN when switching the UPDATE to a SELECT.

Results of EXPLAIN

Can someone provide some insight on how I'm not doing this correctly?

[EDIT: Added list of Indexes]

So is this a big mess of indexes? Do I have a bunch of redundant indexes in here?

enter image description here

Cheers, Jon

1

There are 1 answers

5
twentylemon On BEST ANSWER

It's using the wrong key instead of the index on the 3 columns. You can hint at indexes with USE KEY ... syntax.

https://dev.mysql.com/doc/refman/5.1/en/index-hints.html

You may also want to try reordering your key on the 3 columns. Generally, you want the most restricting column to be first in the index, then the next most restricting and so on.