I have created an product & price function for fishing gear and i have really big problems with the peformance when doing the searches for products.
The function is found here:
http://www.fiskesnack.com/productsearch/
There are two MyISAM tables (tblPriceData & tblPriceCategory):
CREATE TABLE `tblPriceData` (
`Id` int(11) NOT NULL auto_increment,
`sShopLink` varchar(70) NOT NULL default '0',
`sProductTitle` varchar(100) NOT NULL default '0',
`sSale` varchar(5) default '0',
`dblCurrentPrice` double default NULL,
`dblOldPrice` double default NULL,
`sStockStatus` varchar(50) default NULL,
`sDirectLink` varchar(250) default '0',
`dPriceDate` datetime default '0000-00-00 00:00:00',
PRIMARY KEY (`Id`),
KEY `Test_Index1` (`sShopLink`,`sProductTitle`,`sSale`,`dblCurrentPrice`,`dblOldPrice`,`sStockStatus`,`sDirectLink`,`dPriceDate`,`Id`),
FULLTEXT KEY `Test_Index2` (`sProductTitle`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COMMENT='Contains raw data from shops';
CREATE TABLE `tblPriceCategory` (
`Id` int(11) NOT NULL auto_increment,
`sProductName` varchar(80) NOT NULL default '',
`sCategoryName` varchar(50) default NULL,
`sManufacturerName` varchar(50) default NULL,
PRIMARY KEY (`Id`),
KEY `Test_Index2` (`Id`,`sProductName`,`sCategoryName`,`sManufacturerName`),
KEY `Test_Index4` (`sCategoryName`,`sProductName`,`sManufacturerName`),
FULLTEXT KEY `Test_Index3` (`sProductName`,`sCategoryName`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
Depending on which field are choosen in the PHP form the WHERE & SORT BY selection differs.
SELECT tblPriceData.sProductTitle,tblPriceData.sShopLink,tblPriceData.sSale,tblPriceData.dblCurrentPrice,tblPriceData.dblOldPrice,tblPriceData.sStockStatus,tblPriceData.sDirectLink,tblPriceData.dPriceDate,tblPriceData.ID,tblPriceCategory.sCategoryName
FROM tblPriceData
LEFT JOIN tblPriceCategory ON tblPriceData.sProductTitle=tblPriceCategory.sProductName
$where
GROUP BY tblPriceData.sProductTitle, tblPriceData.sShopLink, tblPriceData.dblCurrentPrice
ORDER BY $sortorder1,$sortorder2,$sortorder3 LIMIT $limit OFFSET $offset
The $where variable can be either separate or in combination with an "AND" between them.
tblPriceCategory.sCategoryName LIKE '$category'
tblPriceData.sProductTitle LIKE '%$_POST[searchcriteria]%'
tblPriceData.sShopLink LIKE '$shop'
tblPriceData.dblCurrentPrice BETWEEN '$pricelow' AND '$pricehigh'
tblPriceData.sSale > $sale
ORDER BY can be combinations like this depending on what the user chooses.
ORDER BY tblPriceData.sProductTitle,tblPriceData.dblCurrentPrice,tblPriceData.sSale
ORDER BY tblPriceData.dblCurrentPrice,tblPriceData.sProductTitle,tblPriceData.sSale
ORDER BY tblPriceData.sSale,tblPriceData.dblCurrentPrice,tblPriceData.sProductTitle
Please help me with this, it is more complex than i can solve, im a beginner with Indexes and so on. :(
All improvements is good improvements. I have biggest problems with searching on all fields blank with sorting, but also big problems with categories searches and so on. please feel fre to try the function to see yourself.
Best regards from Anders Bergman