SQL Queries vs Conditions in PHP

拈花ヽ惹草 提交于 2020-01-02 13:46:09

问题


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?

  1. Getting an element from array and run simple mysql query like SELECT *...... WHERE..... (Running 1-2k sql queries)
  2. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!