Paul
Paul

Reputation: 11756

Whats faster file_exists or DB retrieval?

I'm currently tweaking the way our images our stored for our site. For every user I'm looking to see if they have a profile image and I"m doing this by checking to see if the file exists in their folder structure. Is this faster then storing/retrieving the name of the image in a DB table?

My current file_exists code looks like this:

$gender = ($gender == 1) ? 'female' : 'male';

$filename = SITE_ROOT . $this->img_url . $user_id . 'medium_thumb.jpg';

if (file_exists($filename)) {
    $filename = $this->img_url . $user_id . 'medium_thumb.jpg?v=' . time();
}
else {
    $filename = '/images/'.$gender.'.jpg';
}       

return $filename

Upvotes: 4

Views: 1635

Answers (6)

SDC
SDC

Reputation: 14212

I would advise you to use file_exists() even if the filename is stored fully on the database -- this will give you a reasonable fall-back when an error results in your DB not being in sync with your filesystem. It is good to have multiple levels of error handling for this kind of thing.

Given that, the question is unnecessary, since you'd use file_exists() in either case.

Furthermore, I would recommend resisting the temptation to micro-optimise your code. Unless you're making a lot of file_exists() calls, it isn't going to be making a huge difference to the speed of your program. Trying to fine-tune your performance at this level is generally unnecessary.

If you are worried about the performance of your code, use a profiling tool such as XDebug to show you where the real performance bottlenecks are. You will have some, but I guarantee they won't be in the code you're looking at here, unless it's looping.

Upvotes: 8

Omid Kamangar
Omid Kamangar

Reputation: 5788

I think, querying the database will take more time, because finally it goes to the disk too.

I mean, if you check the disk, you have one operation, but for database, you connect to the database and then it goes to the disk (where the db files are stored). So, file system should be faster (I haven't benchmarked it though!).

Upvotes: 0

Maxim Krizhanovsky
Maxim Krizhanovsky

Reputation: 26739

Do not store all images in one folder; use subfolders - otherwise there will be huge performance hit 'caused by the I/O (when you have 10k+ files will be noticable; with 100k+ files will be huge)

Make sure the image is served by a lightweight web server, for example nginx, and not from apache, as apache takes too much resources.

And now on the question. Generally file system will be faster. However, file system is harder to scale accross different servers. If you have 2 web servers for example, from which one you will serve the avatars? You need either to copy all files on all servers, or use shared disk, or use distributed file system. So you have to keep in mind not only the performance, but the horizontal scalability as well.

Also, you can use a cache for the files, such as Varnish

Upvotes: 1

Yehonatan
Yehonatan

Reputation: 3225

I think the file_exists is much more faster. While using sql you have to access drivers and such, file_exists is a system action.

Upvotes: 1

Peter
Peter

Reputation: 2556

Since the answer depends on so many variables, I would suggest running a simple test to both answer your question "which is better?" and also the related question "how much better?":

Use curl (or your favorite automated HTTP client) to hit your file-based code 1000 times and measure the time elapsed on the client side and resources consumed on the server side, and then do the same with your DB-based code.

If the values are small, run the test several times (or maybe increase the test size to 10000).

Upvotes: 0

Dr. Dan
Dr. Dan

Reputation: 2288

In my view, on the shorter run, your approach will be faster. But if you fetch the image name from database and stored in the session, then it will be a faster in the longer run since you can retrieve the value from the session each time for that page access instead of checking the file exists on the server or not.

Upvotes: 0

Related Questions