vendredi 3 avril 2015

MySQL query on medium-sized table fast on one system, slow on another

I have the following table with about 40k rows:



CREATE TABLE IF NOT EXISTS `log_ui_activity` (
`uiActivityLogEntryId` INTEGER UNSIGNED auto_increment ,
`uid` INTEGER UNSIGNED,
`from` DATETIME,
`duration` INTEGER UNSIGNED,
`nCharactersTyped` INTEGER UNSIGNED,
`nClicks` INTEGER UNSIGNED,
`hadOtherInteractions` INTEGER UNSIGNED,
`currentPage` TEXT,
`currentPageArgs` TEXT,
`currentPageStateInfo` TEXT,
`createdAt` DATETIME NOT NULL,
`updatedAt` DATETIME NOT NULL,
PRIMARY KEY (`uiActivityLogEntryId`)
) ENGINE=InnoDB;


with two additional indices, each on createdAt and uid.


When I run the following query:



SELECT *
FROM log_ui_activity
WHERE `createdAt` IN (
SELECT MAX(`createdAt`) FROM log_ui_activity
GROUP BY uid);


It finishes within 0.2s on Windows 7 and running XAMPP with:



MySql Ver 14.14 Distrib 5.6.20, for Win32 (x86)



However, it takes several minutes when running the exact same query on an exact copy of the data on a Mac Pro (verified: structure, indices and engine are all the same) running MAMP with:



MySql Ver 14.14 Distrib 5.5.38, for osx10.6 (i386) using EditLine wrapper



I have even tried different clients...


Any ideas how the query can be so much slower, even though everything is/appears to be identical?


UPDATE


As suggested in the answer, using JOIN instead of IS IN fixes things. For reference, this is the JOIN statement:



SELECT *
FROM log_ui_activity a
INNER JOIN (
(SELECT MAX(`createdAt`) createdAt FROM log_ui_activity GROUP BY uid) tmp
)
ON (a.createdAt = tmp.createdAt);

Aucun commentaire:

Enregistrer un commentaire