Which is faster for checking, sql/database query or file_get_content?
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
If the file does not change often then you should import it into a table and create an index on the column you want to search on. A query on an indexed column should be much faster than:
- fetching an 8MB (86MB?) file over the wire
- loading all of it into memory (php has a memory limit)
- searching it using
strpos
(which only stops if it finds a match or reaches the end of string) - only to find that the value being searched does not exist