SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Patindex


Patindex

Author
Message
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (85K reputation)SSC Guru (85K reputation)SSC Guru (85K reputation)SSC Guru (85K reputation)SSC Guru (85K reputation)SSC Guru (85K reputation)SSC Guru (85K reputation)SSC Guru (85K reputation)

Group: General Forum Members
Points: 85959 Visits: 41091
Now you're scaring me! Tongue I wonder if VM's have a problem that way? I've gotta test this at work on Monday... we have some identical boxes some of which have been VM'd and some not.

Or, maybe it's a simple as parallelism... my box is a single CPU... could you add the MAXDOP 1 option to the first "group" of SELECTs and post the output from that?

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
D.Oc
D.Oc
Ten Centuries
Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)

Group: General Forum Members
Points: 1069 Visits: 6480
hmm.. With (MAXDOP=1)


for SQL 2005




********************************************************************************
Found, no index
********************************************************************************
===== Far Right =====
--------------CharIndex

SQL Server Execution Times:
CPU time = 8640 ms, elapsed time = 5027 ms.
--------------PatIndex %aa%

SQL Server Execution Times:
CPU time = 8750 ms, elapsed time = 4993 ms.
--------------PatIndex %aa

SQL Server Execution Times:
CPU time = 8750 ms, elapsed time = 5034 ms.
--------------Right

SQL Server Execution Times:
CPU time = 703 ms, elapsed time = 427 ms.
--------------Substring

SQL Server Execution Times:
CPU time = 735 ms, elapsed time = 418 ms.
--------------Like %aa%

SQL Server Execution Times:
CPU time = 8594 ms, elapsed time = 4922 ms.
--------------Like %aa

SQL Server Execution Times:
CPU time = 8999 ms, elapsed time = 5404 ms.
================================================================================
===== Far Left =====
--------------CharIndex

SQL Server Execution Times:
CPU time = 1063 ms, elapsed time = 675 ms.
--------------PatIndex %aa%

SQL Server Execution Times:
CPU time = 1109 ms, elapsed time = 724 ms.
--------------PatIndex aa%

SQL Server Execution Times:
CPU time = 8969 ms, elapsed time = 5400 ms.
--------------Left

SQL Server Execution Times:
CPU time = 719 ms, elapsed time = 731 ms.
--------------Substring

SQL Server Execution Times:
CPU time = 719 ms, elapsed time = 735 ms.
--------------Like %aa%

SQL Server Execution Times:
CPU time = 968 ms, elapsed time = 610 ms.
--------------Like aa%

SQL Server Execution Times:
CPU time = 9000 ms, elapsed time = 5226 ms.
================================================================================
===== Middle =====
--------------CharIndex

SQL Server Execution Times:
CPU time = 4610 ms, elapsed time = 2836 ms.
--------------PatIndex %aa%

SQL Server Execution Times:
CPU time = 4874 ms, elapsed time = 2850 ms.
--------------Substring

SQL Server Execution Times:
CPU time = 813 ms, elapsed time = 479 ms.
--------------Like %aa%

SQL Server Execution Times:
CPU time = 4750 ms, elapsed time = 2856 ms.
================================================================================




SQL 2008



********************************************************************************
Found, no index
********************************************************************************
===== Far Right =====
--------------CharIndex

SQL Server Execution Times:
CPU time = 11657 ms, elapsed time = 9175 ms.
--------------PatIndex %aa%

SQL Server Execution Times:
CPU time = 11281 ms, elapsed time = 7517 ms.
--------------PatIndex %aa

SQL Server Execution Times:
CPU time = 10219 ms, elapsed time = 5786 ms.
--------------Right

SQL Server Execution Times:
CPU time = 859 ms, elapsed time = 493 ms.
--------------Substring

SQL Server Execution Times:
CPU time = 718 ms, elapsed time = 438 ms.
--------------Like %aa%

SQL Server Execution Times:
CPU time = 10548 ms, elapsed time = 5706 ms.
--------------Like %aa

SQL Server Execution Times:
CPU time = 10234 ms, elapsed time = 5853 ms.
================================================================================
===== Far Left =====
--------------CharIndex

SQL Server Execution Times:
CPU time = 1109 ms, elapsed time = 711 ms.
--------------PatIndex %aa%

SQL Server Execution Times:
CPU time = 1048 ms, elapsed time = 745 ms.
--------------PatIndex aa%

SQL Server Execution Times:
CPU time = 10577 ms, elapsed time = 5558 ms.
--------------Left

SQL Server Execution Times:
CPU time = 781 ms, elapsed time = 812 ms.
--------------Substring

SQL Server Execution Times:
CPU time = 812 ms, elapsed time = 818 ms.
--------------Like %aa%

SQL Server Execution Times:
CPU time = 1001 ms, elapsed time = 660 ms.
--------------Like aa%

SQL Server Execution Times:
CPU time = 10609 ms, elapsed time = 5585 ms.
================================================================================
===== Middle =====
--------------CharIndex

SQL Server Execution Times:
CPU time = 5454 ms, elapsed time = 3206 ms.
--------------PatIndex %aa%

