开发者

php mysql timestamp - a few questions

I have a products table to which I've added a new field to use as a timestamp. I'm not sure if it should be a bigi开发者_如何学JAVAnt field or a timestamp field though. What's the difference and which will offer the best performance?

I want to compare the current date to the timestamp value and if the difference is greater than X days (meaning that the product is no longer available) to exclude the product from my query results.

I've never used timestamps before and I need a kick-start to understand the usage, Thanks.


To store time in MySQL down to the second you may choose between

  • TIMESTAMP
  • DATETIME

Their relationship is discussed here.

But to make a long story short, a timestamp is an integer and is, if no value is provided, automatically set. so it's a good choice if you want to store the time of the last insert for example. datetime has to be set explicitely and is "optically" a string of the format 'YYYY-MM-DD HH:MM:SS'. But MySQL processes both very fast, so TS isn't faster just b/c it's an integer.

Especially if you want to operate on the time beyond <,>,= then I'd recommend DATETIME. There are a lot of functions available.


The general idea is that you use timestamp to record changes in the MySQL records since they are updated every time you update the record and you use datetime to store a particular event.

For your operations on field itself, MySQL supports functions that can do the calculations on datetime.

In terms of performance, timestamp uses more space than datetime and is generally faster.


Mysql timestamps are human readable. (Below taken from MySQL website)

E.g. TIMESTAMP columns are displayed in the same format as DATETIME columns. In other words, the display width is fixed at 19 characters, and the format is 'YYYY-MM-DD HH:MM:SS'.

Using these lets you use MySQL functions like DATE_ADD, which you can use in your queries.

Or you can use a UNIX timestamp (MySQL field type INT). Then you can do maths on the value, and then format it how you like using the PHP DATE function (second parameter is the timestamp).


FOA, we have to examine the differences between TIMESTAMP and DATETIME.

  • TIMESTAMP
    • need 4 bytes
    • '1970-01-01 00:00:01' UTC to '2038-01-19 03:14:07' UTC ( same as UNIX Epoch )
    • UTC conversion will be occurred when you read/write.
    • Table can have only one TIMESTAMP column.
  • DATETIME
    • need 8 bytes
    • '1000-01-01 00:00:00' to '9999-12-31 23:59:59
    • No conversion.
    • Table can have one or more DATETIME columns.

Usually, I use TIMESTAMP when I want to trace changes of the records ( like modified datetime ) and use DATETIME if I want to record invariable date and time. ( like created datetime )

So, If you have a plan to handle lots of records and do not need over epoch time, TIMESTAMP is smaller and faster solution. But Note that it has limitations that I mentioned above.

References:

  • http://dev.mysql.com/doc/refman/5.0/en/datetime.html
  • http://dev.mysql.com/doc/refman/5.0/en/storage-requirements.html
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