0

我刚刚注意到这很奇怪(至少在我看来),事情......

我已经确保查询没有被缓存,但是,在IN()函数中只使用一个值的查询非常慢......我想不出任何理由。我可以在那里放一个虚拟值但是......我真的不想:/

SELECT Clues.* FROM Clues INNER JOIN SolutionWords 
ON SolutionWords.SolutionWordID = Clues.SolutionWordID  
WHERE SolutionWord COLLATE utf8_general_ci = 'TEST A' 
AND Clues.CultureID IN (1);

受影响的行:0
时间:3.843ms

SELECT Clues.* FROM Clues INNER JOIN SolutionWords 
ON SolutionWords.SolutionWordID = Clues.SolutionWordID  
WHERE SolutionWord COLLATE utf8_general_ci = 'TEST B' 
AND Clues.CultureID IN (1,2);

受影响的行:0
时间:0.141ms

SELECT Clues.* FROM Clues INNER JOIN SolutionWords 
ON SolutionWords.SolutionWordID = Clues.SolutionWordID  
WHERE SolutionWord COLLATE utf8_general_ci = 'TEST C' 
AND Clues.CultureID IN (1);

受影响的行:0
时间:3.815ms

[SQL] 
SELECT Clues.* FROM Clues INNER JOIN SolutionWords 
ON SolutionWords.SolutionWordID = Clues.SolutionWordID  
WHERE SolutionWord COLLATE utf8_general_ci = 'TEST D' 
AND Clues.CultureID IN (1,2);

受影响的行数:0
时间:0.142ms

SELECT Clues.* FROM Clues INNER JOIN SolutionWords 
ON SolutionWords.SolutionWordID = Clues.SolutionWordID  
WHERE SolutionWord COLLATE utf8_general_ci = 'TEST E' 
AND Clues.CultureID IN (1);

受影响的行:0
时间:3.843ms

SELECT Clues.* FROM Clues INNER JOIN SolutionWords 
ON SolutionWords.SolutionWordID = Clues.SolutionWordID  
WHERE SolutionWord COLLATE utf8_general_ci = 'TEST F' 
AND Clues.CultureID IN (1,2);

受影响的行数:0
时间:0.142ms

编辑:更改参数顺序-> 无效

**[SQL] SELECT Clues.* FROM Clues INNER JOIN SolutionWords ON SolutionWords.SolutionWordID = Clues.SolutionWordID  WHERE Clues.CultureID IN (1) AND SolutionWord COLLATE utf8_general_ci = 'TEST 1' ;
Affected rows: 0
Time: 3.833ms**

[SQL] 
SELECT Clues.* FROM Clues INNER JOIN SolutionWords ON SolutionWords.SolutionWordID = Clues.SolutionWordID  WHERE SolutionWord COLLATE utf8_general_ci = 'TEST 2' AND Clues.CultureID IN (1,2);
Affected rows: 0
Time: 0.141ms

[SQL] 
SELECT Clues.* FROM Clues INNER JOIN SolutionWords ON SolutionWords.SolutionWordID = Clues.SolutionWordID  WHERE SolutionWord COLLATE utf8_general_ci = 'TEST 3' AND Clues.CultureID IN (1);
Affected rows: 0
Time: 3.821ms

[SQL] 
SELECT Clues.* FROM Clues INNER JOIN SolutionWords ON SolutionWords.SolutionWordID = Clues.SolutionWordID  WHERE SolutionWord COLLATE utf8_general_ci = 'TEST 4' AND Clues.CultureID IN (1,2);
Affected rows: 0
Time: 0.141ms

[SQL] 
SELECT Clues.* FROM Clues INNER JOIN SolutionWords ON SolutionWords.SolutionWordID = Clues.SolutionWordID  WHERE SolutionWord COLLATE utf8_general_ci = 'TEST 5' AND Clues.CultureID IN (1);
Affected rows: 0
Time: 3.848ms

[SQL] 
SELECT Clues.* FROM Clues INNER JOIN SolutionWords ON SolutionWords.SolutionWordID = Clues.SolutionWordID  WHERE SolutionWord COLLATE utf8_general_ci = 'TEST 6' AND Clues.CultureID IN (1,2);
Affected rows: 0
Time: 0.142ms


EDIT: EXPLAIN for slow query (actually, it's EXACTLY the same for the fast one as well unless I'm missing something obvious)

