查询多个连接和索引卡在“复制到tmp表”

时间:2014-01-06 21:18:40

标签: mysql

我有这个查询在奇怪的情况下不运行。它正在进行两次连接,我在两个不同的表上有两个同名的索引(但这不重要)。当我从任一表中删除其中一个索引时,查询运行正常。使其运行的其他东西是删除order by子句,但我绝对需要它。当我运行show processlist时,状态将停留在“复制到tmp表”上。我正在谈论的索引是channelID。

查询

SELECT twitterTweets.*, 
       COUNT(twitterRetweets.id) AS retweets, 
       sTwitter.followers, 
       sTwitter.date 
FROM   twitterTweets 
       LEFT JOIN twitterRetweets 
              ON twitterTweets.id = twitterTweetsID 
       JOIN sTwitter 
         ON DATE(twitterTweets.dateCreated) = sTwitter.date 
            AND twitterTweets.channelID = sTwitter.channelID 
WHERE  twitterTweets.channelID = 32 
       AND type = 'tweet' 
       AND DATE(dateCreated) >= '2013-12-05' 
       AND DATE(dateCreated) <= '2014-01-05' 
GROUP  BY twitterTweets.id 
ORDER  BY dateCreated 

解释结果

1   SIMPLE  sTwitter        ref channelID       channelID       5   const                   1162    Using where; Using temporary; Using filesort
1   SIMPLE  twitterTweets   ref channelID       channelID       5   const                   17456   Using where
1   SIMPLE  twitterRetweets ref twitterTweetsID twitterTweetsID 5   social.twitterTweets.id 3   

为三个表创建

CREATE TABLE `twitterTweets` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `clientID` int(11) DEFAULT NULL,
  `divisionID` int(11) DEFAULT NULL,
  `accountID` int(11) DEFAULT NULL,
  `channelID` int(11) DEFAULT NULL,
  `type` enum('tweet','reply','direct in','direct out') COLLATE utf8_unicode_ci DEFAULT 'tweet',
  `subType` enum('reply beginning','retweet beginning','reply middle','retweet middle') COLLATE utf8_unicode_ci DEFAULT NULL,
  `tweetID` bigint(20) DEFAULT NULL,
  `tweet` text COLLATE utf8_unicode_ci,
  `replies` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL,
  `hash` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL,
  `retweet` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL,
  `source` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `media` text COLLATE utf8_unicode_ci,
  `tweetUrls` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `expandedUrls` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `replyToStatus` bigint(20) DEFAULT NULL,
  `user` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL,
  `followers` int(11) DEFAULT NULL,
  `following` int(11) DEFAULT NULL,
  `updates` int(11) DEFAULT NULL,
  `group1` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL,
  `campaign` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL,
  `segment` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL,
  `destination` varchar(100) COLLATE utf8_unicode_ci DEFAULT NULL,
  `retweets` smallint(6) DEFAULT '0',
  `favorites` smallint(6) DEFAULT NULL,
  `dateCreated` datetime DEFAULT NULL,
  `dateAdded` datetime DEFAULT NULL,
  `dateUpdated` datetime DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `channelID` (`channelID`)
) ENGINE=MyISAM AUTO_INCREMENT=296264 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

CREATE TABLE `twitterRetweets` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `clientID` int(11) DEFAULT NULL,
  `divisionID` int(11) DEFAULT NULL,
  `accountID` int(11) DEFAULT NULL,
  `channelID` int(11) DEFAULT NULL,
  `twitterTweetsID` int(11) DEFAULT NULL,
  `tweetID` bigint(20) DEFAULT NULL,
  `screenName` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL,
  `followers` int(11) DEFAULT NULL,
  `following` int(11) DEFAULT NULL,
  `updates` int(11) DEFAULT NULL,
  `favorites` smallint(6) DEFAULT NULL,
  `dateAdded` datetime DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `twitterTweetsID` (`twitterTweetsID`)
) ENGINE=MyISAM AUTO_INCREMENT=93821 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

CREATE TABLE `sTwitter` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `clientID` int(11) DEFAULT NULL,
  `divisionID` int(11) DEFAULT NULL,
  `accountID` int(11) DEFAULT NULL,
  `channelID` int(11) DEFAULT NULL,
  `following` int(11) DEFAULT '0',
  `followers` int(11) DEFAULT '0',
  `updates` int(11) DEFAULT '0',
  `date` date DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `channelID` (`channelID`)
) ENGINE=MyISAM AUTO_INCREMENT=35615 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

那么如何在两个索引到位的情况下运行这个东西呢?我必须忽略其中一个吗?

0 个答案:

没有答案