[Solved] Logarithm function in sqlite query?
I really need to perform an
update on over 400,000 rows using the logarithm function. Unfortunately, the SQL logarithm function does not seem to exist in sqlite. Is there any way I can derive the logarithm function or import the
The only other way I know how to do this is I believe order O(n^2) through python. This approach will take too long (I tried, it took about and 1.5 hours to go through 6% on my slow computer).
I also found out why it took so long. The primary key in the database was not marked as a primary key. So the code that I was using followed:
for row in database: ...calculations for the row... ...sql update for the specific row which follows:... for search_row in database: if search_row[id] = row[id]: ...update values here...
Incredibly inefficient… O(n^2)
There is no built-in logarithm function; you have to define your own function.
It turns out that math functions (including logarithm) are implemented in Sqlite but disabled in standard Sqlite compilation on Ubuntu. In order to enable them you should download Sqlite sources from here (“Obtaining the code”) and compile according to the instructions (“Compiling”). The necessary option -DSQLITE_ENABLE_MATH_FUNCTIONS will be enabled by default. After compilation you will get the
sqlite3 executable in the build directory, this should be run as a local file:
$ ./sqlite3 <database-file>
BEFORE the compilation I recommend to install
readline so that history browsing (the arrows) and auto-completion work correctly in the Sqlite command line.
sudo apt install libreadline-dev
What makes you think it will be
O(n^2)? If anything, it should be linear:
Just about the only reason why it is so slow if you use auto-commit mode – this would cause SQLite to flush buffers to disk on every single UPDATE.
If you start transaction, then use SELECT / UPDATE loop, and finally commit, your update should be very fast (provided that your logarithm function is not very slow).
For anti log, use
power(10, number) function.