Solved

How to avoid LIKE clause in join - sql server

Posted on 2014-04-15
6
420 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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Join & Write a Comment

Suggested Solutions

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

757 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

22 Experts available now in Live!

Get 1:1 Help Now