Google Groups Home
Help | Sign in
insert into tbl1 select * from tbl2order by field1 doesnt work!
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  Messages 76 - 100 of 121 - Expand all  -  Translate all to Translated (View all originals) < Older  Newer >
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Kalen Delaney  
View profile  
 More options May 24 2006, 6:09 pm
Newsgroups: microsoft.public.sqlserver.programming
From: "Kalen Delaney" <replies@public_newsgroups.com>
Date: Wed, 24 May 2006 15:09:39 -0700
Local: Wed, May 24 2006 6:09 pm
Subject: Re: insert into tbl1 select * from tbl2order by field1 doesnt work!
Ok, I just reread this. Rick Sawtell,  lance mortensen, and Joseph Jorden do
not say the clustered index sorts the data physically. They just say it
sorts the data. We have all admitted as much. The index sorts the data
logically, so SQL Server can follow pointers from one page to the next, but
if you look at the output from DBCC IND, which shows all the pages that
belong to a table, and which page is previous and which page is next, you
will see the pages are frequently not in order.

--
HTH
Kalen Delaney, SQL Server MVP
www.solidqualitylearning.com

"NumbLock" <pcga...@yahoo.com> wrote in message

news:uPb9cs3fGHA.1324@TK2MSFTNGP04.phx.gbl...


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
NumbLock  
View profile  
 More options May 24 2006, 6:19 pm
Newsgroups: microsoft.public.sqlserver.programming
From: NumbLock <pcga...@yahoo.com>
Date: Wed, 24 May 2006 17:19:56 -0500
Local: Wed, May 24 2006 6:19 pm
Subject: Re: insert into tbl1 select * from tbl2order by field1 doesnt work!
I believe that what you say about future behavior of a clustered indexed
column can change.  I agree with that although I do doubt it.  My last
post was just to illustrate as you said that this is obviously a subject
that has not been fully addressed by Microsoft in their documentation.
I am going on past experience when I say what I said.  I am not 100
years old and don't know all but when I hear all of these supposed
experts who purport to know so much chiding me on a fact that is
published and believed by other supposed *experts* and the proofing
staff at sybex and ms press to be correct, I have an obligation to
explain my point.  Have you considered that your book might be wrong?
And a clustered index scan will produce ordered results based on the
index?

A further quote from Sawtell's book:
Clustered indexes are a lot like a dictionary.  They change the physical
structure of the data to match the constraints of the index.  That means
that if you index on last name, for example Adams would be placed
physically in front of Barnes in the data file.

I don't see why they would change this behavior when physically ordering
the data is the main purpose of the index. You might be right about the
data getting out of order in some instances.  But this goes against the
facts I've stated.  You've done the research.

But I leave it at that.  I do respect your work.  But I know that nobody
is perfect, not me or you or ms press and if I am wrong, I am wrong.


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Kalen Delaney  
View profile  
 More options May 24 2006, 6:32 pm
Newsgroups: microsoft.public.sqlserver.programming
From: "Kalen Delaney" <replies@public_newsgroups.com>
Date: Wed, 24 May 2006 15:32:51 -0700
Local: Wed, May 24 2006 6:32 pm
Subject: Re: insert into tbl1 select * from tbl2order by field1 doesnt work!

> Have you considered that your book might be wrong? And a clustered index
> scan will produce ordered results based on the index?

As I have said, many times a clustered scan will produce ordered results
based on the index. But it is not guaranteed. The optimizer can
theoretically choose not to follow the pointers if you don't say ORDER BY.

The other issue is that the rows and pages are almost never physically
ordered after any inserts or updates have occurred that have incurred
splits. And you can prove it using the commands I have mentioned.

> A further quote from Sawtell's book:
> Clustered indexes are a lot like a dictionary.  They change the physical
> structure of the data to match the constraints of the index.  That means
> that if you index on last name, for example Adams would be placed
> physically in front of Barnes in the data file.

This is most likely true when you first create the index. But the physical
order is not maintained. Yes, inserting into full pages is expensive, but
not THAT expensive. This you can prove also. Find or create a table with
100s of pages. Create a clustered index with fillfactor = 100 so all your
pages are full.
Now insert into the table a few rows with values at the beginning of the
clustered sequence while you are monitoring statistics (SET STATISTICS IO
ON).
You will see that the number of pages accessed is small, whereas if the
pages had to be maintained in physically sorted sequence, you would see the
number of page access would be just about equal to the number of pages in
the table.

--
HTH
Kalen Delaney, SQL Server MVP
www.solidqualitylearning.com

"NumbLock" <pcga...@yahoo.com> wrote in message

news:%237UFG$3fGHA.4892@TK2MSFTNGP02.phx.gbl...


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
NumbLock  
View profile  
 More options May 24 2006, 6:40 pm
Newsgroups: microsoft.public.sqlserver.programming
From: NumbLock <pcga...@yahoo.com>
Date: Wed, 24 May 2006 17:40:14 -0500
Local: Wed, May 24 2006 6:40 pm
Subject: Re: insert into tbl1 select * from tbl2order by field1 doesnt work!
If indexes are properly tuned and the proper fill factor maintained, the
physical reordering will be done at index creation.   There will be
adequate room to add new rows at the appropriate place.  If you have
database with a 100% fill factor and you insert a row at the top it will
take a looooooooong time to insert.  That I have seen.  If it is not
physically reordering the data, what is it doing?  It does not really
matter at this point.  If it is not physically reordering the data, how
do you explain the use of the word physically in reference to a
clustered index?  And why does rick say the following:

Clustered indexes are a lot like a dictionary.  They change the physical
structure of the data to match the constraints of the index.  That means
that if you index on last name, for example Adams would be placed
physically in front of Barnes in the data file.

