What is database optimization doing? Why is it so long?

This optimizations checks all existing databases and shrink them by removing unused space and reorganizes indexes making access much faster.

First run might be very slow as optmizations are never run on Android by default.

 

The optimization will always show a progress dialog or notification, even if defined to start on boot because it can make the phone quite slow while working. That way you can cancel it at any time should it prevent you from using the phone as usual.