What do you mean you saved the files in the database? Their paths or their actual contents?
Anyway, if you want to do this efficiently, you should take a look at clustering algorithms. Start
here then move on to something more algorithmic.
Be warned though, if this is something you know nothing about, you wont do it in one afternoon.
cheers