Search code examples
pythondjangopostgresqldjango-ormoverhead

Python function call vs database query (django ORM) overhead


While working in a Django project, when I see that I can reduce a database query for some cases if I use a function call (e.g datetime.today()) I opt for that to get more efficiency cause as far as I know, database query is the most expensive operation in a production environment.

Am I right about this? Think of a postgres database with hundred thousands of records, I use the datetime.today() function and check if it is today, if not then don't run the database query (filter exists() query). Doing the database query all the time serves my logical purpose too, but I'm adding the datetime function call only for efficiency purpose as doing the query only for today is enough for this case. This bit of code is inside a loop.

Will this approach be more efficient than simply doing the database query all the time?


Solution

  • Generally speaking a database query will be faster than a python function or a django one as databases are very low level compared to python.

    this is due partly due to the fact python has many different abstractions and the system has to work through them to perform the function whereas databases are all generally created with low level languages (postgresql was created in C which is commonly known as the lowest level language that is still useable for programming in).

    additionally python is not compiled and is interpreted instead meaning it is never converted to faster assembly code but bytecode that is interpreted by a Virtual machine whereas databases are made in c which compiles to Assembly which runs directly on the CPU

    If you are wanting to save time and optimize i would suggest looking at the caching framework as if you are running this query once a day you could add it to the cache with an expiry of one day and just check it

    Edit:

    checking a benchmark django ORM can manage about 2073.64 Get transactions a minute roughly 1 every 30 milliseconds whereas datetime benchmarks show it can parse in 0.01ms so in this case its probably datetime.today