Monday, 26 August 2013

Make this part of PHP code faster

Make this part of PHP code faster

I have a portion of code that isn't so optimized.
Probably a LEFT JOIN will make it faster. But I have some troubles with
this kind of advanced queries.
I would like to use the LEFT JOIN with NULL value
(http://explainextended.com/2009/09/18/not-in-vs-not-exists-vs-left-join-is-null-mysql/)
for queries into the first cicle.
This is the portion of code:
$query = "SELECT * FROM SitiWeb_Categorie WHERE MaxArticoliConLinks > 0";
$result = mysql_query($query, $db);
while($row = mysql_fetch_array($result))
{
$Disponibile = 0;
//Trovo tutti gli articoli redatti ed assegnati a questa categoria
//Verifico inoltre che non è presente nella tabella delle pubblicazioni
$query2 = "SELECT Articolisti_Articoli.ID AS ArticoloID FROM
Articolisti_Articoli, Articolisti_Incarichi WHERE
Articolisti_Incarichi.CategoriaID = '$row[ID]' AND
Articolisti_Articoli.IncaricoID = Articolisti_Incarichi.ID ORDER BY
Articolisti_Articoli.ID DESC";
$result2 = mysql_query($query2, $db);
while($row2 = mysql_fetch_array($result2))
{
$query3 = "SELECT COUNT(ID) AS Tot FROM SitiWeb_Articoli WHERE
ArticoloID = $row2[ArticoloID]";
$result3 = mysql_query($query3, $db);
$row3 = mysql_fetch_array($result3);
if($row3[Tot]==0)
{
$Disponibile++;
}
if($Disponibile==10)
{
break;
}
}
if($Disponibile<10)
{
}
}

No comments:

Post a Comment