Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/62.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MYSQL:左外连接sql需要很长时间_Mysql_Sql_Optimization - Fatal编程技术网

MYSQL:左外连接sql需要很长时间

MYSQL:左外连接sql需要很长时间,mysql,sql,optimization,Mysql,Sql,Optimization,我有这个查询,执行它需要很长时间 SELECT DISTINCT ticket.`id`, `sender`, `text`, `receivedtime`, `priorityid`, `cityid`, `categoryid`, `statusid`,

我有这个查询,执行它需要很长时间

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 
以下是表格结构:

门票: 票务记录: 用户:

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;

以下是explain命令结果:

  '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'

EXPLAIN建议使用临时表和filesort对TICKET表中的记录进行排序。这有点奇怪,因为在ID上有一个索引,您正在对其进行排序,但考虑到它匹配350K条记录,这可能就是它速度慢的原因

当您试图查找最新记录时,请尝试包含一个限制“where”子句,例如将搜索限制到上周(不要忘记在receivedTime上创建索引)


您也可以考虑从TigKeTistor到用户没有外部连接——UID列不是NULL,因此没有匹配的用户就不应该有记录。 @Ali创建fiddle-->架构时出错:您的SQL语法有错误;检查与您的MySQL服务器版本对应的手册,以了解在第14行的“索引1(id)、密钥发送者(sender)、密钥优先级id(priorityId)、密钥cityId”附近使用的正确语法:请也包括

用户
表。还有一个建议:也许你想考虑使用<代码>主键< /代码>使用<代码> ID>代码>列。键
Index 1
id
),键
sender
sender
),键
priorityId
),键
cityId
cityId
),键
categoryId
categoryId
),键
statusId
statusId>)但是我想做一个基于搜索的用户-UID在您发布的SQL中没有UID的“where”子句-这是另一个查询的一部分吗?
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;
  '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'