I have a fairly simple query that runs super fast most of the time and takes up to 30 seconds in other rare occasions. I have no clue as to what is causing it. Here is an example of what I'm talking about.
mysql> UPDATE matchSessionMembers msm JOIN matchSessions ms ON msm.matchNo = ms.matchNo AND ms.STATUS != 'N' SET msm.STATUS = 'P' WHERE msm.memberNo = 7 AND msm.STATUS = 'M';
Query OK, 0 rows affected (0.00 sec)
Rows matched: 0 Changed: 0 Warnings: 0mysql> UPDATE matchSessionMembers msm JOIN matchSessions ms ON msm.matchNo = ms.matchNo AND ms.STATUS != 'N' SET msm.STATUS = 'P' WHERE msm.memberNo = 7 AND msm.STATUS = 'M';
Query OK, 0 rows affected (0.00 sec)
Rows matched: 0 Changed: 0 Warnings: 0mysql> UPDATE matchSessionMembers msm JOIN matchSessions ms ON msm.matchNo = ms.matchNo AND ms.STATUS != 'N' SET msm.STATUS = 'P' WHERE msm.memberNo = 7 AND msm.STATUS = 'M';
Query OK, 0 rows affected (3.01 sec)
Rows matched: 0 Changed: 0 Warnings: 0mysql> UPDATE matchSessionMembers msm JOIN matchSessions ms ON msm.matchNo = ms.matchNo AND ms.STATUS != 'N' SET msm.STATUS = 'P' WHERE msm.memberNo = 7 AND msm.STATUS = 'M';
Query OK, 0 rows affected (0.00 sec)
Rows matched: 0 Changed: 0 Warnings: 0mysql> UPDATE matchSessionMembers msm JOIN matchSessions ms ON msm.matchNo = ms.matchNo AND ms.STATUS != 'N' SET msm.STATUS = 'P' WHERE msm.memberNo = 7 AND msm.STATUS = 'M';
Query OK, 0 rows affected (1.88 sec)
Rows matched: 0 Changed: 0 Warnings: 0mysql> UPDATE matchSessionMembers msm JOIN matchSessions ms ON msm.matchNo = ms.matchNo AND ms.STATUS != 'N' SET msm.STATUS = 'P' WHERE msm.memberNo = 7 AND msm.STATUS = 'M';
Query OK, 0 rows affected (0.00 sec)
Rows matched: 0 Changed: 0 Warnings: 0
In this query I have set indexes for all msm.matchNo, ms.matchNo, ms.status, msm.status, and msm.memberNo.
I ran the same query 6 times and had 4 cases where the queries finished in under a second while 2 cases took more than a second to process. I did some more testing and got profile data for one of the more severe cases which took up to 30 seconds to execute this stupid query.
mysql> SHOW PROFILE FOR QUERY 3;
+---------------------------+-----------+
| Status | Duration |
+---------------------------+-----------+
| starting | 0.000075 |
| checking permissions | 0.000013 |
| checking permissions | 0.000012 |
| Opening tables | 0.000073 |
| checking permissions | 0.000011 |
| checking permissions | 0.000013 |
| System lock | 0.000023 |
| init | 0.000021 |
| updating main table | 0.000014 |
| optimizing | 0.000020 |
| statistics | 0.000117 |
| preparing | 0.000028 |
| executing | 0.000011 |
| Sending data | 31.940094 | <==
| updating reference tables | 0.000039 |
| end | 0.000016 |
| end | 0.000015 |
| query end | 0.000048 |
| closing tables | 0.000039 |
| freeing items | 0.000036 |
| logging slow query | 0.000011 |
| cleaning up | 0.000014 |
+---------------------------+-----------+
I have a few other UPDATE queries with similar symptoms. Why is this query taking so long to execute even though there aren't any rows actually being updated? What could this "Sending data" bottleneck possibly be?
** edit: added CREATE TABLE code for both tables being used
CREATE TABLE
matchSessionMembers(matchNoint(11) NOT NULL,memberNoint(11) NOT NULL,statuschar(1) NOT NULL DEFAULT 'M' COMMENT 'M=match, P=pending, Y=saidYes, N=saidNo, D=deleted',mutedchar(1) NOT NULL DEFAULT 'N',deletedchar(1) NOT NULL DEFAULT 'N',regDatetimedatetime DEFAULT NULL,exitDatetimedatetime DEFAULT NULL,
PRIMARY KEY (matchNo,memberNo),
KEYstatus(status),
KEYFK_matchSessionMembers_memberNo(memberNo),
KEYregDatetime(regDatetime),
KEYFK_matchSessionMembers_matchNo(matchNo),
CONSTRAINTFK_matchSessionMembers_matchNoFOREIGN KEY (matchNo) REFERENCESmatchSessions(matchNo),
CONSTRAINTFK_matchSessionMembers_memberNoFOREIGN KEY (memberNo) REFERENCESmembers(memberNo)
) ENGINE=InnoDB DEFAULT CHARSET=utf8CREATE TABLE
matchSessions(matchNoint(11) NOT NULL AUTO_INCREMENT,memberCountint(11) NOT NULL,statuschar(1) NOT NULL DEFAULT 'P' COMMENT 'P=pending, Y=successful, N=unsuccessful, X=expired',isQuickMatchchar(1) NOT NULL DEFAULT 'N',openchar(1) NOT NULL DEFAULT 'N',regDatetimedatetime DEFAULT NULL,activeDatedate DEFAULT NULL,expireDatedate DEFAULT NULL,
PRIMARY KEY (matchNo),
KEYstatus(status),
KEYactiveDate(activeDate),
KEYexpireDate(expireDate)
) ENGINE=InnoDB AUTO_INCREMENT=113912 DEFAULT CHARSET=utf8
UPDATE matchSessionMembers msm JOIN matchSessions ms ON msm.matchNo = ms.matchNo SET msm.STATUS = 'P' WHERE msm.memberNo = 7 AND msm.STATUS = 'M' AND ms.STATUS != 'N';and report back. - Eugen Rieck