什么是最适合我的场景
我有一个表,有将近2000万条记录,基本上存储了用户在网站上做了什么
id
-> primary int 11 auto incrementuser_id
-> index int 11 not nullcreate_date
->(还没有索引)日期时间不为空- 它还有其他列,但在这里命名它们似乎无关紧要
我知道我必须在 create_date 上放置一个索引,但是我是放置一个单列索引还是一个双列,哪个先放在双索引上(考虑到大量记录)?
顺便说一句,我现在使用的查询是这样的:
select max(id) -- in here I'm selecting actions that users have done, after this date, since date is today
from table t
where
t.create_date >= '2014-12-29 00:00:00'
group by t.user_id
最佳答案
您能否使用您的 SELECT 的 EXPLAIN PLAN 编辑您的问题? EXPLAIN Link .同时,你可以试试这个:
- 使用您的日期字段 create_date 进行分区。 Partitions
首先使用最严格的条件构建索引。我认为在你的情况下,create_date + user_id 会更好
创建索引 index_name ON table_name ( create_date , user_id );
关于带有日期时间列的 Mysql 大表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27705458/