EDIT:
I narrowed the mysql wait timeout to this line:
IF @resultsFound > 0 THEN INSERT INTO product_search_query (QueryText, CategoryId) VALUES (keywords, topLevelCategoryId); END IF;
Any idea why this might cause the problem? I canβt solve it!
Hi guys, I wrote a saved proc to search for products in certain categories, due to certain restrictions that I encountered, I could not do what I wanted (limiting, but at the same time returning the total number of rows found, with sorting, etc. .d.)
This meant splitting the row of category identifiers from 1,2,3 to the temporary table, and then building a full-text search query based on the sorting parameters and restrictions, executing the query string, and then selecting the total number of results.
Now, I know that I'm not a MySQL guru, very far from him, it works for me, but I keep getting timeouts with searching for products, etc., so I think this may cause some problem?
Does anyone have any ideas how I can remove this, or even do it much better than I probably don't know?
Thanks..
DELIMITER $$ DROP PROCEDURE IF EXISTS `product_search` $$ CREATE DEFINER=`root`@`localhost` PROCEDURE `product_search`(keywords text, categories text, topLevelCategoryId int, sortOrder int, startOffset int, itemsToReturn int) BEGIN declare foundPos tinyint unsigned; declare tmpTxt text; declare delimLen tinyint unsigned; declare element text; declare resultingNum int unsigned; drop temporary table if exists categoryIds; create temporary table categoryIds ( `CategoryId` int ) engine = memory; set tmpTxt = categories; set foundPos = instr(tmpTxt, ','); while foundPos <> 0 do set element = substring(tmpTxt, 1, foundPos-1); set tmpTxt = substring(tmpTxt, foundPos+1); set resultingNum = cast(trim(element) as unsigned); insert into categoryIds (`CategoryId`) values (resultingNum); set foundPos = instr(tmpTxt,','); end while; if tmpTxt <> '' then insert into categoryIds (`CategoryId`) values (tmpTxt); end if; CASE WHEN sortOrder = 0 THEN SET @sortString = "ProductResult_Relevance DESC"; WHEN sortOrder = 1 THEN SET @sortString = "ProductResult_Price ASC"; WHEN sortOrder = 2 THEN SET @sortString = "ProductResult_Price DESC"; WHEN sortOrder = 3 THEN SET @sortString = "ProductResult_StockStatus ASC"; END CASE; SET @theSelect = CONCAT(CONCAT(" SELECT SQL_CALC_FOUND_ROWS supplier.SupplierId as Supplier_SupplierId, supplier.Name as Supplier_Name, supplier.ImageName as Supplier_ImageName, product_result.ProductId as ProductResult_ProductId, product_result.SupplierId as ProductResult_SupplierId, product_result.Name as ProductResult_Name, product_result.Description as ProductResult_Description, product_result.ThumbnailUrl as ProductResult_ThumbnailUrl, product_result.Price as ProductResult_Price, product_result.DeliveryPrice as ProductResult_DeliveryPrice, product_result.StockStatus as ProductResult_StockStatus, product_result.TrackUrl as ProductResult_TrackUrl, product_result.LastUpdated as ProductResult_LastUpdated, MATCH(product_result.Name) AGAINST(?) AS ProductResult_Relevance FROM product_latest_state product_result JOIN supplier ON product_result.SupplierId = supplier.SupplierId JOIN category_product ON product_result.ProductId = category_product.ProductId WHERE MATCH(product_result.Name) AGAINST (?) AND category_product.CategoryId IN (select CategoryId from categoryIds) ORDER BY ", @sortString), " LIMIT ?, ?; "); set @keywords = keywords; set @startOffset = startOffset; set @itemsToReturn = itemsToReturn; PREPARE TheSelect FROM @theSelect; EXECUTE TheSelect USING @keywords, @keywords, @startOffset, @itemsToReturn; SET @resultsFound = FOUND_ROWS(); SELECT @resultsFound as 'TotalResults'; IF @resultsFound > 0 THEN INSERT INTO product_search_query (QueryText, CategoryId) VALUES (keywords, topLevelCategoryId); END IF; END $$ DELIMITER ;
Any help is much appreciated!