SQL Server Execution Times:
CPU time = 5874 ms, elapsed time = 3229 ms.
--------------Substring

SQL Server Execution Times:
CPU time = 797 ms, elapsed time = 450 ms.
--------------Like %aa%

SQL Server Execution Times:
CPU time = 5531 ms, elapsed time = 3205 ms.
================================================================================



I have three more machines with sql 05 on them in my house, and tommorow I'll test this code on them too just to see what will happen. I could do it now but it is 2 AM here Wink

-------------------------------------------------------------
"It takes 15 minutes to learn the game and a lifetime to master"
"Share your knowledge. It's a way to achieve immortality."

Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (85K reputation)SSC Guru (85K reputation)SSC Guru (85K reputation)SSC Guru (85K reputation)SSC Guru (85K reputation)SSC Guru (85K reputation)SSC Guru (85K reputation)SSC Guru (85K reputation)

Group: General Forum Members
Points: 85959 Visits: 41091
Wow... even those seem pretty long. Maybe it's cache on my machine. I've not checked but it was advertised as a "server quality" mother board with lot's of cache... I bought it because a friend at one of the local stores gave me a deal on it. I'll take a look at the BIOS and see what's going on.

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Ninja's_RGR'us
Ninja's_RGR'us
One Orange Chip
One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)

Group: General Forum Members
Points: 28845 Visits: 9671
Hey Jeff I found the problem with the test machine... it's just crappy.

I reran the test with 10 000 rows (100 times less data), and it still ran only 110 times faster than the original test.

So that's all she wrote Tongue.
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (85K reputation)SSC Guru (85K reputation)SSC Guru (85K reputation)SSC Guru (85K reputation)SSC Guru (85K reputation)SSC Guru (85K reputation)SSC Guru (85K reputation)SSC Guru (85K reputation)

Group: General Forum Members
Points: 85959 Visits: 41091
Heh... "Crap happens". Tongue

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Anipaul
Anipaul
SSCertifiable
SSCertifiable (7.3K reputation)SSCertifiable (7.3K reputation)SSCertifiable (7.3K reputation)SSCertifiable (7.3K reputation)SSCertifiable (7.3K reputation)SSCertifiable (7.3K reputation)SSCertifiable (7.3K reputation)SSCertifiable (7.3K reputation)

Group: General Forum Members
Points: 7301 Visits: 1407
The article and discussion both are superb...



Carla Wilson-484785
Carla Wilson-484785
SSCommitted
SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)

Group: General Forum Members
Points: 1915 Visits: 1950
Jeff, thanks for that great test script. I like your code for building 1,000,000 row table. I'm saving this code for reference.

Thanks for pointing out how much more efficient LEFT is than LIKE or CHARINDEX. (I knew that, but wasn't thinking about it.)
My original WHERE clause:
WHERE Address LIKE 'XX%' would be much faster as
WHERE LEFT(Address,2) = 'XX'

It's easy to get caught up in
WHERE Col1 LIKE '%aa%'
OR Col1 LIKE '%bb%
OR Col1 LIKE 'xx%'

Maybe this is where LIKE gets the bad rap - when the code should be using LEFT() instead.
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (85K reputation)SSC Guru (85K reputation)SSC Guru (85K reputation)SSC Guru (85K reputation)SSC Guru (85K reputation)SSC Guru (85K reputation)SSC Guru (85K reputation)SSC Guru (85K reputation)

Group: General Forum Members
Points: 85959 Visits: 41091
Carla Wilson (9/15/2008)
Jeff, thanks for that great test script. I like your code for building 1,000,000 row table. I'm saving this code for reference.

Thanks for pointing out how much more efficient LEFT is than LIKE or CHARINDEX. (I knew that, but wasn't thinking about it.)
My original WHERE clause:
WHERE Address LIKE 'XX%' would be much faster as
WHERE LEFT(Address,2) = 'XX'

It's easy to get caught up in
WHERE Col1 LIKE '%aa%'
OR Col1 LIKE '%bb%
OR Col1 LIKE 'xx%'

Maybe this is where LIKE gets the bad rap - when the code should be using LEFT() instead.


Yep... from what I can see, LIKE %aa% is faster than either %aa or aa% and LEFT/RIGHT blows that away.

Thanks for the feedback on the test table. If you have any questions on how it works, please don't hesitate to ask.

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
MuhammadSiddiqi
MuhammadSiddiqi
Valued Member
Valued Member (54 reputation)Valued Member (54 reputation)Valued Member (54 reputation)Valued Member (54 reputation)Valued Member (54 reputation)Valued Member (54 reputation)Valued Member (54 reputation)Valued Member (54 reputation)

Group: General Forum Members
Points: 54 Visits: 124
vibhasjog ,
Please check if you are using transaction in this step in case you don't have your linked server configured for this.
DPhillips-731960
DPhillips-731960
Ten Centuries
Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)

Group: General Forum Members
Points: 1248 Visits: 801
Jeff, great examples and test building. It amazes me you have the time to post such significant and applicable content and in such detail.

Thank you for your many contributions!
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search