SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


12345»»»

Passing Parameters as (almost) 1, 2, and 3 Dimensional Arrays Expand / Collapse
Author
Message
Posted Monday, May 12, 2008 2:50 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 9:01 PM
Points: 20,164, Visits: 13,702
Comments posted to this topic are about the item Passing Parameters as (almost) 1, 2, and 3 Dimensional Arrays

--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

"Data isn't the only thing that's supposed to have Integrity."

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

For better, quicker answers on T-SQL questions, click on the following...
http://www.sqlservercentral.com/articles/Best+Practices/61537/

For better answers on performance questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
Post #499145
Posted Thursday, May 22, 2008 7:42 AM


SSCrazy Eights

SSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy Eights

Group: General Forum Members
Last Login: 2 days ago @ 2:45 PM
Points: 8,681, Visits: 4,953
Good article (of course).

I decided to try an experiment on your big numbers CTE (mainly because I'm bored this morning).

First, I ran it exactly as presented in the article, and got these CPU times: 219, 219, 187, 203, 187 (5 runs).

Then I modified it the CTE to select from my BigNumbers table (100-million rows of overkill). Run times: 328, 328, 313, 312, 312

But when I modified the CTE to select from a cross join of Numbers (10-thousand rows) to Numbers, instead of sys.all_objects to sys.all_objects, I got these run times: 188, 219, 218, 235, 218

I also tried using a table with 100-thousand rows of Numbers: 188, 172, 172, 140, 157

Since we're looking for a way to have millions of rows, the 100-thousand row table won't do, but I wanted to test it to see the baseline.

Since I saw a pattern in this, whereby the smaller the base table, the faster the query (makes sense), I tried making a 1000-row SmallNumbers table, cross joining that to itself, and running from there. Run times: 203, 203, 203, 187, 172

Just for the sake of test-to-break, I also tried creating a table called TinyNumbers, with 101 rows in it, and then cross joining that 3 times. Results: 171, 219, 219, 219, 234

It got my best run-times and best functionality off a 2-CTE Numbers structure, as follows:

;WITH 
Multiplier (Mult) as
(select top (len(@parameter)/100000) row_number() over (order by number)
from dbo.tinynumbers
union
select 1
from dbo.tinynumbers
where len(@parameter)/100000 < 1),

cteTally (Number) AS
(select top (len(@parameter)-1)
row_number() over (order by n1.number)
from dbo.smallnumbers n1
cross join dbo.smallnumbers n2
cross join multiplier)

SELECT ROW_NUMBER() OVER (ORDER BY Number) AS Number,
SUBSTRING(@Parameter,Number+1,CHARINDEX(',',@Parameter,Number+1)-Number-1) AS Value
FROM cteTally
WHERE SUBSTRING(@Parameter,Number,1) = ','

This one adds about .01 second of CPU time because of the Multiplier CTE, but it also means it can handle up to 1-billion character inputs.

It appears, after these tests, that it's faster to run a cross-join in a CTE (builds a worktable in tempdb) than to query a large numbers table, but that a 1000-row base is faster than a larger base (like sys.all_objects). The speed difference for the smaller cross-join is slight, but measurable, and might be worth it on a heavily loaded system.

You might also notice that I modified the Top () portion of the cteTally, and got rid of the Len part of the Where clause in the outer query. Subtracting 1 from the len makes that part of the Where redudant, and cut about .03 off the run-time (172, 173, 171, 181, 173).

When I got rid of the output (assigned the select value to a variable instead of returning it as a result set), and increased the number of elements in the string to 80,000, this version took 1.8 seconds.

(Yeah, like I said, I'm bored this morning.)


- GSquared

"Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon
Post #505178
Posted Thursday, May 22, 2008 7:18 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 9:01 PM
Points: 20,164, Visits: 13,702
Good stuff, Gus! Looks like I have a couple of more things to play with. Glad you were bored this morning! :)

--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

"Data isn't the only thing that's supposed to have Integrity."

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

For better, quicker answers on T-SQL questions, click on the following...
http://www.sqlservercentral.com/articles/Best+Practices/61537/

For better answers on performance questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
Post #505585
Posted Friday, May 23, 2008 5:31 AM
Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Thursday, February 11, 2010 7:52 AM
Points: 3,465, Visits: 976
Awesome one Jeff. Another bomb. I just wonder how much pain you take to write these articles? Excellent one. Keep it up......:)


Post #505762
Posted Friday, May 23, 2008 8:48 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 9:01 PM
Points: 20,164, Visits: 13,702
Anirban Paul (5/23/2008)
Awesome one Jeff. Another bomb. I just wonder how much pain you take to write these articles? Excellent one. Keep it up......:)


Thanks for the great feedback and the question, too, Arniban!

I'm not what I'd call a "prolific" writer... I agonize over everything... form, fit, function, correctness and readability of code, order of presentation, etc. The code is especially important... I hate it when I use someone else's code and it doesn't work as advertised or it's difficult to read and has no embedded documentation. The code examples I create are typically very simple, but they have to follow my own rules... "Make it work, make it fast, make it pretty, and it ain't done 'til it's pretty."

