Working with databases can be a pain when you are designing a low-latency flow in software.
I am not a database expert, yet I have achieved good results with following ideas.
1. In-memory Database instead of RDBMS
Redis, MongoDb and many others free and paid solutions are available. Every solution addresses the same problem in a different way.
Experiment first if the run-time numbers on large insertions and searches meet your criteria.
And the database organizes the data in the way you want it. Evaluate well at design phase.
2. Configuration Parameters
Every RDBMS has some configuration parameters that can be exploited per your needs.
These days RAM is cheaper. So having a big cache size helps.
The other day, I reduced the insertion time in SQLLite by 20 times just by enabling PRAGMA synchronous=OFF
So, read the documentation well.
3. Separate thread
Mostly, INSERT into database are the slowest transactions.
Separate thread or process can be spawned to provide INSERT apis.
4. Design your tables and queries well
Use Indexes with Tables.
Design complex SQL queries properly to save run-time. You can run analyze tools available with RDBMS package for the purpose.
5. Avoid databases
Databases are required only when data is very large, relations are required between tables and you need complex queries on top of them.
In-memory data-structures are useful for in-flow systems.
mmap() may be useful for small files.