Note that there are some explanatory texts on larger screens.

plurals
  1. POIndex Columns and Order
    primarykey
    data
    text
    <p>If I have a select statement like the statement below, what order and what columns should be included in an index?</p> <pre><code>SELECT MIN(BenchmarkID), MIN(BenchmarkDateTime), Currency1, Currency2, BenchmarkType FROM Benchmark INNER JOIN MyCurrencyPairs ON Currency1 = Pair1 AND Currency2 = Pair2 WHERE BenchmarkDateTime &gt; IN_BeginningTime GROUP BY Currency1, Currency2, BenchmarkType; </code></pre> <p>Items to note:</p> <ul> <li>The Benchmark table will have billions of rows</li> <li>The MyCurrencyPairs table is a local table that will have less than 10 records</li> <li>IN_BeginningTime is a input parameter</li> <li>Columns Currency1 and Currency2 are VARCHARs</li> <li>Columns BenchmarkID and BenchmarkType are INTs</li> <li>Column BenchmarkDateTime is a datetime (hopefully that was obvious)</li> </ul> <p>I've created an index with Currency1, Currency2, BenchmarkType, BenchmarkDateTime, and BenchmarkID but I wasn't getting the speed I was wanting. Could I create a better index?</p> <hr> <p>Edit #1: Someone requested the explain results below. Let me know if anything else is needed</p> <p><img src="https://i.stack.imgur.com/QvAHM.png" alt="enter image description here"></p> <hr> <p>Edit #2: Someone requested the DDL (I'm assuming this is the create statement) for the two tables:</p> <p>(this benchmark table exists in the database)</p> <pre><code>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`) ) </code></pre> <p>(I'm creating the MyCurrencyPairs table in my routine)</p> <pre><code>CREATE TEMPORARY TABLE MyCurrencyPairs ( Pair1 VARCHAR(50), Pair2 VARCHAR(50) ) ENGINE=memory; CREATE INDEX IDX_MyCurrencyPairs ON MyCurrencyPairs (Pair1, Pair2); </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload