[SOLVED] Which is faster for checking, sql/database query or file_get_content?

Issue

I have this type of data which i call ids

I have 8.6M of these ids which i use for "validity" check.

tt9916820
tt9916822
tt9916824
tt9916826

I was wondering which would be better & faster?

Using file_get_contents() like this:

$imdbid = $_GET['id'];
if (strpos(file_get_contents("http://127.0.0.1/ids.txt"),$imdbid) !== false) {
    echo 'valid id';
} else {
    echo 'invalid id';
}

or if i should just import all the 8.6M+ ids to my database and use sql query like this:

$imdbid = $_GET['id'];
$existing = $wpdb->get_var("SELECT COUNT(*) from wp_postmeta where dbids='$imdbid'");
if ($existing) {
    echo 'valid id';
} else {
    echo 'invalid id';
}

Which one would be faster?

Im newbie, if there are any and any better ways of doing this, please let me know as i wanted to learn more. Thank you!

Solution

With a suitable INDEX, the database can discover whether a given value is in the table, and do it in a few milliseconds.

Searching a file with 8.6 million rows (perhaps 86MB in your example) will seconds, maybe even minutes. Furthermore, your suggested code will need room to hold that entire 86MB between fetching and searching.

And, with strpos, if the ids are variable length, you could get a spurious match. For example, searching for "tt9916822" will get 3 hits, none of which are successful. The database solution will prevent this.

tt9916824
tt9916826
tt99168220
tt99168221
tt99168222

Answered By – Rick James

Answer Checked By – Timothy Miller (BugsFixing Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *