MySQL:IN(p1)的功能是否与IN(p1,p2,...)不同?

时间:2013-05-14 03:49:41

标签: mysql query-performance

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

我确保查询没有被缓存,但是,在 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

1 个答案:

答案 0 :(得分:1)

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

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

看看它是怎么回事。

Chers,

雷纳托