Timestamp as int field, query performance

I store the timestamp as an int field. And on a large table, it takes too long to get the rows inserted in the date, because I use the mysql function FROM_UNIXTIME.

SELECT * FROM table WHERE FROM_UNIXTIME(timestamp_field, '%Y-%m-%d') = '2010-04-04'

Is there a way to speed up this request? Maybe I should use a query for strings with timestamp_field >= x AND timestamp_field < y?

thank


EDITED This query works fine, but you have to take care of the index on timestamp_field .

SELECT * FROM table WHERE 
timestamp_field >= UNIX_TIMESTAMP('2010-04-14 00:00:00')
AND timestamp_field <= UNIX_TIMESTAMP('2010-04-14 23:59:59')
+3
source share
2 answers

Use UNIX_TIMESTAMP in a constant instead of FROM_UNIXTIME in a column:

SELECT * FROM table
WHERE timestamp_field
   BETWEEN UNIX_TIMESTAMP('2010-04-14 00:00:00')
       AND UNIX_TIMESTAMP('2010-04-14 23:59:59')

, timestamp_field, . , non sargable, ​​ FROM_UNIXTIME .

timestamp_field, .

, , SELECT *.

+3

, datetime , , , , unix .

FROM_UNIXTIME , , , , . , , , , .

, int , , < > , , , , .

0

Source: https://habr.com/ru/post/1743375/


All Articles