I am using sqlite3 via sqlalchemy package in Python 3.7. The SQLite database file is stored on a USB 2.0 flash drive, plugged in a MacBook Pro.
Even though many times I don't have problems, but sometimes I see a write operation (for example, a transaction of updating 2 tables in a commit) would be very slow, e.g. 8 seconds. This happens frequent enough that I have to debug it.  In good times, the same operation takes less than 0.5 second.
My question is: how can I distinguish where this slowness is in sqlalchemy or in sqlite3 or just the USB driver itself? I was not able to find an existing post with such problem.
(I was wondering if I should replace USB 2.0 drive with USB 3.0 drive, maybe because USB 2.0 is half-duplex? But I'm not sure and wanted to see if any way to confirm).
Finally I went out and bought a USB 3.1 drive (SanDisk), so with nothing else changed,  the performance is greatly improved:  the max latency is reduced from 12 seconds to 0.25 second. Looks like USB 2.0 drive was the main cause of the slowness. (Sqlite3 WAL mode helped a bit too). Thanks for comments from @Shawn and @Selcuk.
sqlite3 via sqlalchemy write operation is slow on USB drive