September 22, 2009 at 8:25 am
Hello, I have to queries that are the same except for a like vs a substriing in the where clause. The query with the substring runs very fast where the query with the like clause runs painfully slow.
Below are the queries.
SELECT customer.customer_no, customer.customer_id, upper(first_name) 'first_name', upper(last_name) 'last_name',
telephone_no, address_1, post_code, email_address , address_match_key
FROM customer, address
WHERE alpha_key = 'BEDOYA TERE'
and customer.customer_id=address.customer_id
AND customer.active_address_id=address.address_id
and substring(address_match_key, 1, 5) = '07093'
SELECT customer.customer_no, customer.customer_id, upper(first_name) 'first_name', upper(last_name) 'last_name',
telephone_no, address_1, post_code, email_address , address_match_key
FROM customer, address
WHERE alpha_key = 'BEDOYA TERE'
and customer.customer_id=address.customer_id
AND customer.active_address_id=address.address_id
and address_match_key LIKE '07093%'
The address table has a nonclustered index on the address_match_key column. All stats are up to date.
I have attached the query plans.
Best Regards,
~David
September 22, 2009 at 8:37 am
It looks like your execution plans got hosed when you posted them. They're riddled with garbage from the formatting. Was that the XML plan? You'll need to re-post the execution plans and perhaps surround them with the proper tags. Please use the Preview option to make sure they look correct.
September 22, 2009 at 8:51 am
What is the best way to present the plans? I copied them from SSMS and put code tags around them.
Best Regards,
~David
September 22, 2009 at 8:57 am
When I include a wildcard on both sides of the like string it uses the better plan and returns immediately.
SELECT customer.customer_no, customer.customer_id, upper(first_name) 'first_name', upper(last_name) 'last_name',
telephone_no, address_1, post_code, email_address , address_match_key
FROM customer, address
WHERE alpha_key = N'BEDOYA TERE'
and customer.customer_id=address.customer_id
AND customer.active_address_id=address.address_id
and address_match_key LIKE N'%07093%'
Best Regards,
~David
September 22, 2009 at 9:03 am
Here's an example of getting the plan into the forum thread.......
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
September 22, 2009 at 10:36 am
Zipping them almost always works.
[font="Times New Roman"]-- RBarryYoung[/font], [font="Times New Roman"] (302)375-0451[/font] blog: MovingSQL.com, Twitter: @RBarryYoung[font="Arial Black"]
Proactive Performance Solutions, Inc. [/font][font="Verdana"] "Performance is our middle name."[/font]
September 22, 2009 at 11:25 am
The zipped query plans are attached. The query with the like in it work well sporatically but the query with the substring works well all the time. Thank you.
Best Regards,
~David
September 22, 2009 at 11:54 am
What would cause the SS optimizer to choose the wrong plan. The same query with index hints runs far better that the path taken by SS. Here are the 2 queries:
Slow:
SELECT customer.customer_no, customer.customer_id, upper(first_name) 'first_name', upper(last_name) 'last_name',
telephone_no, address_1, post_code, email_address , address_match_key
FROM customer, address
WHERE alpha_key = N'BEDOYA TERE'
and customer.customer_id=address.customer_id
AND customer.active_address_id=address.address_id
and address_match_key LIKE N'07093%'
Fast one:
SELECT customer.customer_no, customer.customer_id, upper(first_name) 'first_name', upper(last_name) 'last_name',
telephone_no, address_1, post_code, email_address , address_match_key
FROM customer (index (customer_x2)), address (index (address_x0))
WHERE alpha_key = 'BEDOYA TERE'
and customer.customer_id=address.customer_id
AND customer.active_address_id=address.address_id
and address_match_key LIKE '07093%'
The fast one forces index usage.
Best Regards,
~David
September 22, 2009 at 12:14 pm
September 22, 2009 at 5:38 pm
David Kranes (9/22/2009)
What would cause the SS optimizer to choose the wrong plan. The same query with index hints runs far better that the path taken by SS. Here are the 2 queries:Slow:
SELECT customer.customer_no, customer.customer_id, upper(first_name) 'first_name', upper(last_name) 'last_name',
telephone_no, address_1, post_code, email_address , address_match_key
FROM customer, address
WHERE alpha_key = N'BEDOYA TERE'
and customer.customer_id=address.customer_id
AND customer.active_address_id=address.address_id
and address_match_key LIKE N'07093%'
You're using the "old style" of joins in the WHERE clause. My recommendation would be to try using ANSI joins (like INNER JOIN) and see if the optimizer makes better choices without (ugh!) having to use index hints.
Fast one:
SELECT customer.customer_no, customer.customer_id, upper(first_name) 'first_name', upper(last_name) 'last_name',
telephone_no, address_1, post_code, email_address , address_match_key
FROM customer (index (customer_x2)), address (index (address_x0))
WHERE alpha_key = 'BEDOYA TERE'
and customer.customer_id=address.customer_id
AND customer.active_address_id=address.address_id
and address_match_key LIKE '07093%'
The fast one forces index usage.
--Jeff Moden
Change is inevitable... Change for the better is not.
September 23, 2009 at 9:49 am
That's where I was going to go with this too Jeff, the ANSI JOIN syntax. I wanted to see the table structure though because I noticed that the join is on both Customer_ID and Address_ID which looked funny. This may be a case for normalization as well.
September 23, 2009 at 10:02 am
Good point! I did re-write the query but it made no difference. Here is the query with the index hints which works well. It still needs the hints. Arg!!!
SELECT
customer.customer_no,
customer.customer_id,
upper(first_name) 'first_name',
upper(last_name) 'last_name',
telephone_no,
address_1,
post_code,
email_address,
address_match_key
FROM
customer (index (customer_x2)) INNER JOIN address (index (address_x0))
ON customer.customer_id=address.customer_id
AND customer.active_address_id=address.address_id
WHERE
alpha_key = 'BEDOYA TERE'
AND address_match_key LIKE N'07093%'
Best Regards,
~David
September 23, 2009 at 10:13 am
Would collation setting have anything to do with this type of performance issues?
Best Regards,
~David
September 23, 2009 at 10:17 am
As asked before, would you please post the DDL for your table and the indexes?
September 23, 2009 at 1:06 pm
Sorry for the delay in providing this. I have attached the DDL.
Best Regards,
~David
Viewing 15 posts - 1 through 15 (of 25 total)
You must be logged in to reply to this topic. Login to reply