Oh what I'm after is much more grizzley than that!Basically I'm working on an ecommerce website which displays a page of browse/search results on the same web page as a product detail.Here is the search results query...CREATE PROCEDURE GetSearchResults ( @Search varchar(255))ASSELECT productID, p.name, imageThumb, priceFROM products pINNER JOIN categories c ON p.categoryID = c.categoryIDWHERE ( description LIKE '%' + @Search + '%' OR p.name LIKE '%' + @Search + '%' OR c.name LIKE '%' + @Search + '%' ) AND statusID <> 2ORDER BY nameIDGO
Furthermore, on one of these pages containing search results and a product detail, there are buttons/links to enable you to move to the next or previous product detail in the search results. The links have on the query string the pk of the current product being viewed plus a flag saying whether you want the next or the prevous item.So here is a snippet from the query which returns the 'next' product given a pk.. SELECT TOP 1 @productID = productID, @name= name, @mainImage =imageMain, @price = price, @description = description, @nameID = nameID FROM products WHERE nameID > (SELECT nameID FROM products WHERE productID= @productID) AND productID in (SELECT productID FROM products p INNER JOIN categories c ON p.categoryID = c.categoryID WHERE ( description LIKE '%' + @Search + '%' OR p.name LIKE '%' + @Search + '%' OR c.name LIKE '%' + @Search + '%' ) AND statusID <> 2) ORDER BY nameID
'NameID' is a calculated field which adds the name of the product to the pk and is used to order the products alphabetically.So you see, this query looks up the nameID for the given product, looks up all the products which have a higher alphabetical order, orders them and picks up the top one. Furthermore, it makes sure its within the search results.So by now, you can see, things are getting extreme. Its already doing the search query twice per page request if you click a next/prev button - once for the page of search results and once for the product detail.But there's more....I also need to configure the next/previous links to be active or inactive depending on whether there is actually a next or a previous product. Obviously, for example, the first product from the search results doesn't have a previous product.To do this, I'm getting a count of the total products in the search results and also the total number of products which are alphabetically lower than the selected product. From this data one can determine a product's position in the search results.Thus here are the queries... SELECT @position = COUNT(*) FROM products WHERE nameID <= @nameID AND productID IN (SELECT productID FROM products p INNER JOIN categories c ON p.categoryID = c.categoryID WHERE ( description LIKE '%' + @Search + '%' OR p.name LIKE '%' + @Search + '%' OR c.name LIKE '%' + @Search + '%' ) AND statusID <> 2) SELECT @total = COUNT(*) FROM products WHERE productID IN (SELECT productID FROM products p INNER JOIN categories c ON p.categoryID = c.categoryID WHERE ( description LIKE '%' + @Search + '%' OR p.name LIKE '%' + @Search + '%' OR c.name LIKE '%' + @Search + '%' ) AND statusID <> 2)
Thus, per page request, the search query is being run 4 times. Something tells me that this is not good, although it does actually work.Can anybody get all this stuff into a single query which only does the search once?X.