You can answer this yourself if you think about what happens when you talk to the database:
- Your program should send a request to the database. Depending on whether the database server is running in the process or somewhere else on the network, this can take from a few microseconds to several milliseconds.
- The database server should analyze your request and formulate an execution plan. Depending on the server, it may cache the execution plan for frequently executed requests. If not, plan a few more microseconds to create the plan.
- The database server must execute your plan by reading out any disk blocks needed to access the data. Each disk access will take tens of milliseconds. Depending on how large the table is and how indexed it is, your query may take a few seconds.
- The database server must pack the data and send it back to the application. Again, depending on whether it will be in the process or over the network, it will take microseconds in milliseconds, and it will depend on how much data is sent back.
- Your application should convert the extracted data into a usable form. This is probably a microsecond or less.
In comparison, searching for a hashed data structure requires several memory accesses, which can take several nanoseconds each. The difference is several orders of magnitude .
source share