Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
Stoad
Freaky Yak Linguist
1983 Posts |
Posted - 2003-11-05 : 02:41:37
|
| Taken from there >> http://weblogs.sqlteam.com/davidm/posts/412.aspx#FeedbackSearching for Prime NumbersCREATE TABLE Numbers(Number INT NOT NULL PRIMARY KEY CLUSTERED)GODECLARE @i INTSET @i = 1WHILE @i < 10001BEGIN INSERT Numbers(Number) values (@i) SET @i = @i + 1ENDGO--DavidM solutionSELECT X.Number as PrimesFROM Numbers N CROSS JOIN Numbers XWHERE X.Number%N.Number != 0 AND X.Number > 1 AND N.Number > 0 AND N.Number < X.Number and X.Number%2 !=0GROUP BY X.NumberHAVING (X.Number - Count(*)) = 2--my queryselect * from numbers t where not exists(select 0from numbers tt where t.number%tt.number=0 andtt.number<t.number and tt.number>1)On my machine the 2nd query 5 times faster than the 1st one.The below query 80 times faster than the 1st query:select * from numbers t where not exists(select 0from numbers tt where t.number%tt.number=0 andtt.number<=sqrt(t.number) and tt.number>1)What's wrong with my sql server?LOL... Owais!! I know you are here :) What can you say on this? |
|
|
nr
SQLTeam MVY
12543 Posts |
Posted - 2003-11-05 : 04:39:54
|
| ???Why wouldn't you expect that?The first two queries are donig a lot of duplicate checks that the last is not.how about CREATE TABLE Numbers(Number INT NOT NULL PRIMARY KEY CLUSTERED)GODECLARE @i INTSET @i = 1WHILE @i < 10000BEGINSET @i = @i + 1INSERT Numbers(Number) select @iwhere not exists (select *from numbers tt where @i%tt.number=0 andtt.number<=sqrt(@i)) ENDGOselect * from Numbers==========================================Cursors are useful if you don't know sql.DTS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
|
Stoad
Freaky Yak Linguist
1983 Posts |
Posted - 2003-11-05 : 05:41:48
|
| LOL, Nigel,that's the case.. I would expect this result, but I was confusedby today's David's response (see link above) to my suggestion.PS Of course your code is very good but here I am talking aboutperformance of different retrieving queries. |
 |
|
|
mohdowais
Sheikh of Yak Knowledge
1456 Posts |
Posted - 2003-11-05 : 05:43:21
|
oooooh...i feel woozy now Owais Where there's a will, I want to be in it. |
 |
|
|
Stoad
Freaky Yak Linguist
1983 Posts |
Posted - 2003-11-05 : 05:47:23
|
| Woozy... woozy... by what?? :) |
 |
|
|
mohdowais
Sheikh of Yak Knowledge
1456 Posts |
Posted - 2003-11-05 : 07:07:07
|
by all the numbers i see flying in front of my eyes! Owais Where there's a will, I want to be in it. |
 |
|
|
Stoad
Freaky Yak Linguist
1983 Posts |
Posted - 2003-11-05 : 07:47:09
|
| Lucky Owais.. has seen flying numbers... |
 |
|
|
Arnold Fribble
Yak-finder General
1961 Posts |
Posted - 2003-11-05 : 16:50:23
|
I think David was believing the cost estimates too much. For extra credit, implement a quadratic sieve in T-SQL |
 |
|
|
Stoad
Freaky Yak Linguist
1983 Posts |
Posted - 2003-11-05 : 17:16:06
|
| Who? Me? Have no brains enough... LOL!! |
 |
|
|
|
|
|