Si tengo una instrucción de selección como la siguiente declaración, ¿qué orden y qué columnas deben incluirse en un índice?Columnas de índice y pedido
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;
Los productos que tener en cuenta:
- La tabla de referencia tendrá mil millones de filas
- La tabla MyCurrencyPairs es una tabla local que tendrá menos de 10 registros
- IN_BeginningTime es un parámetro de entrada
- Columnas Currency1 y Currency2 son VARCHARs
- Columnas BenchmarkID y BenchmarkType son Intercepciones
- Columna BenchmarkDateTime es una fecha y hora (esperemos que eso era obvio)
He creado un índice con Currency1, Moneda2, BenchmarkType, BenchmarkDateTime y BenchmarkID pero que no estaba recibiendo la velocidad que dejaba que desear. ¿Podría crear un mejor índice?
Editar # 1: Alguien solicitó los resultados de explicación a continuación. Déjame saber si se necesita otra cosa
Edición # 2: Alguien pidió el DDL (estoy asumiendo que esto es la sentencia de creación) de las dos tablas:
(esto tabla de referencia existe en la base de datos)
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`)
)
(estoy creando la tabla MyCurrencyPairs en mi rutina)
CREATE TEMPORARY TABLE MyCurrencyPairs
(
Pair1 VARCHAR(50),
Pair2 VARCHAR(50)
) ENGINE=memory;
CREATE INDEX IDX_MyCurrencyPairs ON MyCurrencyPairs (Pair1, Pair2);
Puede ejecutar un EXPLAIN
Gracias por la explicación. Los campos Currency1 y 2, ¿son varchar por alguna razón? es decir, ¿contiene texto? Si es así, ¿hay alguna manera de convertirlos en búsquedas en lugar de cambiar los tipos de campo a INT? es decir, "GBP" => 1, "USD" => 2 – FreudianSlip
Lamentablemente, no ... este es un sistema que ha estado aquí por un tiempo y que sería un cambio importante. Desearía que el arquitecto original hubiera hecho eso ... ni idea de por qué tratarías de hacer coincidir todo en VARCHAR ..... – Miles