I also hate it when someone's graphics are too small to read and, when you zoom in on them, too blurry to easily read.

This last article, if I had to guess, took me somewhere between 12 and 16 hours to write. A more prolific writer could probably have banged it out in an hour or two and still done a heck of a good job.

Heh... just imagine me writing a book... 11 pages every 2 days... it would take an eon for me to write one. I don't know how some of these folks can turn out a couple of 500 or 1,000 page books a year... I envy them, quite a bit.


--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

"Data isn't the only thing that's supposed to have Integrity."

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

For better, quicker answers on T-SQL questions, click on the following...
http://www.sqlservercentral.com/articles/Best+Practices/61537/

For better answers on performance questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
Post #505923
Posted Friday, May 23, 2008 9:49 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 7:09 PM
Points: 6,189, Visits: 8,924
Jeff Moden (5/23/2008)


Heh... just imagine me writing a book... 11 pages every 2 days... it would take an eon for me to write one. I don't know how some of these folks can turn out a couple of 500 or 1,000 page books a year... I envy them, quite a bit.


Hmm Jeff. Food for thought. If you can crank 11 pages in 2 days:

11 pages every 2 days = 5.5pages per day.

So 750 pages ==> 27 weeks at 5 days a week (and we all know you don't walk away on weekends...).

So - I take it that means we can be expecting 2 books a year from you?


----------------------------------------------------------------------------------
Your lack of planning does not constitute an emergency on my part...unless you're my manager...or a director and above...or a really loud-spoken end-user..All right - what was my emergency again?
Post #505986
Posted Friday, May 23, 2008 11:00 AM


Right there with Babe

Right there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with Babe

Group: General Forum Members
Last Login: 2 days ago @ 12:58 PM
Points: 746, Visits: 1,213
Thanks Jeff, for some more COOL STUFF.

Most days I don't have time to read the more lengthy articles, but I did today, and I've been meaning to look into these Tally thingies you keep talking about, and see how they actually have some usefulness.

That's some great, and efficient code, and some of it is now in my "library".

Thanks again,


Tom Garth
Vertical Solutions

"There are three kinds of men. The one that learns by reading. The few who learn by observation. The rest of them have to pee on the electric fence for themselves." -- Will Rogers
Post #506023
Posted Friday, May 23, 2008 3:22 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 9:01 PM
Points: 20,164, Visits: 13,702
That's some of the best compliments I could hope for, Tom... someone adding this type of code to their library. Thanks!

If you haven't done so already, here's a recent article I wrote about how the Tally table works to replace loops, in some cases...

http://www.sqlservercentral.com/articles/TSQL/62867/

... and, if you get really bored on this fine 3 day weekend, here's all my stuff, so far. The one on running balances has some pretty neat stuff that can be used for other, surprising things...

http://www.sqlservercentral.com/Authors/Articles/Jeff_Moden/80567/

What're even more worth reading are the great suggestions some folks wrote about in the discussions that followed each article and the wonderful code examples some folks submitted as a part of the discussions.


--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

"Data isn't the only thing that's supposed to have Integrity."

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

For better, quicker answers on T-SQL questions, click on the following...
http://www.sqlservercentral.com/articles/Best+Practices/61537/

For better answers on performance questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
Post #506153
Posted Friday, May 23, 2008 5:28 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 9:01 PM
Points: 20,164, Visits: 13,702
Matt Miller (5/23/2008)
Jeff Moden (5/23/2008)


Heh... just imagine me writing a book... 11 pages every 2 days... it would take an eon for me to write one. I don't know how some of these folks can turn out a couple of 500 or 1,000 page books a year... I envy them, quite a bit.


Hmm Jeff. Food for thought. If you can crank 11 pages in 2 days:

11 pages every 2 days = 5.5pages per day.

So 750 pages ==> 27 weeks at 5 days a week (and we all know you don't walk away on weekends...).

So - I take it that means we can be expecting 2 books a year from you?


Oh heck no... those are two 8 hour days... gotta earn a living and a book isn't likely to be a great source of income... HEH! If you don't believe that, look at Celko... he's got a number of books on the market and he still dresses funny... :D


--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

"Data isn't the only thing that's supposed to have Integrity."

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

For better, quicker answers on T-SQL questions, click on the following...
http://www.sqlservercentral.com/articles/Best+Practices/61537/

For better answers on performance questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
Post #506172
Posted Friday, May 23, 2008 10:21 PM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 7:09 PM
Points: 6,189, Visits: 8,924
Jeff Moden (5/23/2008)

Oh heck no... those are two 8 hour days... gotta earn a living and a book isn't likely to be a great source of income... HEH! If you don't believe that, look at Celko... he's got a number of books on the market and he still dresses funny... :D


The funny clothes I can deal with. The tin foil hat on the other hand...:)


----------------------------------------------------------------------------------
Your lack of planning does not constitute an emergency on my part...unless you're my manager...or a director and above...or a really loud-spoken end-user..All right - what was my emergency again?
Post #506202
« Prev Topic | Next Topic »

12345»»»

Permissions Expand / Collapse