Friedrich Ewald My Personal Website

How much Precision do you need?

Probably not so much, or at least less than you might think. We often need to store dates and times in a database and retrieve them later. I have seen people choosing to store those in the highest precision available. For example, in Python the precision for a datetime object is microseconds. That is one millionth of a second! For a lot of use cases, this kind of precision is not needed. For many cases, second precision is enough. Examples for use-cases that probably don’t need the highest precision are:

  • Blog posts
  • Comments
  • Social networks
  • Audit logs
Furthermore, it makes sense to use the same precision in your code as in your database. By doing so, you get the exact same date and time when you save an object to the database and when you retrieve it later. This is especially important in tests where exact equality is tested. One example is MySQL which has the DATETIME(3) type where the number denotes the sub-second precision. Defining the precision explicitly makes it not only more predictable in your code, it also avoid wasting space. As usual, it depends on the use case. It does make sense to think a little bit about the precision to avoid problems later in the development process.

About the author

is an experienced Software Engineer with a Master's degree in Computer Science. He started this website in late 2015, mostly as a digital business card. He is interested in Go, Python, Ruby, SQL- and NoSQL-databases, machine learning and AI and is experienced in building scalable, distributed systems and micro-services at multiple larger and smaller companies.