How to return multiple results from an array of options?
I have an array of data :
$ary = new array(
array("domain"=>"domain1", "username"=>"username1"),
array("domain"=>"domain1", "username"=>"username2"),
array("domain"=>"domain2", "username"=>"username3"),
);
I need to use this data to retrieve data from a MySql database with the following table structure (simplified for illustration).
domain_table
domain_id
domain
user_table
user_id
user
stuff_table
stuff_id
... details
link_table
link_id
user_id -- The user we are searching for connections on
connected_user_id -- from the array above
stuff_id
I need to fetch every row in the stuff table for a single user_id that also has a connected_user_id from the array.
I'm using PDO
There may be hundreds (possibly thousands) of entries in $ary.
I could generate a very large query by looping thorugh the array and adding loads of joins. I could perform a single query for each row in $ary. I could create a temporary table with $ary and use a join. Something else?
开发者_如何学PythonWhat is the best way - fastest processor time without being too arcane - to achieve this?
- perform a single query for each row - bed way because of small speed
- many joins better then 1.
- if it is possible - make view & use it.
If your entire dataset is HUGE and doesn't fit in memory, joins shouldn't be your choice.
Do sequential selects. Select rows from your link_table, gather user_id's out of the result in PHP. Then select rows from user_table using "where user_id in (?)". Handle grouping of results in PHP.
Even with large tables selects by key will be fast. And having 2-5 selects instead of 1 select is not a problem.
Joins will be fast while your DB fits into RAM. Then problems arise.
精彩评论