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
Solved

How to avoid LIKE clause in join - sql server

Posted on 2014-04-15
6
448 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

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.​
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

856 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