I know this question has been asked many times before, but I haven’t found an answer that addresses my specific case.
Using PHP & MySQL…
I’m about to add user profile pics to an app and as I see it I have several options. Two of which are as follows:
1. Make no reference to the image in the database and store the images
like so:-
-
/assets/avatars/32px/userid.jpg
-
/assets/avatars/90px/userid.jpg
- /assets/avatars/256px/userid.jpg
This would mean no extra database lookups as I will already have the relevant userid.
1. Store the relative path in the users_meta table and retrieve the url’s using a join when I retrieve user data.
What are the pro’s con’s of each method? And if in your opinion, neither method is the best way, what would you consider to be the most efficient and scaleable way of storing/referencing and retrieving user profile pics.
Many thanks
EDIT: To add some more detail :
My concern with option number 1 is that I am unsure how the filesystem would cope with 1 million+ files in a single directory. Would it slow to a crawl? If so how could I work around this? Is there a structure that would eliminate this potential issue? Perhaps using the time a user joined as part of the url and storing the images by the month.
e.g. /assets/avatars/10_2012/small/userid.jpg
with 10_2012 meaning all users who registered in October of 2012.
Or alternatively store only 1000 images in each directory and access the like so:
- /assets/avatars/0/small/userid.jpg
- /assets/avatars/1000/small/userid.jpg
- /assets/avatars/2000/small/userid.jpg
Adding a new directory every time the app reaches 1000 * n users registered.
Would this offer an advantage?
As you correctly noted – in case of using file system for your needs you don’t have to do an extra DB lookup. This saves you time and resources.
The only benefit of using a DB for this that I can see is a “single-point” back-up. But I doubt it’s a very valid “pro” or if it even outweighs the extra complexity introduced by an extra level of indirection caused by a DB lookup
Edit (to match the edit in the question): The way file systems work greatly depends on the operating system. What is your target platform?