I have this query , take long time when I executed it
SELECT DISTINCT ticket.`id`,
`sender`,
`text`,
`receivedtime`,
`priorityid`,
`cityid`,
`categoryid`,
`statusid`,
`activeuserid`,
`note`,
`operationid`,
'' AS SMSHistory,
'' AS replySMS,
'' AS ticketHistory
FROM `ticket`
LEFT OUTER JOIN tickethistory
ON tickethistory.ticketid = ticket.id
LEFT OUTER JOIN users
ON tickethistory.uid = users.uid
ORDER BY ticket.`id` DESC
LIMIT 0, 50
and here is the table structure :
Ticket:
CREATE TABLE `ticket` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`sender` varchar(50) NOT NULL,
`text` text NOT NULL,
`receivedTime` datetime NOT NULL,
`priorityId` int(10) NOT NULL,
`cityId` int(10) NOT NULL,
`categoryId` int(10) NOT NULL,
`statusId` int(10) NOT NULL,
`activeUserId` int(11) NOT NULL,
`note` text NOT NULL,
`operationId` int(10) NOT NULL,
`gateway` varchar(80) NOT NULL,
KEY `Index 1` (`id`),
KEY `sender` (`sender`),
KEY `priorityId` (`priorityId`),
KEY `cityId` (`cityId`),
KEY `categoryId` (`categoryId`),
KEY `statusId` (`statusId`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
----
Tickethistory :
CREATE TABLE `tickethistory` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`ticketId` int(10) NOT NULL,
`uid` int(11) NOT NULL,
`actionId` int(10) NOT NULL,
`time` datetime NOT NULL,
`param1` text NOT NULL,
`param2` text NOT NULL,
`param3` text NOT NULL,
KEY `Index 1` (`id`),
KEY `ticketId` (`ticketId`),
KEY `uid` (`uid`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
----
users :
CREATE TABLE IF NOT EXISTS `users` (
`Uid` int(11) NOT NULL AUTO_INCREMENT,
`UserName` varchar(255) NOT NULL,
`Upassword` varchar(32) NOT NULL,
`UName` text NOT NULL,
`Ucountry` text NOT NULL,
`Umobile` varchar(255) NOT NULL,
`UregisterDate` date NOT NULL DEFAULT '0000-00-00',
`Ugroup` char(1) NOT NULL DEFAULT 'U',
`Usender` varchar(11) NOT NULL DEFAULT 'SMS',
`Ucredits` decimal(11,2) NOT NULL DEFAULT '0.00',
`Uemail` varchar(255) NOT NULL,
`CreditUpdatedDate` date DEFAULT NULL,
`USMSC` varchar(30) NOT NULL,
`repeatedDuration` int(10) DEFAULT '0',
`langid` varchar(10) DEFAULT 'Ar',
`parentId` int(11) NOT NULL DEFAULT '0',
`Usess` varchar(255) NOT NULL DEFAULT '0',
PRIMARY KEY (`Uid`),
UNIQUE KEY `UserName` (`UserName`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
Here is the explain command result:
'id';'select_type';'table';'type';'possible_keys';'key';'key_len';'ref';'rows';'Extra'
'1';'SIMPLE';'ticket';'ALL';'';'';'';'';'348580';'Using temporary; Using filesort'
'1';'SIMPLE';'tickethistory';'ref';'ticketId';'ticketId';'4';'ticket.id';'2';'Distinct'
'1';'SIMPLE';'users';'eq_ref';'PRIMARY';'PRIMARY';'4';'tickethistory.uid';'1';'Using index; Distinct'
The EXPLAIN suggests that it’s using a temporary table and filesort to order the records in the TICKET table. That’s a little weird, because there is an index on ID which you’re sorting on, but given it matches 350K records, that’s probably why it’s slow.
As you’re trying to find the latest records, try to include a limiting “where” clause, for instance by limiting the search to the last week (don’t forget to create an index on receivedTime).
You might also consider not having an outer join from ticketHistory to User – the UID column is NOT NULL, so there should be no records without a matching user.