Matt
06-09-2005, 01:10 PM
Pseudo logic:
SELECT row_id, row_value FROM table WHERE first_condition = true
Evaluate row_value using advanced logic outside MySQL
Create array of desired row_id based upon row_value
SELECT row_id, row_details FROM table WHERE only rows matching array values are returned
I am looking for the most efficient way to accomplish the last step. A loop would be simple. Example #1:
$i = 1;
$row_details = array();
foreach ($array_rowids as $rowid)
{
$row_details[$i++] = $db->get_row("SELECT row_details FROM table WHERE row_id='$rowid'");
}
This would be inefficient in the context of a search engine, where hundreds or thousands of ids may exist in $array_rowids. One way to accomplish this in a single query would be Example #2:
$sql = 'SELECT row_details FROM table WHERE ';
foreach ($array_rowids as $rowid)
{
$sql .= " OR row_id='$rowid'";
}
$row_details = $db->get_results($sql);
This will result in one very large SQL query, rather than many separate ones. Still, the maximum variable character length is going to limit the number of possible results.
It would also be possible to flag those rows that you wished to return, like this Example #3:
foreach ($array_rowids as $rowid)
{
$result = $db->query("UPDATE table SET flag=1 WHERE rowid = '$rowid'");
}
$row_details = $db->get_results("SELECT row_details FROM table WHERE flag=1");
There is yet another problem here: if multiple instances of the code run at the same time, flags will not be properly set. Is there a simple way to accomplish this that I am overlooking (aside from Example #1)?
Thanks,
Matt
SELECT row_id, row_value FROM table WHERE first_condition = true
Evaluate row_value using advanced logic outside MySQL
Create array of desired row_id based upon row_value
SELECT row_id, row_details FROM table WHERE only rows matching array values are returned
I am looking for the most efficient way to accomplish the last step. A loop would be simple. Example #1:
$i = 1;
$row_details = array();
foreach ($array_rowids as $rowid)
{
$row_details[$i++] = $db->get_row("SELECT row_details FROM table WHERE row_id='$rowid'");
}
This would be inefficient in the context of a search engine, where hundreds or thousands of ids may exist in $array_rowids. One way to accomplish this in a single query would be Example #2:
$sql = 'SELECT row_details FROM table WHERE ';
foreach ($array_rowids as $rowid)
{
$sql .= " OR row_id='$rowid'";
}
$row_details = $db->get_results($sql);
This will result in one very large SQL query, rather than many separate ones. Still, the maximum variable character length is going to limit the number of possible results.
It would also be possible to flag those rows that you wished to return, like this Example #3:
foreach ($array_rowids as $rowid)
{
$result = $db->query("UPDATE table SET flag=1 WHERE rowid = '$rowid'");
}
$row_details = $db->get_results("SELECT row_details FROM table WHERE flag=1");
There is yet another problem here: if multiple instances of the code run at the same time, flags will not be properly set. Is there a simple way to accomplish this that I am overlooking (aside from Example #1)?
Thanks,
Matt