问题
If we are having an array of about 1000-2000 elements, and a mysql table of about 1000-2000 (can increase). We have to find that table content is in array or not.
Which is a better approach?
- Getting an element from array and run simple mysql query like
SELECT *...... WHERE.....
(Running 1-2k sql queries) - Get the table stored in mysql in other array and check it's element with other. i.e. nested for-loop type. (Running 1 sql query and then 1-2k condition checks)
回答1:
The obvious answer is: try it.
The almost obvious answer is: doing queries is often faster than iterating through arrays in php. One query is much faster than multiple queries due to less overhead.
This is where i would start:
SELECT * FROM table WHERE something IS IN ('a1', 'a2', ..., 'a1000')
回答2:
scraping data with html simple dom parser is slow, as compared with the built-in dom parser DOMDocument and DOMXPath.
searching large php arrays with 1000+ elements is going to be slower then querying a db.
it could be more efficient to bulk insert the php array to db and query in sql. bulk inserts have considerations like table type (MyISAM|InnoDB etc), disabling keys, large amounts of index's blah blah blah.
at the end you'll need to benchmark.
来源:https://stackoverflow.com/questions/19066884/sql-queries-vs-conditions-in-php