下面是該查詢MySQL的 - 優化「使用臨時」引發的ORDER BY
SELECT * FROM ProductReviews
INNER JOIN RatingActions USING(RatingActionID)
LEFT JOIN ProductRatingVotes USING(RatingActionID)
WHERE ProductReviews.ProductID="200129" AND ProductReviewStatus="1"
ORDER BY RatingActionTimestamp DESC;
這裏是執行計劃
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: ProductReviews
type: ref
possible_keys: FK_ProductReview_ProductID,FK_ProductReviews_RatingActionID
key: FK_ProductReview_ProductID
key_len: 4
ref: const
rows: 1
Extra: Using where; Using temporary; Using filesort
*************************** 2. row ***************************
id: 1
select_type: SIMPLE
table: ProductRatingVotes
type: ref
possible_keys: FK_ProductRatingVotes_RatingActionID
key: FK_ProductRatingVotes_RatingActionID
key_len: 4
ref: scart.ProductReviews.RatingActionID
rows: 1
Extra:
*************************** 3. row ***************************
id: 1
select_type: SIMPLE
table: RatingActions
type: eq_ref
possible_keys: PRIMARY
key: PRIMARY
key_len: 4
ref: scart.ProductReviews.RatingActionID
rows: 1
Extra:
3 rows in set (0.00 sec)
雖然它的掃描只有一個排,using temporary
殺死它,它需要3-4秒才能完成(一個非常繁忙的服務器;在我的本地主機上0.004秒,與沒有排序的版本相比,仍然是6倍慢)。
據我所知,using temporary
是由於order by
列不在第一個表中。
有沒有一種方法來優化此查詢,或者我應該將Timestamp複製到ProductReviews表中?
UPDATE 表:
CREATE TABLE `ProductReviews` (
`ProductReviewID` int(10) unsigned NOT NULL AUTO_INCREMENT,
`ProductID` int(10) unsigned NOT NULL DEFAULT '0',
`RatingActionID` int(10) unsigned NOT NULL DEFAULT '0',
`ProductReviewText` text NOT NULL,
`ProductReviewStatus` tinyint(3) unsigned NOT NULL DEFAULT '0',
PRIMARY KEY (`ProductReviewID`),
KEY `FK_ProductReview_ProductID` (`ProductID`),
KEY `FK_ProductReviews_RatingActionID` (`RatingActionID`),
CONSTRAINT `FK_ProductReviews_RatingActionID` FOREIGN KEY (`RatingActionID`) REFERENCES `ratingactions` (`RatingActionID`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `FK_ProductReview_ProductID` FOREIGN KEY (`ProductID`) REFERENCES `products` (`ProductID`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=66 DEFAULT CHARSET=utf8
CREATE TABLE `ratingactions` (
`RatingActionID` int(10) unsigned NOT NULL AUTO_INCREMENT,
`RatingActionTimestamp` int(10) unsigned NOT NULL DEFAULT '0',
`CustomerID` int(10) unsigned DEFAULT NULL,
`RatingActionIPAddress` int(10) unsigned NOT NULL DEFAULT '0',
`RatingActionInputName` varchar(255) NOT NULL DEFAULT '',
PRIMARY KEY (`RatingActionID`),
KEY `FK_RatingActions_CustomerID` (`CustomerID`),
CONSTRAINT `FK_RatingActions_CustomerID` FOREIGN KEY (`CustomerID`) REFERENCES `customers` (`CustomerID`) ON DELETE SET NULL ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=142 DEFAULT CHARSET=utf8
CREATE TABLE `ProductRatingVotes` (
`ProductRatingVoteID` int(10) unsigned NOT NULL AUTO_INCREMENT,
`ProductID` int(10) unsigned NOT NULL DEFAULT '0',
`RatingActionID` int(10) unsigned NOT NULL DEFAULT '0',
`ProductRatingVoteValue` tinyint(3) unsigned NOT NULL DEFAULT '0',
`ProductRatingVoteStatus` tinyint(3) unsigned NOT NULL DEFAULT '0',
PRIMARY KEY (`ProductRatingVoteID`),
KEY `FK_ProductRatingVotes_ProductID` (`ProductID`),
KEY `FK_ProductRatingVotes_RatingActionID` (`RatingActionID`),
CONSTRAINT `FK_ProductRatingVotes_ProductID` FOREIGN KEY (`ProductID`) REFERENCES
`products` (`ProductID`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `FK_ProductRatingVotes_RatingActionID` FOREIGN KEY
(`RatingActionID`) REFERENCES `ratingactions` (`RatingActionID`) ON
DELETE CASCADE ON UPDATE CASCADE)
ENGINE=InnoDB AUTO_INCREMENT=142
DEFAULT CHARSET=utf8
你確定你需要所有列作爲結果? – newtover
將'*'改爲一個整數列 - 查詢速度稍快,但執行計劃沒有改變 –
順便說一下,根據執行計劃,您的命令由IS在第一個表中執行。所以你說,'SELECT RatingActions.RatingActionID FROM ...'在生產中幾乎與'SELECT * ...'同時存在,不是嗎? – newtover