I'm using PHP and MySQL.
I need to store a unix time stamp each time one of my users accomplish a given action. I only need an hourly detail level. Is there any reason why I shouldn't reduce storage by storing something like (integer)(time()/3600) instead of the full time stamp? I need to do multiple queries on this time stamp per session per user.
If I save the time stamp as is, I plan to store it as an INT in MySQL. I'll need to create an index combining userID and time stamp.
If I convert the time stamp into a number of hours, I can store it as a MEDIUMINT in MySQL.
Well. One reason: Unless the timestamp is saved as a string, or we are past year 2038 it will not actually use less space.
Generally this will reduce space in a database if you on a table have under 5 millons inserts of timestamp data. In other way you didn't need but for a performance save as unsigned (10) integer in database instead timestamp or date/time. This will be produce faster of indexing and searching a data.
Related
I'm trying to implement a timeseries db to store simple counters using redis (and php, but the language shouldn't be relevant i think). So I've implemented my redis keys as follows (simplified):
someprefix:YYYY-MM-DD:somecounter
Now when i want to get a range of data for a specific interval i just get all keys for the specific range and that's all working fine. (YYYY-MM-DD is the date as UTC)
Now i want to implement the ability to get data according to some timezone X.
My question is: is there any way this key schema can be used for that with any degree of accuracy?
I'm guessing not, since there's no time information at all so i'll also have to add at least the hours and minutes to the key so timezone conversion works correctly. I also probably should save the information in smaller time intervals otherwise when converting timezones there are cases where I would end up getting all data for a different day when the timezone difference shouldn't be more than 13h therefore giving me wrong results, am I right?
Would it be more appropriate to just use unix timestamps instead of the formatted date on the redis keys? For example, if I later on decide to store data with smaller precision, say per hour or per each 10 minutes, what would be a more flexible key format?
Hope I was able to explain my issue correctly, but please feel free to ask for any clarifications.
Thanks
Its always good to go with epoch (UNIX timestamp) when you have to deal with timezone.
I would suggest bucking timestamps to frame the Keys. For example an event happened at timestamp 1409800502515(Thu, 04 Sep 2014 03:15:02 GMT), you could bucket it at Hour level or Day level like this
Hour bucket = 1409800502515 - (1409800502515 % (60 * 60)) = 1409800500000
Day bucket = 1409800502515 - (1409800502515 % (24 * 60 * 60)) = 1409800464000
and frame keys like
someprefix:1409800500000:somecounter OR
someprefix:1409800464000:somecounter
For example for calculating the page views per hour, find the appropriate hourly bucket and increment the counter
mypage.html:1409800464000:page_views INCR 10
Firstly, I'm not sure how you're doing "get all keys for the specific range and that's all working fine" but if you're using KEYS someprefix:* note that this is not a recommended practice for production. Consider using the SCAN command that's available from v2.8 instead.
Secondly, you could consider using an ordered set for counting. So, following your convention, you'll have a key called someprefix:somecounter that you'll be ZADDing to members with the epoch as their score. Use the epoch and the counter's reading as a unique member name (e.g. '1409800500000:1` where 1409800500000 is the epoch and 1 is the counter's value).
Note that you can measure time resolutions from years to microseconds - it all depends on how much div you apply to the original epoch before setting the score.
I used to store dates in MySQL using TIMESTAMP value (int 15), but after reading this:
http://derickrethans.nl/storing-date-time-in-database.html
I'm confused somehow! It's really important for me to be able to show the dates for users in different time zones, and all dates are before year 2038.
Whats the best way for storing date times into MySQL db while we want to manipulate the dates in different time zones?
(please first read the above article before sending any suggestions)
I would appreciate any kind of help
This is what is suggested: Convert the dates to UTC first before storing them in the database as timestamp. Then, whenever you need to display them, just convert them on-the-fly to a user's timezone (with/without DST).
Store them in BIGINT as timestamp converted to UTC+0 timezone.
All,
I'm trying to decide how to deal with time in a project which relies on (server) time intervals (in short, some content is available after user completed a specific action at least n hours before). Right now, it seems like the easiest option would be to extract the Unix time stamp with time() and store it as is in MySQL.
Any reason why this is not a good idea? Any gotcha I need to be aware of? Performance impact?
Timestamps are fine. Don't divide them, it's unneeded calculation. If you plan to query (per object) about a timeout more often than update it then you would be better off storing the expiration time instead of the current (so calculating delta only once). Beware about DATETIME columns: they don't regard timezone setting, while your PHP does... so if you happen to have different timezone settings on different requests, then you're out of luck. Timestamps are absolute, and they also account for manace like daylight-savings times, where 3:01 is 2 minutes after 1:59...
Seems fine to me. Though you should probably store it as a DATETIME and use DateTime objects, rather than UNIX timestamps and time().
$time = new DateTime;
echo $time->format("Y-m-d H:i:s"); //Outputs current time, example: 2012-10-13 22:58:34
Actually, this is the best idea. The function time() give you the number of seconds from January 1th, 1970 00:00:00. There's no performance impact because it's only an integer. In MySQL, create a field like that INT, 10, Unsigned.
Time will give you performance on the SELECT and the WHERE. See http://gpshumano.blogs.dri.pt/2009/07/06/mysql-datetime-vs-timestamp-vs-int-performance-and-benchmarking-with-myisam/
The only problem you have is : time is limited to year 2038... but by the time 2038 come, the internal computer clock bytes will be larger ... hope so.
The other thing you may want to worrie about the DATETIME is : PHP time() run under UTC, while DATETIME depend on the timezone...
Stats when you do INSERT with 10000000 rows.
Stats when you SELECT / WHERE with indexes :
I am trying to create a script for quiz. I have stored current timestamp in MySQL DB as quiz start time, and want to check after each quiz how much time is left.
I have the idea that I will add 30 mins to saved time stamp and subtract this value from current time. That will be the time left. But I don't know the exact way of doing this.
My time stamp is saved in DB in format 2010-08-24 20:08:59. Any one have the idea.
Please let me know if someone have done it, or know how to get it.
Adding 30 mins to time stamp and showing the user how much time is left.
I am using the now() function to store the timestamp in DB.
Thanks
I would personally store the output of PHP time() in the database.
If you a human readable format from this value, you could use date('Y-m-d H:i:s', $fromdatabase);.
You want to store an actual UNIX timestamp in the database, not a string in that format.
You may or may not be doing this already, it depends on the type of column you're using. For MySQL, you should be using TIMESTAMP, which allows you to retrieve the timestamp with
SELECT UNIX_TIMESTAMP(column_name) ...
To store the current time + 30 minutes, all you have to do is:
INSERT INTO table (column_name) VALUES(UNIX_TIMESTAMP() + 1800)
You can know if the time has expired by comparing time() against the value of the column.
This question already has answers here:
Should I use the datetime or timestamp data type in MySQL?
(40 answers)
Closed 9 years ago.
Probably many coders want to ask this question. it is What's the adventages of each one of those MySQL time formats. and which one you will prefer to use it in your apps.
For me i use Unix timestamp because maybe i find it easy to convert & order records with it, and also because i never tried the DATETIME thing. but anyways i'm ready to change my mind if anyone tells me i'm wrong.
Thanks
Timestamp (both PHP ones and MySQL's ones) are stored using 32 bits (i.e. 4 bytes) integers ; which means they are limited to a date range that goes from 1970 to 2038.
DATETIME don't have that limitation -- but are stored using more bytes (8 bytes, if I'm not mistaken)
After, between storing timestamps as seen by PHP, or timestamps as seen by MySQL :
using PHP timestamps means manipulations are easier from PHP -- see Date/Time Functions
using MySQL's timestamps means manipulations are easier from MySQL -- see 11.6. Date and Time Functions
And, for more informations between MySQL's TIMESTAMP and DATETIME datatypes, see 10.3.1. The DATETIME, DATE, and TIMESTAMP Types
As others have said, timestamps can represent a smaller range of datetimes (from 1970 to 2038). However, timestamps measure the number of seconds since the Unix Epoch (1970-01-01 00:00:00 UTC), thereby making them independent of time zone, whereas DATETIME stores a date and time without a time zone. In other words, timestamps unambiguously reference a particular point in time, whereas the exact point in time a DATETIME refers to requires a time zone (which is not stored in a DATETIME field). To see why this can matter, consider what happens if we change our time zone.
Let's say we want to store the datetime 2010-03-27 12:00 UTC. If we store this and retrieve it using a timestamp or DATETIME, then there usually appears to be no difference. However, if the server now changes so that the local time zone is UTC+01, then we get two different results if we pull out the datetime.
If we'd set the field to a DATETIME, it would report the datetime as 2010-03-27 12:00, despite the change in time zone. If we'd set the field to a timestamp, the date would be reported as 2010-03-27 11:00. This isn't a problem with either datatype -- it's just a result of the fact that they store slightly different information.
That really depends. I'll give you 2 examples where one overcome the other:
Timestamp is better than DATETIME when you want to store users session in the database and the session creation time (in Timestamp format) is used for fast row retrieval (with index).
E.g. table may look like this:
[session_create_time AS Timestamp][IP_address AS 32bit Int][etc...]
Having an index on the first two columns can really speed up your queries. If you had a DATETIME value type for the session_create_time field, then it could be taken much more time. Take into account that session queries are executed each time a user request a page, so efficiency is crucial.
DATETIME is better than Timestamp when you want to store a user's date of birth or some historic events that require flexible time range.
Unless digitizing records prior to January 1, 1970, I like the UNIX epoch. Its just a matter of preference, whole unsigned numbers are simpler to deal with when using multiple languages.
Just keep in mind, the epoch starts at January 1, 1970. A lot of companies had been in business for decades, if not longer, prior to that.