Question

I'm a little bit confused - I want to store the date in JavaDB/Derby database, but which data type is better to use?

I can simply store Long like System.currentTimeMillis() and save it to database in BIGINT format. Later I can format it as I want.

Or I can store it like TIMESTAMP('1962-09-23 03:23:34.234') using TIMESTAMP data type.

Which way is more efficient?

Was it helpful?

Solution

The doc for the TIMESTAMP data type and the doc for implementation notes both say that Derby uses nanosecond resolution. That suggests a 64-bit integer, but I don't know Derby’s implementation. If you truly care, look at the source code.

Why would you not use TIMESTAMP? The job of the database is to manage data, so let it. You should not be trying to jerry-rig the data types without a very important reason. A vague concern about "efficiency" is micro-managing the database server’s job.

Furthermore, your question is vague. What does "efficiency" mean? Less space used in storage? Less space used in memory? Advantages in indexing? Faster queries? Faster writes? But, again, those concerns are premature optimization (a bad thing) until you’ve identified a specific bottleneck.

There are advantages to storing a date-time value in a date-time data type, otherwise that data type would not have been added to the database’s repertoire. One important benefit of using TIMESTAMP as intended is that it knows how to parse strings as date-time values.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top