Hey if I am wrong, I sure have a good reason to be wrong when experts
write books with such topics.


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
NumbLock  
View profile  
 More options May 24 2006, 6:44 pm
Newsgroups: microsoft.public.sqlserver.programming
From: NumbLock <pcga...@yahoo.com>
Date: Wed, 24 May 2006 17:44:30 -0500
Local: Wed, May 24 2006 6:44 pm
Subject: Re: insert into tbl1 select * from tbl2order by field1 doesnt work!
FOr what it is worth, if you used the clustered index on the column
which you are doing a range query on it will come back ordered.  I don't
care what your mileage says.


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
NumbLock  
View profile  
 More options May 24 2006, 6:58 pm
Newsgroups: microsoft.public.sqlserver.programming
From: NumbLock <pcga...@yahoo.com>
Date: Wed, 24 May 2006 17:58:35 -0500
Local: Wed, May 24 2006 6:58 pm
Subject: Re: insert into tbl1 select * from tbl2order by field1 doesnt work!
First of all, your query does not even run as written.  And you totally
missed the point of the discussion.  Observe: (btw I shortened this to
1000 rows, but you get the point)

CREATE TABLE TestStuff
  (
  -- this column is clustered and this is the column queried
  ID uniqueidentifier CONSTRAINT UNQ1 UNIQUE CLUSTERED, -- this column
  Data INT PRIMARY KEY NONCLUSTERED
  )
  SET NOCOUNT ON

  DECLARE @Ind INT
  SET @Ind = 1000
  WHILE @Ind > 0
  BEGIN
  SET @Ind = @Ind - 1
  INSERT INTO TestStuff VALUES (newid(), @Ind)
  END

select id from teststuff WHERE id
between
'00000000-0000-0000-0000-000000000000'
AND
'ffffffff-ffff-ffff-ffff-ffffffffffff'

drop table teststuff

