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


Add to briefcase «««12345»»»

O(n) , O(n log n), O(n^2) Expand / Collapse
Author
Message
Posted Tuesday, June 09, 2009 1:58 PM


SSCoach

SSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoach

Group: General Forum Members
Last Login: Yesterday @ 10:04 AM
Points: 15,442, Visits: 9,590
He's talking about memory allocation. I don't know how T-SQL handles variable buffers and such, but in C, you would actually rewrite the memory addresses (or at least, you can). That's different from what Stuff does, per his post. (Can't say for sure. Don't know enough about that level of the database engine.)

- Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
Property of The Thread

"Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon
Post #731838
Posted Tuesday, June 09, 2009 2:45 PM


SSCrazy Eights

SSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy Eights

Group: General Forum Members
Last Login: Friday, March 28, 2014 2:25 PM
Points: 9,902, Visits: 9,479
karthikeyan (6/9/2009)
RBarryYoung (6/8/2009)

3) The "pre-allocate and Stuff" trick popular with mutable strings is not workable in T-SQL because the STUFF() function in T_SQL is NOT like the function of the same name in some general purpose languages: the T-SQL STUFF() is an RHS (right-hand side) function and NOT an LHS (left-hand side) function. AFAIK, there is no function in SQL that can (physically) write into a pre-existing string.


RBarryyoung,

The above two points are confusing me...

you mean to say TSQL STUFF() function can't write into a pre-existing string.

Correct. Except for the TEXT-mod commands (UPDATETEXT, etc.) SQL Server does not have mutable strings for in-memory values, which simply means that technically, you can never "modify" a string in-place, you always have to make a new copy of it. (note: do not try to apply this to on-disk values, like columns in tables, because the terms don't mean the same thing there).

Note that this is in apparent contradiction to the BOL entry for STUFF() which says: "Deletes a specified length of characters and inserts another set of characters at a specified starting point." However, STUFF() does nothing of the sort as demonstrated by this script:
Declare @a varchar(20), @b varchar(20), @c varchar(20)

Select @a='', @b='123456789', @c='xyz'
Select @a as [@a], @b as [@b], @c as [@c]

Set @a = STUFF(@b, 5, 3, @c)

Select @a as [@a], @b as [@b], @c as [@c]

You will note that the @B string is unchanged by STUFF().

Now if BOL had prefaced that statement so that it read "Returns a string by copying the source string and then Deletes a specified length of characters and inserts another set of characters at a specified starting point." it would be correct, but as it currently stands, BOL is wrong.


-- RBarryYoung, (302)375-0451 blog: MovingSQL.com, Twitter: @RBarryYoung
Proactive Performance Solutions, Inc.
"Performance is our middle name."
Post #731868
Posted Tuesday, June 09, 2009 2:49 PM


SSCrazy Eights

SSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy Eights

Group: General Forum Members
Last Login: Friday, March 28, 2014 2:25 PM
Points: 9,902, Visits: 9,479
karthikeyan (6/9/2009)
RBarryYoung (6/8/2009)

3) The "pre-allocate and Stuff" trick popular with mutable strings is not workable in T-SQL because the STUFF() function in T_SQL is NOT like the function of the same name in some general purpose languages: the T-SQL STUFF() is an RHS (right-hand side) function and NOT an LHS (left-hand side) function. AFAIK, there is no function in SQL that can (physically) write into a pre-existing string.


Oops, I just realized that I have a typo here. I reversed LHS and RHS in this statement. I will go back and fix it...


-- RBarryYoung, (302)375-0451 blog: MovingSQL.com, Twitter: @RBarryYoung
Proactive Performance Solutions, Inc.
"Performance is our middle name."
Post #731871
Posted Tuesday, June 09, 2009 2:54 PM


SSCrazy Eights

SSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy Eights

Group: General Forum Members
Last Login: Friday, March 28, 2014 2:25 PM
Points: 9,902, Visits: 9,479
Oh wait, thats not from here is it? That's from my comment on Phil Factor's blog some months ago, I must have mis-copied the headers when using the QUOTE button here.

-- RBarryYoung, (302)375-0451 blog: MovingSQL.com, Twitter: @RBarryYoung
Proactive Performance Solutions, Inc.
"Performance is our middle name."
Post #731880
Posted Tuesday, June 09, 2009 3:03 PM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Wednesday, March 19, 2014 1:43 AM
Points: 2,020, Visits: 2,515
Now I understood your point #2 and #3. But still #1 is not clear for me...

{(n)*(n+1)/2} cost of the naive implementation to {n +(n)*(n-1)/(2*k)}


Again, if you could explain your formula's for my below example, i think it will be very useful for me as well as for others too.

