I have the following sql command which takes lot of time. I am thinking of indexing
SELECT `daily_price_history`.`high`,
`daily_price_history`.`low`,
`daily_price_history`.`open`,
`daily_price_history`.`datetime`,
`daily_price_history`.`close`,
`daily_price_history`.`creation_time`
FROM `daily_price_history`
WHERE (`daily_price_history`.`datetime_utc` BETWEEN '2015-12-04 18:43:28.710229' AND '2020-12-02 18:43:28.710229'
AND `daily_price_history`.`symbol` = 'A')
ORDER BY `daily_price_history`.`creation_time` ASC
So indexing multiple columns (datetime_utc,symbol) is the best way or index two separate columns.
My SHOW CREATE TABLE
CREATE TABLE `daily_price_history` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`symbol` varchar(50) NOT NULL,
`symbolExpectionCount` int(11) NOT NULL,
`url` longtext NOT NULL,
`volume` double NOT NULL,
`high` double NOT NULL,
`low` double NOT NULL,
`open` double NOT NULL,
`datetime` bigint(20) NOT NULL,
`datetime_utc` datetime(6) NOT NULL,
`close` double NOT NULL,
`creation_time` datetime(6) NOT NULL,
`lot_time` datetime(6) NOT NULL,
`periodType` varchar(50) NOT NULL,
`symbolId_id` int(11) NOT NULL,
PRIMARY KEY (`id`),
KEY `daily_price_history_symbolId_id_30077f62_fk_symbol_list_id` (`symbolId_id`),
KEY `datetime` (`datetime`),
CONSTRAINT `daily_price_history_symbolId_id_30077f62_fk_symbol_list_id` FOREIGN KEY (`symbolId_id`) REFERENCES `symbol_list` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=11549349 DEFAULT CHARSET=utf8mb4
I have a table called symbol_list, so here symbol is just text of symbol, whereas symbolId_id is a foreign key relation to the symbol_list