7EA28187-EB9E-4DDB-B285-001B4FF79679
25F052AB-EEF6-491D-A44A-001E77B52765
7C248FDE-D9E1-46C9-8BBE-0049485B225C
D5D3ABBC-E13C-4385-A464-0063BF1DC5EC
9DA0DAC1-C38D-4954-A833-00C3CCA59C6D
CD651222-289B-48B5-813B-00E06E35D50C
AE881326-69DC-4C88-ACE8-00FC4341F02A
50154809-047A-4144-B2D4-011A5CE50F6B
4EAA9CFE-E6DA-4283-9BBD-018801F5D4E2
CA13B3B1-C664-4414-8950-01908BEE210C
5A77D1DA-5FA9-4C49-8CD7-01A35C1E1202
A601A283-A743-4B6A-8047-01B4535FF066
0464924F-2676-4146-9FF8-01E8FE3245DF
EC2667BA-B87D-46E8-B57B-0209E688F081
B9AEEE15-9E18-4ABD-98B7-0215F22EFC58
E9C11DAD-4B9D-49D5-B5AF-0231A9499C5A
F8357918-BF68-4054-A2C9-026A3CBC9592
9E1D6FDA-C574-4AAB-BCDB-02C00CF07DF6
45F624E8-EC94-4726-A500-038753B179D6
E5AD2432-0C1D-4757-B8D1-03A0D200EFED
AE37523F-357C-44FF-9273-03D860D40299
83ABA440-5687-4EAE-80A3-04142E83D1B3
7E625D9A-720C-4A51-A172-04479B9449A9
D7C6E77C-3EF6-4BD0-85D7-0471329860F8
7E4E2005-A0E8-457B-A72F-04A042CFA33A
04A9E1CF-93A6-4E41-9C31-04EDE75A4B64
9AEF33BB-F752-4E98-A2DF-04FD5D3556F7
10F1C2D2-EBD5-45EB-B162-050626A2D215
4884DF77-F2FA-4097-88E8-0539D1DFEF1C
E1EE72C3-5888-4923-B5DD-055A0F6E3D5A
8DAEC0FE-72C9-4D06-8223-05F2C7CC90A9
7C8C8092-F7FC-437A-ADCE-061D74ECC628
CA062ED6-A07B-4F6B-AF2B-065825728844
8DB2D9B9-5324-4FAA-9F2D-065FCBBAD84A
3A6E6B6E-BE65-4DD3-A91A-066F524D0D50
2DEC6A72-844A-4185-B076-0676E1A1146C
9F5D734D-C28C-4086-BD84-06EF31470EFB
3CD31C0D-CEC4-4AC9-BAA0-06EFC296935B
CD22F070-007B-49E4-B4AB-071E54A5DEFC
7731E034-0623-4C2F-B6AD-074B7421ED79
29C5ECF7-AFA7-4771-9660-074EEAE33009
5FA1D878-C625-4A6F-A33E-07DEACE6E673
940ADEE6-B016-4E40-B995-07E187546155
BE7D3F9A-93E7-4774-AD30-07FFD281B656
3367F6F2-DC1D-41F7-B5CF-0815A73D5109
B9D0E8F7-F2CE-444A-B784-08224505D32C
8CC8CCC9-F476-45F3-978D-085A722F7CCC
1965C431-D745-4397-AB25-08CEA472877F
1EDB77DE-1A06-4E52-9098-0951632497EC
D78D0660-C7AE-4C59-8962-0966F4C11137
91603345-F968-45B1-8D97-0A654CE9A279
32152065-2899-4A49-8F6A-0A8043276951
D81B020E-6187-4ADF-8847-0ABEF99F9A8D
230BD9D8-8CA4-42D7-933E-0AC5747705BF
891733EC-FAF7-4525-8598-0AF19AA03DD6
BFB79B98-89D7-4EEF-8AD0-0B70A1482BE7
91AA3900-D39A-41F4-833F-0B7777D73C87
F79DA3E0-848A-403B-AAA2-0BD7D9DEFD8A
8FCD820D-B4A0-4D0F-99E2-0BF1A6FCF1DC
EFC7B9B0-EA04-4DFB-9CD1-0C473C32120D
8A095236-81DF-4406-B75F-0C92C92262B1
185D155D-B6F8-4A6F-A4B0-0CA9A8D8B4CB
5D4ED52F-F9A9-4CD8-9BED-0CFB34683FAB
46A6E101-21AB-454B-9F2D-0D271C22761F
E3F6AAB5-907F-4A27-BCBB-0D7507696491
692F0A30-B41B-41AC-8948-0DBC82301EA5
B13B579A-36EA-41EC-86EC-0DF2D5AB4883
EC62E37E-030E-4C15-B68B-0E18E658BD07
89D5C997-8E54-4BDE-8746-0E42FCA13AE5
D2E7A57C-4D0E-40FA-81D6-0E48385F8516
4AAFF81E-F691-4918-8D69-0E4E13B2BD13
58CB35D7-68BE-42DC-ACBD-0E553F7C3F0D
721F9F74-C351-4D2C-9AAC-0E98B2F83373
707A1691-A40D-46E9-BB92-0F42D40590C2
B0D8F10A-0D90-41A7-AEA5-0F4E74ACE618
8939BE30-95CF-4CC2-A946-0F79E422E329
63AF1B9C-0AB8-4507-9FF1-0F8E88A20168
2C71E475-0F90-462F-B9D2-1042842BFC68
064CDF9E-2DE1-4C8E-81B0-10B931EFA858
495D60DF-EDED-414B-B432-10C641FB9F3C
1E67603E-042D-4CF9-9D59-11E83D5E4AE3
D7F5CB54-BE3A-45C9-B59E-11FED210058C
61F6F918-0429-45EE-97E8-12127CCC5F55
185BB83F-D1C9-449C-B710-121CE652A85A
EC3B8518-21CB-4E0C-98B7-122029FD7FF7
FDD0FC56-5E9D-4949-8B04-122AD337CA47
DF9686CD-60BB-41A5-9FBB-1254EDFB7CDC
F90DDFC7-2FB0-4BF8-ABA1-130B0E401481
2770959E-847F-4179-9888-1336C76332B9
DC9B3996-3A3B-48FC-B2DF-13422E6F1DBA
93096885-86EB-4A17-9928-142CCE865120
F1630CAE-65E2-4C59-9BF7-1445DA84D6B3
8DA64567-9257-485D-B322-1491AAF71E80
A5D99D63-77F5-4BBB-A0A4-14B1EC24E3A3
851FF2B9-5D36-4634-B21D-14B62F25FF4E
8232EC3E-0484-47AA-AA65-14C76C321FCB
78CDDFF6-9594-45AE-8FA5-14F11F6EF753
CCB30866-50B3-4671-97D1-153A9150E113
FC2B3B22-80A7-4FBD-93C5-154DFBA383E6
F387CB43-F00A-4C33-84E2-1591E0554EFD
43A25A17-BDAB-447B-84EB-159A29150107
299DA654-80A4-402A-A1A5-15AE8D8560BF
282FE6C6-1FC3-46BE-81CB-160B66783CB5
648104EB-56FF-45CB-872D-161FD8AE416D
F84E7391-E6F8-490F-9A80-1633F7635CF1
88BE92B1-5989-4F6E-9808-165E243D70E1
1BA375E3-06B3-4517-9258-166480B4C259
F836EC8C-F8FF-4BA8-92AA-1694B1F43DD6
7ECA78B2-7641-4869-B91D-16B486CB7679
07F0AD09-F877-420A-BB75-16C6A75933AB
E6EDFD91-5884-4821-975A-1723DBFB9615
FEE8934F-4BFD-48E5-9C02-1729DC0C92D1
B9F92EC3-7280-4F39-B0D6-1778445E8471
B245CC96-125C-4EF3-81D8-17A13ADDA187
31AE491E-7681-4EB9-AFA5-18193EF9C68E
CA097462-96A8-419D-8528-1833833D16AF
1E9AEBC0-8562-47EB-BD33-18500E3ACC93
772B0621-48A8-42C7-AFEB-187AC7BA8F52
403938C4-ECFD-4FDD-8C3C-18D7DB0AC51F
BB780C8E-EC7E-4432-AAF4-1963D49A66B5
4903BDF4-7526-4F9E-B695-19917B088E97
9321F599-6EEB-41B0-90D0-19B510F429B1
F8898FC6-4F07-4109-84BD-19D2E5D804D0
820D0A8B-75AA-4CAF-BDCA-19E0E58B2498
5470F0AE-594D-4DD1-92F9-1A726658A945
BCB18940-19C7-478D-8ABE-1AEA5A2408A8
721DD063-5D9D-403E-9396-1B54909BF595
B89313D8-5319-438F-8DBC-1BB2B8EDE059
DC240E12-C7F9-45C8-AB0F-1BE875C0E3F8
35443892-4DCC-4BB9-A951-1C847C9194BA
96E92DDC-125D-4E5E-ABD6-1D80FB72C26C
05FE3DB0-D282-403B-A0F1-1DC3988A12CB
42F0FA09-213A-45FE-BA22-1E26FF7434C1
447E4BD2-3B89-4030-BDD4-1EC27DB9817B
D241C029-A573-48AA-B64B-1EC3A34F11A6
1DF08847-B734-4D6E-91F8-1EC5C42C7E58
9E1664C1-DD69-4F3C-AB9C-1EF1BB304D55
0B1C97A2-0C16-49EB-922C-1EFE203D1B0C
60BA35D5-5749-40D8-8943-1F07367AAE2C
9F395382-4A85-44B8-8473-1F09D2621EBD
9F3181EA-04BD-4067-8BC4-1F58FC8FA664
830360E9-A720-411D-9134-1FA85B5C22C9
38A9C0C9-D2F0-4209-AD3A-205582728BEA
956E4A82-B817-4DA4-97B1-207E563EDF41
5F1350F2-C8AE-400A-B026-208DD1A45EC4
920AEC82-15CA-43E0-984A-20AD0EEBB39D
0433CF2C-8A11-453C-A895-20DB3D486A88
3078A367-2863-4A72-828A-20E6A0D8E976
DCF5276B-9472-43EC-8833-20F30D57D42B
A9467A1F-01B1-4D81-AD84-21237A5879AB
F3F7A312-F597-489B-B8F4-21490D2F64AC
EBA6795A-3526-4040-AA5F-214F3E4594FE
C5647D4A-80D7-49DF-A721-2176102B3FFE
9665C76D-013B-42F2-8B0D-227E8674FE52
0960FE21-8C57-40C7-9298-22A2F40EB726
18AAAEBE-31B5-4974-9466-22BD00CD6F71
8EC0F928-E977-47C9-B9EB-22FAFF43C138
D028889E-255B-4C53-B0EB-22FB0019B61C
AD6C9F4B-E484-4ED9-ABEC-233F533BD84D
62E09C82-615A-4A71-9551-235236388860
BC551B69-E42A-48B8-B485-2430EDBF5D57
FCCBF800-02F2-4D50-841F-24A87544BDC8
ADE58D93-AE4C-4D70-BBDC-252AF8ED7796
07CB1B13-8D98-4018-BE53-253BC25395B4
B41A33F1-FBBD-43ED-BBEE-256754B79367
7A819ADF-E418-494A-AA8C-25C6E870C006
D493B5FC-E2F6-45B1-AC29-26349F910729
6BB043DE-9F51-4CFE-BA9D-263519CEAFC8
6234561E-CF16-4604-9DEA-267F4F4B162A
E95730A1-A716-4F88-B920-27093C211AD0
EE6DAA5B-C458-4E35-9F73-2714E2BCFD43
B0D23BC6-D7F0-4C5C-B185-282C9FE77719
12C35C36-4A17-4528-967A-2866BE57BD4A
6FCF4176-E5F1-40D3-ADE1-28942CF7C983
2079BEB8-BCB8-414C-A504-28ADDCF22BCF
D695A90D-9174-4194-BA30-28AE0B79663A
A1A1AE18-23EC-42BA-8015-28B565EE3DE6
AE3216E5-9F5B-4E9F-BBB2-2917390F4ADA
745719CF-D6E1-475F-BF2B-29810AE0AE7E
A89D6991-9AAB-43E2-A56A-2992F82AB1AD
DD6F5CE9-9191-43A2-96EB-29A171E953D0
960E2AB4-060B-40FB-8F9B-2A7E39972A88
7BD19111-933C-49A4-A186-2B12061AA28A
0EC72753-C5DF-47CE-8325-2B1894AB4388
0BBFC4A9-70DC-4643-AAEC-2B287DC87504
3C524E41-C197-415D-BC85-2BD1B13F6FAF
B2239376-8030-46D4-8362-2C30C1E886BB
BAD78826-0A8F-49BC-B951-2C37CFC756E8
93319189-4987-41D1-A379-2C3A5C7C4925
D64BAEE6-29B8-4B20-A8BC-2C57E6E453C8
70922710-A8F6-4BA9-ABF2-2C59168DA3B4
DD3541D0-0949-49A9-A30C-2CCB73DABF13
CBA9AE50-0733-4101-A641-2CE4CA574A88
D79DE65C-E3D9-44F1-8CD0-2D18C62BFB6B
BD480CE6-8254-4F30-9700-2D2338BCDD2C
F0775BBD-015C-460F-88ED-2D43C204FB75
A19683FF-86EC-45AE-A532-2D5A5F00D076
5B5AC302-FD90-438C-B722-2DF017DA46E9
7C7B81C9-DD52-4D64-ACC1-2E047A54E3CE
EAD1263F-E92C-4A53-A0D8-2F5FDFFA4F8A
7652A86F-8854-4DBB-9C3A-2F62F593AC39
36620E1A-6E0D-4D95-93BF-2F7077949004
BA3A0356-F5E6-47FA-94DB-2F86B32E18E7
B5002A1B-A088-470F-9DC0-305DBC1A3C09
44940CBD-674E-4FB1-8F3D-30A5848ADF53
07D454D5-9B97-4E7E-8FFB-310F011F3C7C
02150D75-7FD2-44FF-9430-3130F7B53DA6
6DCDD09B-8029-4231-BB88-318FDFB76911
2B8919EC-6DC0-443C-BD6E-319883A9A4A5
7FFCD08C-DAFA-4061-A9D3-31A58AB56D37
5BA76236-9898-4993-A623-31B109AD3F50
20EEA601-C856-47FC-A524-31C3A1456414
83E9E4BC-D1C6-46A8-88DD-31E5B883A1F9
64606AF3-0B07-4409-A629-31F9CAF004BF
BCE035D6-8E8B-41C7-9E5B-322A0A452807
E1DAD6C2-2202-44A1-8378-322FD6B31184
06102072-3505-4A5E-A45D-32F8740AAF92
28BB0BCA-AAFE-4D51-A6A3-33108189D21A
7E543C76-238B-485F-9806-331A73D9DFA7
846421EE-C424-4951-86FF-332640EA2236
4A583A85-CF14-4CEF-84A0-335B23F8C9D3
561C6E59-E55A-4A6A-A2BA-33BF8D2A3E6C
7104528C-F352-4434-AD09-33CE16043D45
2197F3A7-E3AF-4309-AC5B-340648F3BD7C
E5A12E6A-9073-4D42-AC5D-34309FE4B40D
A65184EC-0920-4475-A1C0-34A7C0EE28B6
FEB38256-2F5C-4E22-80CF-34C3728EB384
45B1AD23-DC8B-4D53-829F-34D10C43D915
F7B572E9-8048-4F98-BFB3-34D3D1DABE9A
EB995A3A-4FD7-4E79-9296-351A17B5D3AC
017458A9-DA1D-4E27-98D1-352C3BC0D834
EA1BC3D4-D9C3-4AF3-96F2-353BDA3D0547
A6BE42F6-3577-4CEF-A972-354C96105F0F
29C12A73-DD8B-4E68-B3DB-355D8ECA7141
57BDD540-6EDE-4EC9-8A56-356FE6C0FFD9
99046C9A-FCA2-4433-86A9-35EDDA014290
6A701E27-5E3B-4A85-9680-3609547ACDDD
2C7BF050-465E-436F-9F38-3614E1B1B9CB
B7D39A7B-7943-44B0-8A01-362FBE05A71A
77F84A94-167C-43CE-94C6-3640CA1F0079
5B0141AA-1E62-450B-BC28-3684FD2F0CC0
854FCA68-65B0-4BDA-93FA-37273CE3C65F
872CF301-7DB2-494F-868B-37D4F0B9CE49
1DC93468-C2B9-4F28-A2FA-37EC40ABAFDC
A110119B-E541-49FF-8CDE-380604103DDF
3757B603-5729-429F-A9E2-3848F182B296
A4EC614D-EA23-44C8-849B-38CDF453310C
74A0255B-27A2-4F6A-B820-38E4CF0DB73F
FD82EC25-9A7D-4543-A488-39213E283B39
9643E5F3-08D7-4D8B-9FB2-392B41E7892B
1F49F4FA-3A66-4A07-AB47-392E25EF63E9 ...

