I have to make over 20 insert statements in one go. I am using UNIX_TIMESTAMP() there to insert seconds since epoch in my time columns.
My php timezone is UTC
So should i use $time = time() for inserting values or UNIX_TIMESTAMP() is fine.
time()
time — Return current Unix timestamp
UNIX_TIMESTAMP()
If called with no argument, returns a Unix timestamp (seconds since '1970-01-01 00:00:00' UTC) as an unsigned integer.
Looks to me they are the same thing. So I highly doubt there would be a difference noticable enough to warrant choosing one over the other just for optimization. Choose whichever makes your code easier to read and understand would be my suggestion. MySQL can handle a very large number of actions at once so doing something trivial like getting the timestamp would be almost instantaneous.
Whatever you do, decide based on functionality, not on efficiency; you probably won't even feel the difference, even with 100 insert statements.
Which option to choose should depend on your functional requirements:
If you must absolutely be certain that the value for each statement is the same, you should "cache" the value of time() and use that to do the insert; btw, this can be done either in MySQL or PHP (see below).
For all else, choose whatever you like.
If you are inserting multiple records, I hope you would consider prepared statements; doing so may actually speed up the insert statements in a more significant way, mainly thanks to the less amount of data that has to travel between code and database. For example:
SET @time = UNIX_TIMESTAMP();
PREPARE stmt FROM 'INSERT INTO mytable VALUES (@time)';
EXECUTE stmt;
EXECUTE stmt;
EXECUTE stmt;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With