简体   繁体   中英

mysql indexing to a column of datetime type

Is it safe to add index to a column of type datetime for a table with million of row? Will it slow down the SELECT query if the query have datetime column in where clause like where finishedon >=DATE_SUB(NOW(), INTERVAL 2 DAY) order by table_name.finishedon desc ;

Here finishedon is a column with datetime format.

Select will be fast if indexes are there for the query you specified, please note if typecasting of DATETIME is done then no index will be used, but in this case its fine.

Please Use InnoDb engine if updates and inserts are frequent in such a large db to improve performance and avoid table level locking for smoother selects

For this query:

select table_name.*
from table_name
where finishedon >= DATE_SUB(NOW(), INTERVAL 2 DAY)
order by table_name.finishedon desc;

Then your index will help for both the where and the order by .

Note that slight changes to this query might make the index less useful for not useful at all. For instance:

where finshedon - interval 2 day >= now()
order by table_name.finishedon desc

might not use the index at all.

Similarly:

where column = 'value' and finshedon - interval 2 day >= now()
order by table_name.finishedon desc

might also make the index less useful, as might join and group by .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM