Patindex

  • Now you're scaring me! 😛 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.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • 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 😉

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

  • 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.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • 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 :P.

  • Heh... "Crap happens". 😛

    --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.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • The article and discussion both are superb...

  • 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.

  • 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.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • vibhasjog ,

    Please check if you are using transaction in this step in case you don't have your linked server configured for this.

  • 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!

  • dphillips (9/16/2008)


    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!

    Thank you very much. Heh... I stay up way too late on some of these.

    The real key is that I have a good friend that I argue with/agree with once in a while. He said something I'll never forget... "A developer must not guess. A developer must know!" On lot's of these posts I say, "Darned good question... I know what some of the myths are, what's the truth?" I write a bunch of code (lots of copy and paste in cases like this) and test the heck out of it so I know the answer for sure before I post... settles lots of arguments when you have the proof in the form of code. 🙂

    As you can see, though, certain machines behave differently. Remi had a machine that made LIKE look much worse than it does on my machine. So, I also post my results so people can compare. If I'm lucky, someone else (couple of good folks helped in this thread) will run the same code and post their results and we all learn more than what the original question was. Give some, take some, everybody (including me) learns more than expected. 😀

    --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.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • Manie, I gotta say it again... good article. You brought some good people out in the discussion about PatIndex. Nicely done! 🙂

    --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.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • Jeff Moden (9/16/2008)


    ... The real key is that I have a good friend that I argue with/agree with once in a while. He said something I'll never forget... "A developer must not guess. A developer must know!" On lot's of these posts I say, "Darned good question... I know what some of the myths are, what's the truth?" I write a bunch of code (lots of copy and paste in cases like this) and test the heck out of it so I know the answer for sure before I post... settles lots of arguments when you have the proof in the form of code. 🙂 ...

    Sort of a Database Mythbusters, eh? 😀

    (my wife was on Mythbusters a couple of weeks ago, that was cool)

    -----
    [font="Arial"]Knowledge is of two kinds. We know a subject ourselves or we know where we can find information upon it. --Samuel Johnson[/font]

  • Wayne West (9/17/2008)


    Jeff Moden (9/16/2008)


    ... The real key is that I have a good friend that I argue with/agree with once in a while. He said something I'll never forget... "A developer must not guess. A developer must know!" On lot's of these posts I say, "Darned good question... I know what some of the myths are, what's the truth?" I write a bunch of code (lots of copy and paste in cases like this) and test the heck out of it so I know the answer for sure before I post... settles lots of arguments when you have the proof in the form of code. 🙂 ...

    Sort of a Database Mythbusters, eh? 😀

    (my wife was on Mythbusters a couple of weeks ago, that was cool)

    Oh, man, talk about a setup for a wise crack. What myth about you was your wife busting, exactly? :w00t:

    ---------------------------------------------------------
    How best to post your question[/url]
    How to post performance problems[/url]
    Tally Table:What it is and how it replaces a loop[/url]

    "stewsterl 80804 (10/16/2009)I guess when you stop and try to understand the solution provided you not only learn, but save yourself some headaches when you need to make any slight changes."

  • It was the episode about the moon landings were faked/conspiracies, I think it aired Labor Day weekend in the US. She was the final segment, the astronomer who shoots a laser at the moon. As it happens, she's doing one of those runs 1am Friday morning.

    They taped it in January, unfortunately I had to be in Phoenix that day and couldn't go to the observatory. The funny thing was that they were testing their cold weather gear (which was all crap) as they went from New Mexico to Alaska to film that special. Dunno why it took so long to air.

    I got a Mythbusters t-shirt out of it, they have an autographed poster at the observatory.

    -----
    [font="Arial"]Knowledge is of two kinds. We know a subject ourselves or we know where we can find information upon it. --Samuel Johnson[/font]

Viewing 15 posts - 31 through 45 (of 51 total)

You must be logged in to reply to this topic. Login to reply