Page 1 of 1

Load Question: file_exists versus MySQL query

Posted: Thu Aug 21, 2008 9:42 pm
by JAB Creations
Which creates less load, file_exists or a MySQL query?

I figure if I execute file_exists for a single file type (GIF) versus executing it three times (GIF, JPG, PNG) it'll definitely create less load then a MySQL query. I could restrict avatar file types or simply convert them to GIF format...not sure.

As I'm only just starting to program my very first generation of PHP/MySQL based membership script (sort of a test version if you will) I'm not sure how often this will occur; I'm not sure if I will need to call a query independently if there is no other data I need to fetch from the MySQL database. I'm thinking there isn't though I could be wrong.

So worst case scenario is I think I'll always fetch more then one column/row and thus should stick the file extension in the database (only three letters for the extension itself). I'd really like to take this sort of thing in to consideration now to get in to good coding practices as I learn more. Thoughts?

Code: Select all

$image_avatar_gif = 'upload/'.get_id_length($row['id']).'_'.$row['username'].'_avatar.gif';
$image_avatar_jpg = 'upload/'.get_id_length($row['id']).'_'.$row['username'].'_avatar.jpg';
$image_avatar_png = 'upload/'.get_id_length($row['id']).'_'.$row['username'].'_avatar.png';
 
if (file_exists($image_avatar_gif)) {echo '<img class="left margin" src="'.$image_avatar_gif.'" />';}
else if (file_exists($image_avatar_jpg)) {echo '<img class="left margin" src="'.$image_avatar_jpg.'" />';}
else if (file_exists($image_avatar_png)) {echo '<img class="left margin" src="'.$image_avatar_png.'" />';}

Re: Load Question: file_exists versus MySQL query

Posted: Fri Aug 22, 2008 9:16 pm
by califdon
If I understand your question, I would think it would normal to include the full filename, including the extension, in the table. Why wouldn't you? Don't worry about the extra 4 bytes unless you are expecting to store hundreds of millions of rows! There are far more critical issues to pay attention to than 4 bytes per row.

Re: Load Question: file_exists versus MySQL query

Posted: Sat Aug 23, 2008 8:32 am
by Weirdan
From my experience, situations where you need to show only user's avatar and nothing of his other info are very rare. And if you're going to select user's data from database, why not select his avatar's filename as well?

Re: Load Question: file_exists versus MySQL query

Posted: Sat Aug 23, 2008 8:54 am
by JAB Creations
This is a load question. :|

Didn't see the second reply when I posted...

I'm trying to find a nice balance between minimal load and seeing what I can do to funnel to that goal. For example If a user uploads an avatar it could be a GIF, JPG, or PNG file. In that instance I could convert all supported file types to GIF and not even have to do half of the above code.

Re: Load Question: file_exists versus MySQL query

Posted: Sat Aug 23, 2008 4:06 pm
by califdon
I think I see what you're driving at. Instead of arbitrary filenames for avatar images submitted by users, you want to name each one the same as its user's username, but with the possibility of 3 file types. You could do that, although I'm not sure why that would be at all important. I would simply store the filename, including extension, in the user's record, as Weirdan said. I think you are worrying about something that is inconsequential, either in terms of storage or page loading time.