Declare @str varchar(5)
select @str = ''
select @str = @str + no from mystring
step 1: '' + '1'
step 2: '1' + '2'
step 3: '12' + '3'
step 4: '123' + '4'
step 5: '1234' + '5'


output of O(n) ?
output of O(n^2). It is 15.
output of {(n)*(n+1)/2}. Again 5*(5+1)/2 = 5*3 = 15
output of {n +(n)*(n-1)/(2*k)}.
If K= 1,
=5+(5)*(5-1)/(2*1)
=5+5*2
=5+10
=15

{(n)*(n+1)/2} cost of the naive implementation to {n +(n)*(n-1)/(2*k)}


how?

{(n)*(n+1)/2} = 15
{n +(n)*(n-1)/(2*k)} = 15

There is no difference.

If K= 0,
=5+(5)*(5-1)/(2*0)
"Divide By Zero error occured".

How should i determine 'k' value? on what basis i have to determine the value?

but I could get it down to O(n*Log(n)) time which is still a huge improvement


Is it denotes FOR XML option?

or

Is it denotes Pseudo cursor method?

Can you explain it?







karthik
Post #731888
Posted Tuesday, June 09, 2009 3:18 PM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Yesterday @ 10:44 PM
Points: 22,525, Visits: 30,290
Karthik,

At this point I'd suggest stopping. Unless you are involved in theoretical research, you really don't need to figure out how to solve Big O mathematical formulas, just understand the O(n) algorithms are better than O(n2) algorithms. Know when to recognisize them, for instance a nested loop using cursors versus a set-based cross join for example.

Plus, you still haven't answered Gail's original questions.




Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Post #731897
Posted Tuesday, June 09, 2009 3:19 PM


SSC-Forever

SSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-Forever

Group: General Forum Members
Last Login: Yesterday @ 1:57 PM
Points: 41,558, Visits: 34,479
karthikeyan (6/9/2009)

output of O(n) ?
output of O(n^2). It is 15.


They aren't formulas to have values plugged into and results calculated. They are descriptions of the time-complexity of algorithms.

To say that O(n^2) is 15 is a meaningless statement (plus 5^2 is not 15. It's 25)



Gail Shaw
Microsoft Certified Master: SQL Server 2008, MVP
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass

Post #731898
Posted Tuesday, June 09, 2009 3:38 PM


SSCrazy Eights

SSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy Eights

Group: General Forum Members
Last Login: Friday, March 28, 2014 2:25 PM
Points: 9,902, Visits: 9,479
karthikeyan (6/9/2009)
{(n)*(n+1)/2} cost of the naive implementation to {n +(n)*(n-1)/(2*k)}


how?

{(n)*(n+1)/2} = 15
{n +(n)*(n-1)/(2*k)} = 15

There is no difference.

If K= 0,
=5+(5)*(5-1)/(2*0)
"Divide By Zero error occured".

Right. the problem is that you are quoting my comments on Phil Factor's blog and I could't edit my typos there. Here is a more correct formula:

k*n + (1-k)*(n2 - n)/2

Note that this is still only an approximation because we are waving away the difference between the number of strings and the possibly different lengths of those strings.


How should i determine 'k' value? on what basis i have to determine the value?
I doubt that you could. AFAIK, I am the only person who has ever claimed that K was anything other than 0 (though if I am correct, then there must be folks inside Microsoft who know it also).

but I could get it down to O(n*Log(n)) time which is still a huge improvement


Is it denotes FOR XML option?

or

Is it denotes Pseudo cursor method?
Neither. The naive pseudocursor method is O(n2). The FOR XML method is O(n). I was referring to the very complex modified pseudocursor method that I posted at Phil Factor's blog. It is O(n*log(n)).


-- RBarryYoung, (302)375-0451 blog: MovingSQL.com, Twitter: @RBarryYoung
Proactive Performance Solutions, Inc.
"Performance is our middle name."
Post #731908
Posted Tuesday, June 09, 2009 7:26 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Yesterday @ 9:45 PM
Points: 36,013, Visits: 30,300
O(wtf2)

--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." -- 04 August 2013
(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #731979
Posted Tuesday, June 09, 2009 10:32 PM


SSCrazy Eights

SSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy Eights

Group: General Forum Members
Last Login: Friday, March 28, 2014 2:25 PM
Points: 9,902, Visits: 9,479
Jeff Moden (6/9/2009)
O(wtf2)

Heh. Hey, I'm not the one who asked the question, I'm just trying to answer them.


-- RBarryYoung, (302)375-0451 blog: MovingSQL.com, Twitter: @RBarryYoung
Proactive Performance Solutions, Inc.
"Performance is our middle name."
Post #732022
« Prev Topic | Next Topic »

Add to briefcase «««12345»»»

Permissions Expand / Collapse