This because if there are too much entries in the database it can take long time to loop on every one.
I have no experience with databases
If you don't know anything about databases, how do you know if the one you're using is too slow in this case?

More to the point: why are you trying to code a particular solution if you know nothing about how to implement the solution you've chosen?