read more »


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Hugo Kornelis  
View profile  
 More options May 24 2006, 7:18 pm
Newsgroups: microsoft.public.sqlserver.programming
From: Hugo Kornelis <h...@perFact.REMOVETHIS.info.INVALID>
Date: Thu, 25 May 2006 01:18:37 +0200
Local: Wed, May 24 2006 7:18 pm
Subject: Re: insert into tbl1 select * from tbl2order by field1 doesnt work!

On Wed, 24 May 2006 14:55:14 -0500, NumbLock wrote:
>So the optimizer can read your mind?  What it says is that the data is
>already ordered if you use an order by.  How much clearer can that be?

Hi Galen,

What this means is NOT that the data will also be returned sorted when
not using an order by.

What this DOES mean is:

- IF you specify an ORDER BY,
  - AND the optimizer decides to use the clustered index,
    - THEN no additional sort step is needed, because traversing the
clustered index pages in logical order (by their pointer chain) will
result in the rows being read in the required order.

This might of course result in a faster execution.

However, the optimizer is still free to choose a different index (though
it will have to add a sort step in that case).

Without the ORDER BY, the optimizer will be even more likely to choose a
different index, since it can do so without the required extra sort
step.

Of course, you are right not to believe what people say on face value.
(Though you should IMO apply the same critical attitude to what you read
in Books Online). The truth can only be found by testing.
In other posts, you've been saying that if you query a column with a
clustered index for a range, the result will always be ordered by that
clustered index. Below is a script to prove that this is not true.

