query rpmdb directly

Last month, I came across a nice little command line tool that displayed basic system information called afetch. This program aims to be a fast and more minimal option versus screenfetch or neofetch.

$ ./afetch 
     /   __)\       USER jorp
     |  /  \ \    DISTRO Fedora
  ___|  |__/ /    KERNEL 5.10.13-200.fc33.x86_64
 / (_    _)_/     UPTIME 237h 39m
/ /  |  |          SHELL bash
\ \__/  |           PKGS 3686 

However, with all these programs, I noticed that execution time seemed to take a long time on Fedora.

Why? Well I started to dig through some neofetch source code and discovered that the default method for counting installed packages is by counting the number of lines outputted by rpm -qa. In my testing, this operation took an average of 2 seconds. afetch used this same method as well.

So, there’s got to be a faster way, right?

Yes. (That I have only tested with Fedora, I’m unsure of file locations on other RPM-based distributions).

Let’s query the RPM database directly with sqlite:

sqlite3 /var/lib/rpm/rpmdb.sqlite "select * from Name"

This operation seems to take 0.02 seconds on average:

$ time rpm -qa | wc -l

real	0m1.967s
user	0m1.858s
sys	0m0.126s

$ time sqlite3 /var/lib/rpm/rpmdb.sqlite "select * from Name" | wc -l

real	0m0.022s
user	0m0.010s
sys	0m0.016s

.. or about 89x faster.. hmm.

This is defnitely a neat little hack, but is probably unwise to implement in software since it’s more likely that the location of rpmdb.sqlite will change before functionality of the rpm package. (Although, you can try and check to see if the database exists in that location first).