如果我有像下面的语句那样的选择语句,索引中应该包括什么顺序和哪些列?索引列和顺序
SELECT MIN(BenchmarkID),
MIN(BenchmarkDateTime),
Currency1,
Currency2,
BenchmarkType
FROM Benchmark
INNER JOIN MyCurrencyPairs ON Currency1 = Pair1
AND Currency2 = Pair2
WHERE BenchmarkDateTime > IN_BeginningTime
GROUP BY Currency1, Currency2, BenchmarkType;
项注意:
- 的基准表将有几十亿的行
- 的MyCurrencyPairs表是本地表,将有不少于10条
- IN_BeginningTime是一个输入参数
- 列Currency1和Currency2是VARCHAR
- 列BenchmarkID和BenchmarkType是在INT
- 列BenchmarkDateTime是一个日期时间(希望这是显而易见的)
我创建了一个指数随CURRENCY1,Currency2,BenchmarkType,BenchmarkDateTime和BenchmarkID但我没有得到我是想的速度。我可以创建一个更好的索引吗?
编辑#1:有人请求下面的解释结果。让我知道你需要什么都
编辑#2:有人要求DDL(我假设这是CREATE语句)的两个表:
(此基准表存在于数据库中)
CREATE TABLE `benchmark` (
`SequenceNumber` INT(11) NOT NULL,
`BenchmarkType` TINYINT(3) UNSIGNED NOT NULL,
`BenchmarkDateTime` DATETIME NOT NULL,
`Identifier` CHAR(6) NOT NULL,
`Currency1` CHAR(3) NULL DEFAULT NULL,
`Currency2` CHAR(3) NULL DEFAULT NULL,
`AvgBMBid` DECIMAL(18,9) NOT NULL,
`AvgBMOffer` DECIMAL(18,9) NOT NULL,
`AvgBMMid` DECIMAL(18,9) NOT NULL,
`MedianBMBid` DECIMAL(18,9) NOT NULL,
`MedianBMOffer` DECIMAL(18,9) NOT NULL,
`OpenBMBid` DECIMAL(18,9) NOT NULL,
`ClosingBMBid` DECIMAL(18,9) NOT NULL,
`ClosingBMOffer` DECIMAL(18,9) NOT NULL,
`ClosingBMMid` DECIMAL(18,9) NOT NULL,
`LowBMBid` DECIMAL(18,9) NOT NULL,
`HighBMOffer` DECIMAL(18,9) NOT NULL,
`BMRange` DECIMAL(18,9) NOT NULL,
`BenchmarkId` INT(11) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`BenchmarkId`),
INDEX `NextBenchmarkIndex01` (`Currency1`, `Currency2`, `BenchmarkType`),
INDEX `NextBenchmarkIndex02` (`BenchmarkDateTime`, `Currency1`, `Currency2`, `BenchmarkType`, `BenchmarkId`),
INDEX `BenchmarkOptimization` (`BenchmarkType`, `BenchmarkDateTime`, `Currency1`, `Currency2`)
)
(我在我的日常创建MyCurrencyPairs表)
CREATE TEMPORARY TABLE MyCurrencyPairs
(
Pair1 VARCHAR(50),
Pair2 VARCHAR(50)
) ENGINE=memory;
CREATE INDEX IDX_MyCurrencyPairs ON MyCurrencyPairs (Pair1, Pair2);
你可以运行一个EXPLAIN
感谢您的解释。 Currency1和2字段,它们是varchar的原因?即包含文字?如果是这样,有什么办法可以将这些转换为查找,所以字段类型可以更改为INT?即“GBP”=> 1,“USD”=> 2 – FreudianSlip 2012-07-23 16:55:59
可悲的是......这是一个已经有一段时间了,这将是一个重大变化。我希望最初的架构师已经这样做了......不知道为什么你会尝试在VARCHARs上匹配所有的东西..... – Miles 2012-07-23 17:08:03