If I have a table with columns: item_id, user_id, vote, date
what columns should I put the index on considering:
I'll need to fetch votes by date for time ranges and
there cannot be more than one vote for each item by a user.
Should the index be sorted ASC or DESC if I want to fetch dates that are more recent?
PRIMARY:item_id,user_id
INDEX: date
Sort DESC for latest first
Note: I assume you are storing date as UNIX TIMESTAMP (From your question title Timestamp Indexing)
You can try the following:
CREATE INDEX mytime_index ON myTable (user_id, date);
You can't set asc
and desc
at index level as it's bydefault on asc
.
Check this out-------------- Quoted from mysql website create indices
An index_col_name specification can end with ASC or DESC. These keywords are permitted for future extensions for specifying ascending or descending index value storage. Currently, they are parsed but ignored; index values are always stored in ascending order
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.