All right, this one has me completely stomped. Hats off to who ever figures that one out.I have a stored procedure who received a bunch of variables and returns a count (or a result set, same thing happens).Depending on tiny changes I do, I either get the results back in a split second or in 21 seconds!! Obviously I need the results back right away.Just to illustrate the changes I am trying:This returns results in a split second DECLARE @company_id int = 10035 DECLARE @name varchar(50) = '' DECLARE @manufacturer_id varchar(50) = '%' DECLARE @manufacturer_part_number varchar(50) = '%' DECLARE @internal_part_number varchar(50) = '' DECLARE @cas_number varchar(50) = '' DECLARE @created_by varchar(50) = '%' DECLARE @archived varchar(50) = '%' DECLARE @language_id int = 1 DECLARE @site_id INT = 0 SELECT COUNT(products.product_id) AS product_count FROM products LEFT JOIN products_language ON products.product_id = products_language.product_id AND products_language.language_id = @language_id WHERE (products_language.name LIKE '%'+@name+'%' OR products.main_name LIKE '%'+@name+'%' OR products_language.synonyms LIKE '%'+@name+'%') AND products.manufacturer_id LIKE '%%' AND products.manufacturer_part_number LIKE '%' AND products.part_number LIKE '%' AND products.cas_number LIKE '%' AND products.created_by LIKE '%%' AND products.company_id=@company_id AND products.archived LIKE '%%'
This returns results in 21 seconds! DECLARE @company_id int = 10035 DECLARE @name varchar(50) = '' DECLARE @manufacturer_id varchar(50) = '%' DECLARE @manufacturer_part_number varchar(50) = '%' DECLARE @internal_part_number varchar(50) = '' DECLARE @cas_number varchar(50) = '' DECLARE @created_by varchar(50) = '%' DECLARE @archived varchar(50) = '%' DECLARE @language_id int = 1 DECLARE @site_id INT = 0 SELECT COUNT(products.product_id) AS product_count FROM products LEFT JOIN products_language ON products.product_id = products_language.product_id AND products_language.language_id = @language_id WHERE (products_language.name LIKE '%'+@name+'%' OR products.main_name LIKE '%'+@name+'%' OR products_language.synonyms LIKE '%'+@name+'%') AND products.manufacturer_id LIKE '%%' AND products.manufacturer_part_number LIKE @manufacturer_part_number AND products.part_number LIKE '%' AND products.cas_number LIKE '%' AND products.created_by LIKE '%%' AND products.company_id=@company_id AND products.archived LIKE '%%'
and again, if I remove any field in the where clause, back to a split second! DECLARE @company_id int = 10035 DECLARE @name varchar(50) = '' DECLARE @manufacturer_id varchar(50) = '%' DECLARE @manufacturer_part_number varchar(50) = '%' DECLARE @internal_part_number varchar(50) = '' DECLARE @cas_number varchar(50) = '' DECLARE @created_by varchar(50) = '%' DECLARE @archived varchar(50) = '%' DECLARE @language_id int = 1 DECLARE @site_id INT = 0 SELECT COUNT(products.product_id) AS product_count FROM products LEFT JOIN products_language ON products.product_id = products_language.product_id AND products_language.language_id = @language_id WHERE (products_language.name LIKE '%'+@name+'%' OR products.main_name LIKE '%'+@name+'%' OR products_language.synonyms LIKE '%'+@name+'%') AND products.manufacturer_id LIKE '%%' AND products.manufacturer_part_number LIKE @manufacturer_part_number'REMOVED PART NUMBER AND products.cas_number LIKE '%' AND products.created_by LIKE '%%' AND products.company_id=@company_id AND products.archived LIKE '%%'
Please help me, not understanding this one at all!