one of reasons why not store even small images in db?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
newmember
Forum Contributor
Posts: 252
Joined: Fri Apr 02, 2004 12:36 pm

one of reasons why not store even small images in db?

Post by newmember »

Hi

I need your opinion or advice on following scenario:

I'm storing thumbnails (~3kb) in database but original image(~200-500kb) are stored in file system.
When i want to display images, first i display thumbnail which links to large original image.
For minimizing db queries i'm doing this in following way:

1) i grab all thumbnails, 20-30 at once, with single query.

2) then i store them in $_SESSION['images'] array

3) script generates link that looks something like: <img src="get_image.php?k=2"> where 2 is index of that image in $_SESSION['images'] array.

get_image.php simply sends out the image according to given k


so with this approach i do only one db query per page view but at the same time script fills $_SESSION
with tens on kb of data.
What even worse is that when many users access pages with images $_SESSION will get larger and larger and probably not what i want :?

alternativly i could query db directly for each image request...but this seems even worse that first approach

so, after some thinking i see that there are 2 last approaches...

first is obvious one is just store thumbnails in file system and probably that what i'm going to do...

as last approach i thought about implementing some cache schemas...
so what really interesting is whether it is possible to improve the situation using cache files?

what do you think guys?
thanks
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

as last approach i thought about implementing some cache schemas... so what really interesting is whether it is possible to improve the situation using cache files?
It's certainly possible... but why would you 1) fetch your thumbnail from the db and store it to filesystem and 2) then serve it to the client? It seems logical to get rid of the first step altogether.
User avatar
newmember
Forum Contributor
Posts: 252
Joined: Fri Apr 02, 2004 12:36 pm

Post by newmember »

It's certainly possible... but why would you 1) fetch your thumbnail from the db and store it to filesystem and 2) then serve it to the client?
yes that would be definitly better that storing in session... :idea: i have to think about it more
what yo actually saing is "use cache file"
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: one of reasons why not store even small images in db?

Post by onion2k »

So you're storing 20 - 30 * 3kb in the session .. approx 75kb then .. and every time a user views a page that session file is being opened and parsed by PHP .. So you're parsing 75kb of data on every page viewed in order to save .. umm .. what exactly?
User avatar
newmember
Forum Contributor
Posts: 252
Joined: Fri Apr 02, 2004 12:36 pm

Post by newmember »

So you're parsing 75kb of data on every page viewed in order to save .. umm .. what exactly?
as i explained.. instead making 20-30 separate db queries for each image
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

newmember wrote: what yo actually saing is "use cache file"
what I'm actually saying is "don't store images into database"
User avatar
newmember
Forum Contributor
Posts: 252
Joined: Fri Apr 02, 2004 12:36 pm

Post by newmember »

what I'm actually saying is "don't store images into database"
yes, i hoped you'll say this :D

thanks.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

newmember wrote:as i explained.. instead making 20-30 separate db queries for each image
Store the filenames in the session then if you must. Personally .. I see nothing wrong with 30 db queries. Some of my sites query MySQL hundreds of times a second. It's not a problem.
Post Reply