Solved

How to avoid LIKE clause in join - sql server

Posted on 2014-04-15
6
451 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Free Webinar: AWS Backup & DR

Join our upcoming webinar with experts from AWS, CloudBerry Lab, and the Town of Edgartown IT to discuss best practices for simplifying online backup management and cutting costs.

 
LVL 69

Assisted Solution

by:Scott Pletcher
Scott Pletcher 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
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.

756 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