如何在Mysql中优化日期时间搜索? [英] How to optimize date time search in Mysql?
本文介绍了如何在Mysql中优化日期时间搜索?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的基本日期时间结构:
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
我想得到 day(date_time)
的结果'2010-10'
和 user_id ='1';
我的SQL是:
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'
但 EXPLAIN
代码显示:
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??
谢谢你非常非常!!
推荐答案
在WHERE子句的列上使用一个函数可以阻止该列上的索引的有效使用。尝试这样做:
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'
在上添加一个索引(user_id,date )
。
这篇关于如何在Mysql中优化日期时间搜索?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文