1   SIMPLE  Clues   ref fk_Clues_Cultures1,fk_Clues_SolutionWords1  fk_Clues_Cultures1  5   const   371462  Using where
1   SIMPLE  SolutionWords   eq_ref  PRIMARY PRIMARY 4   ClueExplorer.Clues.SolutionWordID   1   Using where

1   SIMPLE  Clues   ref fk_Clues_Cultures1,fk_Clues_SolutionWords1  fk_Clues_Cultures1  5   const   371462  Using where
1   SIMPLE  SolutionWords   eq_ref  PRIMARY PRIMARY 4   ClueExplorer.Clues.SolutionWordID   1   Using where

编辑:显示创建表

CREATE TABLE `Clues` (
  `ClueID` int(11) NOT NULL AUTO_INCREMENT,
  `SolutionWordID` int(11) NOT NULL,
  `Clue` text CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
  `SolutionWordBreakup` varchar(45) DEFAULT NULL,
  `SpellingID` int(11) DEFAULT NULL,
  `WordFormID` int(11) DEFAULT NULL,
  `GrammaticalFormID` int(11) DEFAULT NULL,
  `ClueCategoryID` int(11) DEFAULT NULL,
  `ClueFormatID` int(11) DEFAULT NULL,
  `ClueStyleID` int(11) DEFAULT NULL,
  `CultureID` int(11) DEFAULT NULL,
  `Difficulty` int(11) DEFAULT NULL,
  `Interestingness` int(11) DEFAULT NULL,
  `ReviewDate` datetime DEFAULT NULL,
  `Explanation` text,
  `Citation` text,
  `RelevantFrom` datetime DEFAULT NULL,
  `RelevantTo` datetime DEFAULT NULL,
  PRIMARY KEY (`ClueID`),
  KEY `fk_Clues_Spellings` (`SpellingID`),
  KEY `fk_Clues_WordForms1` (`WordFormID`),
  KEY `fk_Clues_GrammaticalForms1` (`GrammaticalFormID`),
  KEY `fk_Clues_ClueFormats1` (`ClueFormatID`),
  KEY `fk_Clues_ClueStyles1` (`ClueStyleID`),
  KEY `fk_Clues_Cultures1` (`CultureID`),
  KEY `fk_Clues_ClueCategories1` (`ClueCategoryID`),
  KEY `fk_Clues_SolutionWords1` (`SolutionWordID`),
  CONSTRAINT `fk_Clues_ClueCategories1` FOREIGN KEY (`ClueCategoryID`) REFERENCES `ClueCategories` (`ClueCategoryID`) ON DELETE CASCADE ON UPDATE CASCADE,
  CONSTRAINT `fk_Clues_SolutionWords1` FOREIGN KEY (`SolutionWordID`) REFERENCES `SolutionWords` (`SolutionWordID`) ON DELETE CASCADE ON UPDATE CASCADE,
  CONSTRAINT `fk_Clues_ClueFormats1` FOREIGN KEY (`ClueFormatID`) REFERENCES `ClueFormats` (`ClueFormatID`) ON DELETE CASCADE ON UPDATE CASCADE,
  CONSTRAINT `fk_Clues_ClueStyles1` FOREIGN KEY (`ClueStyleID`) REFERENCES `ClueStyles` (`ClueStyleID`) ON DELETE CASCADE ON UPDATE CASCADE,
  CONSTRAINT `fk_Clues_Cultures1` FOREIGN KEY (`CultureID`) REFERENCES `Cultures` (`CultureID`) ON DELETE CASCADE ON UPDATE CASCADE,
  CONSTRAINT `fk_Clues_GrammaticalForms1` FOREIGN KEY (`GrammaticalFormID`) REFERENCES `GrammaticalForms` (`GrammaticalFormID`) ON DELETE CASCADE ON UPDATE CASCADE,
  CONSTRAINT `fk_Clues_Spellings` FOREIGN KEY (`SpellingID`) REFERENCES `Spellings` (`SpellingID`) ON DELETE CASCADE ON UPDATE CASCADE,
  CONSTRAINT `fk_Clues_WordForms1` FOREIGN KEY (`WordFormID`) REFERENCES `WordForms` (`WordFormID`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=2458300 DEFAULT CHARSET=utf8
4

1 回答 1

1

马特,您是否认为您的问题可能与您的 where 子句中的列顺序有关?您能否尝试(例如):

...WHERE Clues.CultureID IN (1,2) 
     AND SolutionWord COLLATE utf8_general_ci = 'STRING'

看看情况如何。

雪儿,

雷纳托

于 2013-05-14T04:28:12.077 回答