0

I want to use long timestamp value(may be generated by System.currentTimeInMillis()) as column names in my database. Can System.currentTimeInMillis() method guarantee an always increasing values ?? I have seen people complaining that sometimes it became slower.. !

I am also open to other alternatives that may be considerable for putting as increasing column names. I just want to guarantee uniqueness(until they fall in same millisecond when I can consider them ok..) & increasing sequence ( may be also perhaps smaller in size (less bytes) if anyhow possible!).

Edit: I have a NoSQL database where column names(& hence columns) are sorted in a row as ascending/descending number sequence. Thus I am looking to generate timestamps as column names that could enable me to sort the columns by time.

I am looking to store comments of a blog post in a single row using timestamp values as column names to enable sort by time. I think I wouldnt mind even if 10 ms is the resolution since probablity of someone commenting in the same 1/100 of a sec on the same blog post on my application would be very low.

Edit: Thank you all for your comments and suggestions. Really helpful.. I think I have got a solution to work around the problems of seldom failures of System.currentTimeInMillis(). I could implement like this:-

  • When a user adds a new comment to a post, the frontend with send an id 'suggestedId' which is one greater than the id of last comment( frontend would know about this from the previous database read). This id would be compared with the id generated using System.nanotime(). if the suggestedId is less than the generatedId then generatedId will be used else suggestedId would be used. So it simply means whatever is greater, use that Id. This guarantees monotonocity

Although not truly perfect but yes sounds good for practical usage! Would you guys like to share your thoughts upon this? Thanks!!!

Rajat Gupta
  • 25,853
  • 63
  • 179
  • 294

5 Answers5

2

The general database design issues have been addressed by other commenters, but just on this point:

Can System.currentTimeInMillis() method guarantee an always increasing values ?? I have seen people complaining that sometimes it became slower.. !

For future reference, the word for this (always-increasing values) is monotonicity. No, System.currentTimeMillis() is not monotonic. Not only can it go more slowly, or speed up (if, say, the System it's running on is using NTP for time correction), but it can arbitrarily change up or down (if the user, or a script, changes the system time).

System.nanoTime() does not formally guarantee monotonicity; however, the Hotspot JVM does if and only if the underlying system supports it (modern Linux kernels on modern hardware certainly do). Sounds better - with the caveat that some processors use power management techniques etc which can screw this up in the presence of multiple cores. So it's better, but still not perfect.

Cowan
  • 37,227
  • 11
  • 66
  • 65
  • Thanks Cowan!! How do time UUIDs get the system time, I guess they too use the system clock, hence if system time changes both the timeUUID and currentTimeinMillis would fail I guess.. Or do the timeUUIds use some better strategy/resolution..? – Rajat Gupta Mar 06 '11 at 16:04
  • also added to the question, my recent thoughts on a new strategy for implementing with system time. – Rajat Gupta Mar 06 '11 at 16:38
1

On many systems, System.currentTimeMillis() does not resolve below 10 ms increments. So two different calls can easily return the same value.

I suggest that you keep an auxiliary table with a counter that you can increment to give the next value.

Why do you want this for column names? It seems a very odd sort of data base design.

Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
  • I still think that a separate key that maps to a counter row is a better option for unique names. Doesn't Cassandra have built-in column sorting by time? – Ted Hopp Mar 06 '11 at 06:40
  • The techniques discussed in [this blog post](http://www.ayogo.com/techblog/2010/04/sorting-in-cassandra/) may work for you. The discussion about using TimeUUIDs seems particularly relevant to your application. – Ted Hopp Mar 06 '11 at 17:41
0

I am looking to store comments of a blog post in a single row using timestamp values as column names to enable sort by time.

I'm no NoSQL expert, but I'd say it's not a good idea to store comments as columns in one row. Why don't you add a row per comments along with a timestamp you can sort by?

Using a traditional relational database the table could look like this:

comments
--------
id (PK)
blog_id (FK)
created_on (timestamp)
text

Selecting the comments in order would then be in SQL:

SELECT * from comments WHERE blog_id = ? ORDER BY created_on
Thomas
  • 87,414
  • 12
  • 119
  • 157
  • Could you kindly provide a reason why you consider storing comments *in a single row* is not a good solution in case of NoSQL databases ? Thanks. – Rajat Gupta Mar 06 '11 at 05:30
  • As I said, I'm no expert in NoSQL databases, so I take my experience from traditional relational databases. One reason would be that you might have to read the entire row instead of just the portion you are interested in, if you want to display - let's say comments 20 to 30. Another reason could be comparability between objects. – Thomas Mar 06 '11 at 05:49
  • If you want to use multiple columns, maybe you then should use the same names but either use 2 columns or a combined structured column that contains timestamp and text. (like: "comment" : ["1299390767", "here's some text"]) – Thomas Mar 06 '11 at 05:50
  • `combined structured column that contains timestamp and text.` --yes Cassandra column is not just a single field, it is a structured one containing columnName(defined per single column), columnValue(defined per single column),Clock(timestamp)(defined per single column) – Rajat Gupta Mar 06 '11 at 05:55
0

System.currentTimeMillis() typically has around 10-20ms granularity, but even if it had 1ms granularity, in principle, 1ms is an eternity in computing time and it would be quite plausible, depending on what you're doing, for two calls to end up with the same value. However, I'm guessing that even 20ms is probably not an eternity compared to how frequently people make blog comments.

So, if two people post a comment within the same 20ms (or whatever), just sorting on this value will not define an order for the posts in question. But do you particularly care about this unlikely situation. If you do, then you need to build in a little bit of extra logic (have a counter for the number of messages posted "this millisecond"). I personally wouldn't bother in your use case.

As far as I can understand, you're also storing the data in a fundamentally silly way. Why not just have a "Comments" table with a row per comment and a single time column, which you can sort on as required.

Neil Coffey
  • 21,615
  • 7
  • 62
  • 83
  • Yes I do not really bother about the 20ms granularity but what about sometimes when it gets slower? What may that imply for my case ? – Rajat Gupta Mar 06 '11 at 05:36
  • Regarding the database design I am sure I am storing in a right manner as per what my database (Cassandra) experts suggest.. I am not using a Relational DB as you people are misundertanding my situation. Thanks anyways for awaring me :) – Rajat Gupta Mar 06 '11 at 05:40
  • The granularity of System.currentTimeMillis() is generally governed by the granularity of a hardware clock on your system (I think that usually, the one that is least expensive to read on the architecture in question is chosen). Whilst a granularity of 10-20ms is typical, I think it would be hugely unlikely for the granularity to be much worse than that. Anyway, that shouldn't be a mystery: you can just test it on your server and see what the granularity is, can't you? – Neil Coffey Mar 06 '11 at 14:51
-1

Many databases provide a way to get serial numbers into column. For example see this -- PostgreSQL Autoincrement

Community
  • 1
  • 1
Jayan
  • 18,003
  • 15
  • 89
  • 143