CREATE TABLE test
          (Cluster int NOT NULL,
           Other int NOT NULL,
           Filler char(2000) NOT NULL,
           PRIMARY KEY CLUSTERED (Cluster));
go
CREATE INDEX ix_Other ON test(Other);
go
DECLARE @i int;
SET @i = 1;
WHILE @i < 10000
BEGIN
  INSERT INTO test (Cluster, Other, Filler)
  SELECT @i, CASE WHEN @i % 789 = 0 THEN @i % 987 ELSE 0 END, '';
  SET @i = @i + 1;
END;
go
PRINT 'Without order by:';
SELECT Cluster, Other
FROM   test
WHERE  Cluster BETWEEN 100 AND 4500
AND    Other > 0;
go
PRINT 'With order by:';
SELECT Cluster, Other
FROM   test
WHERE  Cluster BETWEEN 100 AND 4500
AND    Other > 0
ORDER BY Cluster;
go
DROP TABLE test;
go

Results (on SQL Server 2005 and SQL Server 2000 SP4):

Without order by:
Cluster     Other
----------- -----------
3156        195
2367        393
1578        591
789         789
3945        984

With order by:
Cluster     Other
----------- -----------
789         789
1578        591
2367        393
3156        195
3945        984

--
Hugo Kornelis, SQL Server MVP


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Stu  
View profile  
 More options May 24 2006, 7:25 pm
Newsgroups: microsoft.public.sqlserver.programming
From: "Stu" <stuart.ainswo...@gmail.com>
Date: 24 May 2006 16:25:05 -0700
Local: Wed, May 24 2006 7:25 pm
Subject: Re: insert into tbl1 select * from tbl2order by field1 doesnt work!
Aw, Hugo, I thought you had him for a second, but unfotunately he has
apparantly narrowed the scope of his original statement.  In other
words, according to him, a test will only be valid IF the following
conditions are met:

