chestertb
10-08-2009, 08:38 PM
Hi All,
I'm upgrading my shopping cart (php/mySQL) with real time inventory control, and am seeking advice on the efficiency of the lock process.
Each item in my cart is recorded in a file 'cart items', linked by cart number.
I query the cartitems table, and cycle through each record, looking up the item number and adjusting the stock accordingly.
The way I see it, there are two ways of doing this...
In Option 1, I lock the inventory table BEFORE I start that cycle, and unlock it when all cart items are processed, as follows;
$qri = "SELECT CIvrn, CIqty FROM cartitems WHERE CIcrn = '$invoice'";
$rsi = mysql_query($qri, $link);
//lock the table
$qrl = "LOCK TABLES inventory WRITE";
$rsl = mysql_query($qrl, $link);
while($rwi=mysql_fetch_array($rsi))
{
$srn = $rwi['CIvrn'];
$qty = $rwi['CIqty'];
if($srn!="" && $srn!=0)
{
$qrs = "UPDATE inventory SET ISinstore = ISinstore-".$qty.", ISsold = ISsold+".$qty.", ISlastsale = '$sqldate' WHERE ISrn = '$srn'";
$rss = mysql_query($qrs, $link);
}
}
//unlock the table
$qrl = "UNLOCK TABLES";
$rsl = mysql_query($qrl, $link);
The advantage of this is that there is only one lock/unlock call to mysql, but the downside may be that those queued have to wait until the whole cart is processed.
In Option 2, the table is locked for each cart item individually, then unlocked before I cycle through to the next cartitem record.
$qri = "SELECT CIvrn, CIqty FROM cartitems WHERE CIcrn = '$invoice'";
$rsi = mysql_query($qri, $link);
while($rwi=mysql_fetch_array($rsi))
{
$srn = $rwi['CIvrn'];
$qty = $rwi['CIqty'];
if($srn!="" && $srn!=0)
{
$qrl = "LOCK TABLES inventory WRITE";
$rsl = mysql_query($qrl, $link);
$qrs = "UPDATE inventory SET ISinstore = ISinstore-".$qty.", ISsold = ISsold+".$qty.", ISlastsale = '$sqldate' WHERE ISrn = '$srn'";
$rss = mysql_query($qrs, $link);
$qrl = "UNLOCK TABLES";
$rsl = mysql_query($qrl, $link);
}
}
The advantage of this is that those in line won't have to wait, and multiple carts can be processed simultaneously. The disadvantage is that for each item in the cart, the script needs to make a lock/unlock call to mySQL.
Which alternative is more efficient? Is there a 'right" way and a 'wrong' way?
Thanks
CTB
I'm upgrading my shopping cart (php/mySQL) with real time inventory control, and am seeking advice on the efficiency of the lock process.
Each item in my cart is recorded in a file 'cart items', linked by cart number.
I query the cartitems table, and cycle through each record, looking up the item number and adjusting the stock accordingly.
The way I see it, there are two ways of doing this...
In Option 1, I lock the inventory table BEFORE I start that cycle, and unlock it when all cart items are processed, as follows;
$qri = "SELECT CIvrn, CIqty FROM cartitems WHERE CIcrn = '$invoice'";
$rsi = mysql_query($qri, $link);
//lock the table
$qrl = "LOCK TABLES inventory WRITE";
$rsl = mysql_query($qrl, $link);
while($rwi=mysql_fetch_array($rsi))
{
$srn = $rwi['CIvrn'];
$qty = $rwi['CIqty'];
if($srn!="" && $srn!=0)
{
$qrs = "UPDATE inventory SET ISinstore = ISinstore-".$qty.", ISsold = ISsold+".$qty.", ISlastsale = '$sqldate' WHERE ISrn = '$srn'";
$rss = mysql_query($qrs, $link);
}
}
//unlock the table
$qrl = "UNLOCK TABLES";
$rsl = mysql_query($qrl, $link);
The advantage of this is that there is only one lock/unlock call to mysql, but the downside may be that those queued have to wait until the whole cart is processed.
In Option 2, the table is locked for each cart item individually, then unlocked before I cycle through to the next cartitem record.
$qri = "SELECT CIvrn, CIqty FROM cartitems WHERE CIcrn = '$invoice'";
$rsi = mysql_query($qri, $link);
while($rwi=mysql_fetch_array($rsi))
{
$srn = $rwi['CIvrn'];
$qty = $rwi['CIqty'];
if($srn!="" && $srn!=0)
{
$qrl = "LOCK TABLES inventory WRITE";
$rsl = mysql_query($qrl, $link);
$qrs = "UPDATE inventory SET ISinstore = ISinstore-".$qty.", ISsold = ISsold+".$qty.", ISlastsale = '$sqldate' WHERE ISrn = '$srn'";
$rss = mysql_query($qrs, $link);
$qrl = "UNLOCK TABLES";
$rsl = mysql_query($qrl, $link);
}
}
The advantage of this is that those in line won't have to wait, and multiple carts can be processed simultaneously. The disadvantage is that for each item in the cart, the script needs to make a lock/unlock call to mySQL.
Which alternative is more efficient? Is there a 'right" way and a 'wrong' way?
Thanks
CTB