We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

Query Optimise

byte1
byte1 asked
on
Medium Priority
234 Views
Last Modified: 2012-05-07
How do i optimise this query ? The INNER LOOPS seem to take a hit on execution .
GO
 
DECLARE @Name1 nvarchar(100)
DECLARE @Name2 nvarchar(100)
DECLARE @Name3 nvarchar(100)
DECLARE @Name4 nvarchar(100)
 
DECLARE	@return_value int
declare @counter int
	set @counter = 0
	while @counter < 4650
BEGIN	
 
		 SET @Name1 = N'Name1'
		SET @Name2 = N'Name2'
		SET @Name3 = N'Name3'
		SET @Name4 = N'Name4'
		SET @Type = 1
		
		
 IF OBJECT_ID('tempdb..#SearchNames') IS NOT NULL DROP TABLE #SearchNames
 
CREATE TABLE #SearchNames
(
Names varchar(100)  NOT NULL
)
 
 
 
BEGIN   
SELECT * FROM oc_SDN WHERE Ent_Num IN 
			 (
		SELECT Ent_Num FROM oc_SDN A INNER JOIN #SearchNames B ON A.SDN_Name LIKE '%'  + B.Names  + '%'
			  )
OR Ent_Num IN ( 
		SELECT Ent_Num FROM oc_Aliases A INNER JOIN #SearchNames B ON A.Alt_Name LIKE '%'  + B.Names  + '%'
			  )
END
 
SET @counter = @counter +1
SELECT	'Return Value' = @return_value
END
 
GO

Open in new window

Comment
Watch Question

Kevin CrossChief Technology Officer
CERTIFIED EXPERT
Most Valuable Expert 2011

Commented:
Think something is missing from your code as you never put any data into the temp table and have two sets of BEGIN/END tags that seem out of place.
AneeshDatabase Consultant
CERTIFIED EXPERT
Top Expert 2009

Commented:
How will you populate the #SearchNames ?

Author

Commented:
Sorry about that, I left some code.

if LEN(@Name1) > 0 BEGIN
SET @Name1 = replace(replace(replace(@Name1,' ','<>'),'><',''),'<>','%') 
INSERT INTO #SearchNames (Names) VALUES (@Name1)
END  
if LEN(@Name2) > 0 BEGIN	
SET @Name2 = replace(replace(replace(@Name2,' ','<>'),'><',''),'<>','%')  
INSERT INTO #SearchNames (Names) VALUES (@Name2)
END  
if LEN(@Name3) > 0 BEGIN
SET @Name3 =   replace(replace(replace(@Name3,' ','<>'),'><',''),'<>','%') 
INSERT INTO #SearchNames (Names) VALUES (@Name3)
 END  
if LEN(@Name4) > 0 BEGIN
SET @Name4 =  replace(replace(replace(@Name4,' ','<>'),'><',''),'<>','%') 
INSERT INTO #SearchNames (Names) VALUES (@Name4)
END  

Open in new window

AneeshDatabase Consultant
CERTIFIED EXPERT
Top Expert 2009

Commented:
can you post the complete query

Author

Commented:

GO
 
DECLARE @Name1 nvarchar(100)
DECLARE @Name2 nvarchar(100)
DECLARE @Name3 nvarchar(100)
DECLARE @Name4 nvarchar(100)
DECLARE @Type int
 
DECLARE	@return_value int
declare @counter int
	set @counter = 0
	while @counter < 100
BEGIN	
 
		 SET @Name1 = N'Name1'
		SET @Name2 = N'Name2'
		SET @Name3 = N'Name3'
		SET @Name4 = N'Name4'
		SET @Type = 1
		
		
 IF OBJECT_ID('tempdb..#SearchNames') IS NOT NULL DROP TABLE #SearchNames
 
CREATE TABLE #SearchNames
(
Names varchar(100)  NOT NULL
)
 
if LEN(@Name1) > 0 BEGIN
SET @Name1 = replace(replace(replace(@Name1,' ','<>'),'><',''),'<>','%') 
INSERT INTO #SearchNames (Names) VALUES (@Name1)
END  
if LEN(@Name2) > 0 BEGIN	
SET @Name2 = replace(replace(replace(@Name2,' ','<>'),'><',''),'<>','%')  
INSERT INTO #SearchNames (Names) VALUES (@Name2)
END  
if LEN(@Name3) > 0 BEGIN
SET @Name3 =   replace(replace(replace(@Name3,' ','<>'),'><',''),'<>','%') 
INSERT INTO #SearchNames (Names) VALUES (@Name3)
 END  
if LEN(@Name4) > 0 BEGIN
SET @Name4 =  replace(replace(replace(@Name4,' ','<>'),'><',''),'<>','%') 
INSERT INTO #SearchNames (Names) VALUES (@Name4)
END  
 
if(@Type = 1)
BEGIN   
SELECT * FROM oc_SDN WHERE Ent_Num IN 
			 (
		SELECT Ent_Num FROM oc_SDN A INNER JOIN #SearchNames B ON A.SDN_Name LIKE '%'  + B.Names  + '%'
			  )
OR Ent_Num IN ( 
		SELECT Ent_Num FROM oc_Aliases A INNER JOIN #SearchNames B ON A.Alt_Name LIKE '%'  + B.Names  + '%'
			  )
END
ELSE
BEGIN
SELECT * FROM oc_SDN 
WHERE Ent_Num IN (
		SELECT Ent_Num FROM oc_SDN A INNER JOIN #SearchNames B ON A.SDN_Name = B.Names WHERE LEN(Names) > 0
			  )
OR Ent_Num IN ( 
		SELECT Ent_Num FROM oc_Aliases A INNER JOIN #SearchNames B ON A.Alt_Name = B.Names WHERE LEN(Names) > 0
			  )
END
 
SET @counter = @counter +1
SELECT	'Return Value' = @return_value
END
 
GO

Open in new window

Database Consultant
CERTIFIED EXPERT
Top Expert 2009
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
About the indexing , I was not able to create a - SDN_Name INCLUDE (Ent_Num ), but it did work the other way around, where Ent_Num was the index and SDN_Name was in the Included columns.

Ent_num int
SDN_Name nvarchar(max)


Error:
Column 'SDN_Name' in table 'dbo.oc_SDN' is of a type that is invalid for use as a key column in an index. (Microsoft SQL Server, Error: 1919)

Author

Commented:
Update :
I changed the SDN_Name datatype to nvarchar(150) which allowed me to create an index as specified.
AneeshDatabase Consultant
CERTIFIED EXPERT
Top Expert 2009

Commented:
Is it faster ?

Author

Commented:
It did improve my search time. Thanks. But I wish i could do more improvements.
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.