1.  There must be a clustered index
2.  The WHERE clause must reference the columns in the index AND no
other columns covered by an additional index.
3.  The SELECT statement cannot contain aggragates.

Just pointing it out, because it took me several posts of his to
ascertain those conditions.  

Stu


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Dan  
View profile  
 More options May 24 2006, 7:33 pm
Newsgroups: microsoft.public.sqlserver.programming
From: Dan <dsullivanATdanal.com>
Date: Wed, 24 May 2006 23:33:32 +0000 (UTC)
Local: Wed, May 24 2006 7:33 pm
Subject: Re: insert into tbl1 select * from tbl2order by field1 doesnt work!
It works fine on my system. It may be a configuration issue. I am running
on a mulitprocessor system. Without a ORDER BY clause on the ID column the
rows do not come out in order. 1000 rows isn't enough to see the effect,
on my system if there are only 1000 rows the result always comes out in order
on my system too.

Dan

...

read more »


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
NumbLock  
View profile  
 More options May 24 2006, 7:38 pm
Newsgroups: microsoft.public.sqlserver.programming
From: NumbLock <pcga...@yahoo.com>
Date: Wed, 24 May 2006 18:38:29 -0500
Local: Wed, May 24 2006 7:38 pm
Subject: Re: insert into tbl1 select * from tbl2order by field1 doesnt work!
This is what I needed to see.  I agree that you have proved my statement
wrong.  This is what I consider to be *expert*.  Someone who can back up
their claim.  Why don't you write a book, Hugo?


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Hugo Kornelis  
View profile  
 More options May 24 2006, 7:40 pm
Newsgroups: microsoft.public.sqlserver.programming
From: Hugo Kornelis <h...@perFact.REMOVETHIS.info.INVALID>
Date: Thu, 25 May 2006 01:40:10 +0200
Local: Wed, May 24 2006 7:40 pm
Subject: Re: insert into tbl1 select * from tbl2order by field1 doesnt work!
On 24 May 2006 16:25:05 -0700, Stu wrote:

>Aw, Hugo, I thought you had him for a second, but unfotunately he has
>apparantly narrowed the scope of his original statement.  In other
>words, according to him, a test will only be valid IF the following
>conditions are met:

>1.  There must be a clustered index
>2.  The WHERE clause must reference the columns in the index AND no
>other columns covered by an additional index.
>3.  The SELECT statement cannot contain aggragates.

>Just pointing it out, because it took me several posts of his to
>ascertain those conditions.  

>Stu

Hi Stu,

My goal was not to "have" anyone - I believe we are all here to learn or
to share knowledge - both in my case!

I may well have missed some points. I've just been reading the entire
thread and I didn't check every detail.

If the requirements are as you state, it will be hard to post a repro.
But I still feel uncomfortable leaving off the ORDER BY. There is at
least one situation (unfortunately not something you can reproduce on a
test system) that might mess up the order of results. I once caught a
post by someone from the MS team who helped building the optimizer and
query engine that mentioned a rare optimization technique. Suppose you
submit a query that forces a clustered index scan. Just a split second
later, I submit my query that has to scan the same index. If my query
has no ORDER BY, the engine will let my query "look over your shoulder",
picking up data from the pages read for your query. Once your query is
done, my query will go back to the first page of the clustered index and
start reading until it gets to the point where it started looking over
your shoulder.

That would ruin the expected ordering. It would only happen on very busy
systems. And only intermittently. Very nasty bug to track down!!

BTW, I also think that Galen aka NumbLock makes some very valid points.
Way to many books just claim that a clustered index controls the
physical ordering of the data, where in reality it stores data in a
pointer chain that is logically ordered (if you follow the pointers),
but can physically be scattered all around the DB. And way to many books
state things that might mislead one into thinking that the results from
a query that uses a clustered index will always be ordered. The point
that Galen takes in this thread is incorrect - but it's very easy to see
how he has come to his conclusions. The writers of BOL and the authors
of several books are to blame. As are many frequent usenet posters (me
probably included).

--
Hugo Kornelis, SQL Server MVP


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
NumbLock  
View profile  
 More options May 24 2006, 7:42 pm
Newsgroups: microsoft.public.sqlserver.programming
From: NumbLock <pcga...@yahoo.com>
Date: Wed, 24 May 2006 18:42:10 -0500
Local: Wed, May 24 2006 7:42 pm
Subject: Re: insert into tbl1 select * from tbl2order by field1 doesnt work!
My original statement was and is that a if you omit the order by clause
on a column which has a clustered index on it, it would always come back
in index order.  I said an index on another column would effect the
outcome.  But Hugo did not use any other columns in his where clause, he
used only the between clause on the clustered column.  Directly
disproving what I thought to be true.


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
NumbLock  
View profile  
 More options May 24 2006, 7:45 pm
Newsgroups: microsoft.public.sqlserver.programming
From: NumbLock <pcga...@yahoo.com>
Date: Wed, 24 May 2006 18:45:58 -0500
Local: Wed, May 24 2006 7:45 pm
Subject: Re: insert into tbl1 select * from tbl2order by field1 doesnt work!
Further, even if you omit the AND other > 0 it still has the last couple
of rows out of order.  But if you do a select * instead of specifying a
column list, it comes back in index order.  Still, I have been proved
wrong.  Thanks for enlightening me.


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Hugo Kornelis  
View profile  
 More options May 24 2006, 7:48 pm
