Today I was faced with the issue of search efficiency for large sets, and I did everything possible to reduce it to the most basic business. I feel that things like this are probably related to some kind of classic problem or basic concept that I am missing, so a pointer to that would be great.
Suppose I have a table definition like
CREATE TABLE foo(
id int,
type bool,
reference int,
PRIMARY KEY(id),
FOREIGN KEY(reference) REFERENCES foo(id),
UNIQUE KEY(reference)
) Engine=InnoDB;
It is filled with n lines, where n / 2 is randomly assigned type = 1. Each line refers to another with the same type , with the exception of the first, which has a link = null.
Now we want to print all elements with type 1. I assume that at some point it will recursively call something like
function printFoo1($ref){
if($ref==null)
return;
$q = 'SELECT id, reference FROM foo WHERE id='.$ref;
$arr = mysql_fetch_array( mysql_query($q) );
echo $arr[0];
printFoo1($arr[1]);
}
Unlike
function printFoo2($ref){
$q = 'SELECT id FROM foo WHERE type=1';
$res = mysql_query($q);
while( $id = mysql_fetch_array($res) ){
echo $id[0];
}
}
, 1 "id", , 2 n/2 , , , SELECT.
? , , 1 2?