Solved

How to avoid LIKE clause in join - sql server

Posted on 2014-04-15
6
432 Views
Last Modified: 2014-05-03
Hi All,

In the below query, update statment is taking very long time to generate output. Please advise how to rewrite below query for better performance.

I don't want to remove '[' and ']' symbol in query because in the DB data contains '[' and ']'  symbols.
CREATE TABLE #temp
(

[Inquiry Number] NVARCHAR(255),
[CPO Number] VARCHAR(50),
[SO Number] VARCHAR(50)
)

CREATE nonclustered INDEX ix_InqNum ON #temp ([Inquiry Number]) include ([CPO Number])

INSERT INTO #temp
(
	[Inquiry Number],[CPO Number]
)

SELECT distinct PRA.inq_num,[PO Number]
FROM [LocalDB].dbo.ps_requests_asr PRA with(nolock)left join [RemoteDB].[DBName].dbo.CPOA with(nolock) ON cast(Inquiries as NVARCHAR(450)) = '[' + PRA.inq_num + ']' 
WHERE PRA.inq_num is not null

UPDATE	#temp SET [SO Number]= CUST.[SO] FROM #temp INNER JOIN [RemoteDB].[DBName].dbo.CPOA CPON WITH(NOLOCK) ON cast(CPON.Inquiries as NVARCHAR(450)) = '[' + #temp.[Inquiry Number] + ']'    	
		INNER JOIN [RemoteDB].[DBName].dbo.BI_CUSTMON CUST WITH(NOLOCK) ON CUST.[Customer PO] LIKE #temp.[CPO Number] + '%'
		
select * from #temp

Open in new window

0
Comment
Question by:sqldba2013
  • 3
  • 2
6 Comments
 
LVL 12

Expert Comment

by:HugoHiasl
ID: 40003510
If you do insert the [ and ] in the insert to the temp table and remove it from the join then the join can use your index which is useless at the moment.

INSERT INTO #temp
(
	[Inquiry Number],[CPO Number]
)
SELECT distinct '['+PRA.inq_num+']',[PO Number]

Open in new window


and

UPDATE	#temp SET [SO Number]= CUST.[SO] FROM #temp INNER JOIN [RemoteDB].[DBName].dbo.CPOA CPON WITH(NOLOCK) ON cast(CPON.Inquiries as NVARCHAR(450)) = #temp.[Inquiry Number]    	
		INNER JOIN [RemoteDB].[DBName].dbo.BI_CUSTMON CUST WITH(NOLOCK) ON CUST.[Customer PO] LIKE #temp.[CPO Number] + '%'
		

Open in new window


And when you are doing changes don't forget to create an index on [CPO Number] of #temp also. Since your join to cpo number is a "starts with" it can also use an index.
0
 

Author Comment

by:sqldba2013
ID: 40003539
I have already created non-clustered index on CPO number of #temp table.

I am facing query slowness issue with UPDATE statement not with INSERT.

Could you please tell me how to avoid like statement in above update statement or how to rewrite above query for better performance.

I have tried with = operator in place of like and I didn't get data as per my requirement for = operator.

Please advise.
0
 

Author Comment

by:sqldba2013
ID: 40003601
As per your suggestion, I have removed [ and ] from join and I got NULL as a output result.
CREATE TABLE #temp
(

[Inquiry Number] NVARCHAR(255),
[CPO Number] VARCHAR(50),
[SO Number] VARCHAR(50)
)

CREATE nonclustered INDEX ix_InqNum ON #temp ([Inquiry Number]) include ([CPO Number])

INSERT INTO #temp
(
	[Inquiry Number],[CPO Number]
)

SELECT distinct '['+PRA.inq_num+']',[PO Number]
FROM [LocalDB].dbo.ps_requests_asr PRA with(nolock)left join [RemoteDB].[DBName].dbo.CPOA with(nolock) ON cast(Inquiries as NVARCHAR(450)) = PRA.inq_num 
WHERE PRA.inq_num is not null

UPDATE	#temp SET [SO Number]= CUST.[SO] FROM #temp INNER JOIN [RemoteDB].[DBName].dbo.CPOA CPON WITH(NOLOCK) ON cast(CPON.Inquiries as NVARCHAR(450)) = #temp.[Inquiry Number]
		INNER JOIN [RemoteDB].[DBName].dbo.BI_CUSTMON CUST WITH(NOLOCK) ON CUST.[Customer PO] LIKE #temp.[CPO Number] + '%'
		
select * from #temp

Open in new window

0
Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

 
LVL 69

Assisted Solution

by:ScottPletcher
ScottPletcher earned 250 total points
ID: 40004271
SQL is almost certainly copying all the data from the remote server to the local server then doing the join (would have to see the query plan to be sure).  Instead, you need to copy your selection data -- inq_num from LocalDB].dbo.ps_requests_asr -- over to the remote server, execute the joins/update there, then copy the data back to the local server.
0
 
LVL 12

Accepted Solution

by:
HugoHiasl earned 250 total points
ID: 40023456
There are multiple problems.

1. All datatypes should exact match. You cast once to nvachar(450) and the other indexed field is nvarchar(255).

2. You ues CPON.Inquiries as counterpart. It needs to have an index on this column. But you don't use the original datatype of this column but cast it to nvarchar before joining. This means it cannot use the index (if one exists).

Fix both and the performance should be much better.
0
 

Author Closing Comment

by:sqldba2013
ID: 40039108
the above solutions worked for me.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Viewers will learn how the fundamental information of how to create a table.

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now