Newsgroups: microsoft.public.sqlserver.programming
From: Hugo Kornelis <h...@perFact.REMOVETHIS.info.INVALID>
Date: Thu, 25 May 2006 01:48:54 +0200
Local: Wed, May 24 2006 7:48 pm
Subject: Re: insert into tbl1 select * from tbl2order by field1 doesnt work!

On Thu, 25 May 2006 01:40:10 +0200, Hugo Kornelis wrote:

(snip)

> There is at
>least one situation (unfortunately not something you can reproduce on a
>test system) that might mess up the order of results.

Afterthought....

After reading Dan's latest post, I realize that I have missed a
situation. One that would be easier to repro.

Use a multi-processor system with multiple drives. Set up a distributeed
partitioned view (on SQL 2000 or SQL 2005) or a partitioned table (SQL
2005 only) that uses multiple drives. Execute a query with a range
selection on the clustering/partitioning column, such that part of the
data is on one drive and part is on the other drive. I expect SQL Server
to come up with a plan that assigns two processors to each get matching
rows from one of the drives, then merge the results. It would be
unsorted.

Since it's almost 2AM in my time zone, I'll leave it to someone else to
post the actual SQL code to verify this expectation.

--
Hugo Kornelis, SQL Server MVP


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Hugo Kornelis  
View profile  
 More options May 24 2006, 7:54 pm
Newsgroups: microsoft.public.sqlserver.programming
From: Hugo Kornelis <h...@perFact.REMOVETHIS.info.INVALID>
Date: Thu, 25 May 2006 01:54:02 +0200
Local: Wed, May 24 2006 7:54 pm
Subject: Re: insert into tbl1 select * from tbl2order by field1 doesnt work!

On Wed, 24 May 2006 18:38:29 -0500, NumbLock wrote:
>This is what I needed to see.  I agree that you have proved my statement
>wrong.

Hi Galen,

Thanks -- but according to Stu, I missed one of the requirements that
was hidden somewhere in this monster thread.

>  This is what I consider to be *expert*.  Someone who can back up
>their claim.

<blush>

Most of what I know about this comes from reading Kalen Delaney's books.
The rest is partially from reading postings by many other people who
have contributed to this thread and many who have not, and from
experimenting.

>  Why don't you write a book, Hugo?

My native language is Dutch. Almost all Dutch SQL professionals are able
to read English quite easily. The market for Dutch SQL books is
non-existing.

Oh, and I have no time for it either. :-)

--
Hugo Kornelis, SQL Server MVP


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Hugo Kornelis  
View profile  
 More options May 24 2006, 7:58 pm
Newsgroups: microsoft.public.sqlserver.programming
From: Hugo Kornelis <h...@perFact.REMOVETHIS.info.INVALID>
Date: Thu, 25 May 2006 01:58:30 +0200
Local: Wed, May 24 2006 7:58 pm
Subject: Re: insert into tbl1 select * from tbl2order by field1 doesnt work!

On Wed, 24 May 2006 18:45:58 -0500, NumbLock wrote:
>Further, even if you omit the AND other > 0 it still has the last couple
>of rows out of order.

Hi Galen,

(checking)

Dang, you're right.

That one even managed to surprise me!

(It's logical once you understand the reason - I just didn't see it
coming).

Let me know if you need an explanation. I'm off to bed now, but I'll
gladly tell you why this happens tomorrow.

--
Hugo Kornelis, SQL Server MVP


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Kalen Delaney  
View profile  
 More options May 24 2006, 8:06 pm
Newsgroups: microsoft.public.sqlserver.programming
From: "Kalen Delaney" <replies@public_newsgroups.com>
Date: Wed, 24 May 2006 17:06:42 -0700
Local: Wed, May 24 2006 8:06 pm
Subject: Re: insert into tbl1 select * from tbl2order by field1 doesnt work!
But I may be enlisting Hugo's help when I need to come up with examples for
my query tuning chapters!
:-)

--
HTH
Kalen Delaney, SQL Server MVP

"Hugo Kornelis" <h...@perFact.REMOVETHIS.info.INVALID> wrote in message

news:e7s972lqq1so7li8bkvsk4qpbvkkquun4f@4ax.com...


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Kalen Delaney  
View profile  
 More options May 24 2006, 8:07 pm
Newsgroups: microsoft.public.sqlserver.programming
From: "Kalen Delaney" <replies@public_newsgroups.com>
Date: Wed, 24 May 2006 17:07:56 -0700
Local: Wed, May 24 2006 8:07 pm
Subject: Re: insert into tbl1 select * from tbl2order by field1 doesnt work!
This is a great example, Hugo. Thanks!
Might a just suggest putting SET NOCOUNT ON in it, though?

--
HTH
Kalen Delaney, SQL Server MVP

"Hugo Kornelis" <h...@perFact.REMOVETHIS.info.INVALID> wrote in message

news:89p9721ung6nucnn489poe07mkicajmtim@4ax.com...


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Stu  
View profile  
 More options May 24 2006, 8:52 pm
Newsgroups: microsoft.public.sqlserver.programming
From: "Stu" <stuart.ainswo...@gmail.com>
Date: 24 May 2006 17:52:54 -0700
Local: Wed, May 24 2006 8:52 pm
Subject: Re: insert into tbl1 select * from tbl2order by field1 doesnt work!

NumbLock wrote:
> My original statement was and is that a if you omit the order by clause
> on a column which has a clustered index on it, it would always come back
> in index order.  I said an index on another column would effect the
> outcome.  But Hugo did not use any other columns in his where clause, he
> used only the between clause on the clustered column.  Directly
> disproving what I thought to be true.

SELECT Cluster, Other
FROM   test
WHERE  Cluster BETWEEN 100 AND 4500
AND    Other > 0;  -- What do you call this, if not an additional
column?

