mysql日期怎么优化_如何在Mysql中优化日期时间搜索?

bd96500e110b49cbb3cd949968f18be7.png

this is my basic date-time structure:

primary key(datetime) key

auot_id date_time user_id

1 2010-10-01 20:32:34 1

2 2010-10-02 20:32:34 1

3 2010-11-03 20:32:34 2

4 2010-10-04 20:32:34 1

5 2010-11-05 20:32:34 1

And I want to get the result of the day(date_time) at '2010-10' and the user_id = '1';

My SQL is:

SELECT * FROM datetime WHERE user_id = 1 AND DATE_FORMAT(date,'%Y-%m') = '2010-10'

but the EXPLAIN code display:

SIMPLE datetime ALL (NULL) (NULL) (NULL) (NULL) 5 Using where

so ,this line of code doesn't seem to be very effectively。How could I to construct the table to make my search more effective??

Thank you very much!!

解决方案

Using a function on a column in a WHERE clause prevents efficient usage of an index on that column. Try this instead:

SELECT *

FROM `datetime`

WHERE user_id = 1

AND `date` >= '2010-10-01' AND `date` < '2010-11-01'

Add an index on (user_id, date).

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值