user390480
user390480

Reputation: 1665

PHP vs MySQL getting random - query vs read from file - performance

I have 10 files containing anywhere from 50 to 250 rows each.

I need to be able to pull 1 or more random rows from different files.

I currently do this...

$lines = file($filePath);
if ($number == "1") {
    return $lines[array_rand($lines)];
}
else {
    shuffle($lines);
    return array_slice($lines, 0, $number);
}

However, I just read of a way to do the same thing using MySQL here:

https://stackoverflow.com/a/4329447/390480

Would I really have that much of a performance gain moving these 10 files to a MySQL database and doing that query instead?

Thanks!

Upvotes: 4

Views: 498

Answers (2)

aleation
aleation

Reputation: 4844

From my experience, when you don't have too much information, it's quicker to retrieve info from a file than from a database. In my case I have files where I store arrays with types of houses for example, and it was faster to get it from files. The files were about 10kb only, and the performance was 10 times faster, maybe I'm wrong with the performance improvement but definetively it was fast enough to make me delete those tables containing those rows and use the text files xD.

The best way to ensure the performance is to make use of the function microtime() and do it both ways. Then you can see the performance through your own benchmark.

I use this a lot:

$start = microtime(true);

// my code;
// If it's not a very long script, you should always put it inside a loop, let's say thousands times or more, depending on the script, because sometimes just the resources used by the system could vary the benchmark, as suggested by @Colin Morelli .

echo microtime(true) - $start;

You get the output of the microseconds elapsed.

Upvotes: 2

Kasyx
Kasyx

Reputation: 3200

Making it inside database should be faster than reading whole file and shuffling it. But you have to insert this data to database which will generate additional workload. You should check it manually, which option is faster.

BUT remember about one more thing. When you will have large files, then memory usage can be huge, so you should think about it too.

Upvotes: 0

Related Questions