I'm sorry; I think you must be getting tired.  I fail to see how Hugo's
query is substantially different than the first query I posted (several
hours ago).


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Gert-Jan Strik  
View profile  
 More options May 25 2006, 7:39 am
Newsgroups: microsoft.public.sqlserver.programming
From: Gert-Jan Strik <so...@toomuchspamalready.nl>
Date: Thu, 25 May 2006 13:39:19 +0200
Local: Thurs, May 25 2006 7:39 am
Subject: Re: insert into tbl1 select * from tbl2order by field1 doesnt work!
I don't want to spoil the party, but I don't get it.

Hugo's query proves that the clustered index is not necessarily used if
you have a WHERE clause that filters on the clustered index's column!
Look at the query plan: no clustered index anywhere in sight.

And that is basic knowledge, since the optimizer simply makes a tradeoff
between the cost of using a nonclustered index (and potential bookmark
lookups) or following the clustered index.

In this case the tradeoff is extremely simple, since the nonclustered
index is covering the query and thus will require no bookmark lookups.
Sorry, but that is a no brainer.

Please note that the person you do recognize as being an export (Hugo)
also described that merry-go-round scans (on Enterprise Edition, as of
SQL Server 2000) could mess up the order of the clustered index scan, as
well as his excellent idea that a partitioned table on multiple disks
(as of SQL Server 2005) might do the same.

In the future I expect more innovations in the Storage Engine which will
increase performance and exacerbate this behavior.

Gert-Jan


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
NumbLock  
View profile  
 More options May 25 2006, 9:54 am
Newsgroups: microsoft.public.sqlserver.programming
From: NumbLock <pcga...@yahoo.com>
Date: Thu, 25 May 2006 08:54:43 -0500
Local: Thurs, May 25 2006 9:54 am
Subject: Re: insert into tbl1 select * from tbl2order by field1 doesnt work!
I neglected to mention here that if you leave off the AND other > 0 it
still comes back out of order in the last couple of rows.  I did a
followup post to explain what I meant.

Do not worry.  The coup against your self-professed expert status has
been put down.  You can have your little newsgroup back now.


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Stu  
View profile  
 More options May 25 2006, 10:30 am
Newsgroups: microsoft.public.sqlserver.programming
From: "Stu" <stuart.ainswo...@gmail.com>
Date: 25 May 2006 07:30:20 -0700
Local: Thurs, May 25 2006 10:30 am
Subject: Re: insert into tbl1 select * from tbl2order by field1 doesnt work!
Whatever.  You're the one that claimed you liked a good argument; I
just wanted to point out a fallacythatt developed over the course of
your postings.  You seem to be the only one whose feelings are hurt.

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Numblock  
View profile  
 More options May 25 2006, 11:10 am
Newsgroups: microsoft.public.sqlserver.programming
From: Numblock <pcga...@yahoo.com>
Date: Thu, 25 May 2006 10:10:36 -0500
Local: Thurs, May 25 2006 11:10 am
Subject: Re: insert into tbl1 select * from tbl2order by field1 doesnt work!
Kalen,  I have agreed that Hugo has proved my theory incorrect.
However, based on our argument about the physical ordering of data in a
clustered index, msdn2 states the following at

http://msdn2.microsoft.com/en-us/library/ms190639.aspx

"Clustered indexes are not a good choice for the following attributes:

     * Columns that undergo frequent changes
       This causes in the whole row to move, because the Database Engine
must keep the data values of a row in physical order. This is an
important consideration in high-volume transaction processing systems in
which data is typically volatile."

This statement completely goes against what you and expert David Portas
and others have said about physical ordering of the data in a clustered
index.

I've seen that the results come back skewed in Hugo's example.  Perhaps
you should use some of your contacts at MS to resolve this ambiguity.


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Numblock  
View profile  
 More options May 25 2006, 11:25 am
Newsgroups: microsoft.public.sqlserver.programming
From: Numblock <pcga...@yahoo.com>
Date: Thu, 25 May 2006 10:25:40 -0500
Local: Thurs, May 25 2006 11:25 am
Subject: Re: insert into tbl1 select * from tbl2order by field1 doesnt work!
Yet another quote about the physical ordering of a clustered index:
I understand the concept of a page split, but if a proper fill-factor is
maintained there should be no page splits.  Isn't that correct?

This one from http://doc.ddart.net/mssql/sql70/8_des_05_5.htm

When you create a clustered index, the data in the table is stored in
the data pages of the database according to the order of the values in
the indexed columns. When new rows of data are inserted into the table
or the values in the indexed columns are changed, Microsoft® SQL Server™
may have to reorganize the storage of the data in the table to make room
for the new row and maintain the ordered storage of the data. This also
applies to nonclustered indexes; when data is added or changed, SQL
Server may have to reorganize the storage of the data in the
nonclustered index pages. When adding a new row to a full index page,
SQL Server will move approximately half the rows to a new page to make
room for the new row, a reorganization known as a page split. This can
impair performance and fragment the storage of the data in a table.


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Numblock  
View profile  
 More options May 25 2006, 11:42 am
Newsgroups: microsoft.public.sqlserver.programming
From: Numblock <pcga...@yahoo.com>
Date: Thu, 25 May 2006 10:42:01 -0500
Local: Thurs, May 25 2006 11:42 am
Subject: Re: insert into tbl1 select * from tbl2order by field1 doesnt work!
Yes stu... Both of my feelings are hurt. :)  If you are gonna call
yourself an expert, you will have to prove it in my book.   And I do
love a good argument.  Fact is what I said about your query was true.


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Messages 76 - 100 of 121 < Older  Newer >
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2009 Google