I am executing the below query to get daily user signin details from my table . I posted the sample table and the query i am executing .
Problem
My Query is not executing as range query it examines the whole table . It becomes slow for me . If I index the timestamp column is not useful because there is no major differnce between timestamp in milliseconds . I couldn’t do indexing the timestamp column for a table because my product in a production setup . It will contains millions of rows. How can execute this query as a range one or any better solutions?
MYSQL Table
+—————+—————————————————————————————————————————————————————————————+
| Table | Create Table |
+---------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| SignInDetails | CREATE TABLE `SignInDetails` (
`USER_ID` bigint(20) DEFAULT NULL,
`UserName` char(200) DEFAULT NULL,
`TIMESTAMP` bigint(20) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+---------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
Query
select USER_ID,TIMESTAMP from SignInDetails where TIMESTAMP
between UNIX_TIMESTAMP(CURRENT_DATE()-INTERVAL 1 DAY)*1000 and
UNIX_TIMESTAMP(CURRENT_DATE())*1000
Explain Output
+----+-------------+---------------+------+---------------+------+---------+------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+---------------+------+---------------+------+---------+------+------+-------------+
| 1 | SIMPLE | SignInDetails | ALL | NULL | NULL | NULL | NULL | 21 | Using where |
+----+-------------+---------------+------+---------------+------+---------+------+------+-------------+
Total Rows In a table
21
Your desc output doesn’t indicate that
timestampis indexed column. Can you provide the DDL, so that we can see that you actually creating an index for it?Alternatively, you could use 2 columns to store the timestamp with milliseconds resolution, one is used for search and the other for extra resolution. I can imagine combinations:
datetime/msec,date/bigint-msec,datetime/bigint-msec. Actual combination is going to depend on what kind of queries are used most frequently.