How to make MySQL use functional index on a datetime column?

我的未来我决定 提交于 2020-12-16 02:23:05

问题


Say I'm running MySQL 8 with a table data containing about 1M rows. And I want to filter a datetime column on date a range (using a date index).

CREATE TABLE `data` (
  `rowId` int NOT NULL AUTO_INCREMENT,
  `data` json NOT NULL,
  `created` DATETIME NOT NULL,                               -- <-- datetime column for a date index
  `created_date` DATE AS (cast(`created` as date)) NOT NULL, -- <-- generated date column
  PRIMARY KEY (`rowId`),
  INDEX (`created`),                                         -- <-- datetime index w/ cardinality ~ 1M 
  INDEX (`created_date`)                                     -- <-- date index w/ cardinality of 1250
  INDEX `created_cast` ((cast(`created` as date)))           -- <-- functional "date" index w/ cardinality of 1250
  --     ^ I WANT TO USE THIS INDEX, BUT DON'T KNOW HOW
) ENGINE=InnoDB;

Then let's filter rows only from 2018, let's say:

SELECT COUNT(*) FROM data
WHERE created >= CAST('2018-01-01' AS DATE) AND created < CAST('2019-01-01' AS DATE);
-- Query time: 0.16 s
-- EXPLAIN shows: key: created, Using where; Using index
-- Uses the whole datetime index w/ cardinality of ~ 1M 


SELECT COUNT(*) FROM data 
WHERE created_date BETWEEN CAST('2018-01-01' AS DATE) AND CAST('2018-12-31' AS DATE);
-- Query time: 0.09 s
-- EXPLAIN shows: key: created_date, Using where; Using index
-- Uses the date index w/ cardinality of 1250


SELECT COUNT(*) FROM data
WHERE CAST(created AS DATE) BETWEEN CAST('2018-01-01' AS DATE) AND CAST('2018-12-31' AS DATE);
-- Query time: 0.35 s, that's slow!
-- EXPLAIN shows: key: NULL, Using where
-- Doesn't use any index at all! 

Is there a way to use this functional index?

The queries above use either created (datetime) or created_date (date from generated column) indexes.

Is there a way to use functional index created_cast directly? I've even tried with USE INDEX or FORCE INDEX, but no luck.

Thank you all :)


回答1:


I would rewrite your queries and remove the CAST and use the he generated column, as you have no conversion in it, but it needs space.

CREATE TABLE data (created datetime,
`created_date` DATE AS (cast(`created` as date)) NOT NULL,  INDEX testin (`created`),                                         -- <-- datetime index w/ cardinality ~ 1M 
  INDEX (`created_date`))
INSERt INTO data (created) VALUEs('2018-02-01'),('2018-02-01'),('2018-02-01'),('2018-02-01')
    SELECT COUNT(*) FROM data
    WHERE YEAR(`created`) = 2018;
    
| COUNT(*) |
| -------: |
|        4 |
    SELECT COUNT(*) FROM data FORCE INDEX (testin)
    WHERE DATE(created) BETWEEN '2018-01-01' AND '2018-12-31';
| COUNT(*) |
| -------: |
|        4 |
    EXPLAIN SELECT COUNT(*) FROM data FORCE INDEX (testin) 
    WHERE DATE(created) BETWEEN '2018-01-01' AND '2018-12-31'
id | select_type | table | partitions | type  | possible_keys | key    | key_len | ref  | rows | filtered | Extra                   
-: | :---------- | :---- | :--------- | :---- | :------------ | :----- | :------ | :--- | ---: | -------: | :-----------------------
 1 | SIMPLE      | data  | null       | index | null          | testin | 6       | null |    4 |   100.00 | Using where; Using index
    SELECT COUNT(*) FROM data 
    WHERE created BETWEEN '2018-01-01 00:00:00' AND '2018-12-31 23:49:59';
| COUNT(*) |
| -------: |
|        4 |
EXPLAIN     SELECT COUNT(*) FROM data 
    WHERE created BETWEEN '2018-01-01 00:00:00' AND '2018-12-31 23:49:59';
id | select_type | table | partitions | type  | possible_keys | key    | key_len | ref  | rows | filtered | Extra                   
-: | :---------- | :---- | :--------- | :---- | :------------ | :----- | :------ | :--- | ---: | -------: | :-----------------------
 1 | SIMPLE      | data  | null       | index | testin        | testin | 6       | null |    4 |   100.00 | Using where; Using index

db<>fiddle here



来源:https://stackoverflow.com/questions/65162398/how-to-make-mysql-use-functional-index-on-a-datetime-column

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!