May 12, 2005 at 3:28 am
Hello,
I have a strange problem when using the IN keyword in a SQL query.
It works fine when the subquery generating the IN data returns 90.000 rows but as soon as it returns more than 96.000 rows (or something like that) it fails.
I have tried to use EXISTSinstead with a similar result.
JOIN is not an option since the subquery may or may not return more than 1 rows and I only want to be sure that I get atleast one row.
Any ideas?
/Thanks Joel
May 12, 2005 at 3:41 am
Humm nevermind, I found the problem.
My Query didn't return the rown in the same order every time. Doh.
May 12, 2005 at 4:34 am
Rows are never guaranteed to be returned in any specific order unless you specify an order with ORDER BY.
May 12, 2005 at 5:09 am
or you have a clustered index ?!?!
**ASCII stupid question, get a stupid ANSI !!!**
May 12, 2005 at 5:25 am
Thanks for the answers,
I forgot to add the ORDER BY clase I realised that
Still strange that I didn't get the data in order when nothing was updated, inserted or deleted.....I have a clusteder index (since primary key is always clustered)
But the order solved my problems (I'm creating a Page method and the order is therefore vital).
May 12, 2005 at 5:50 am
sushila: What do you mean "have a clustered index"? A clustered index does not guarantee a specific order of rows for a select query.
joel: Sure, it might seem strange that the order changes, but like I said there is nothing that guarantees a specific order. Temporary worktables, hashing, paralellism or lots of other factors might influence the order in which the query engine receives the rows from storage to return them.
May 12, 2005 at 6:08 am
Chris - doesn't a clustered index ensure that the rows are always returned in the order based on the indexed column ?!
Also - a primary key doesn't implicitly mean clustered!
**ASCII stupid question, get a stupid ANSI !!!**
May 12, 2005 at 6:40 am
Run this and find for yourself :
CREATE TABLE [dbo].[TypesCommentairesADPS] (
[PkTypeCommentaireADP] [int] IDENTITY (1, 1) NOT NULL ,
[DescTypeCommentaireADP] [varchar] (50) COLLATE French_CI_AS NOT NULL
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[TypesCommentairesADPS] WITH NOCHECK ADD
CONSTRAINT [PK_TypesCommentairesADPS] PRIMARY KEY CLUSTERED
(
[PkTypeCommentaireADP]
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[TypesCommentairesADPS] ADD
CONSTRAINT [IX_TypesCommentairesADPS_Unique] UNIQUE NONCLUSTERED
(
[DescTypeCommentaireADP]
) ON [PRIMARY] ,
CONSTRAINT [CK_TypesCommentairesADPS_DescTypeCommentaireADP] CHECK ([DescTypeCommentaireADP] '')
GO
Insert into dbo.TypesCommentairesADPS (DescTypeCommentaireADP) values ('Update')
Insert into dbo.TypesCommentairesADPS (DescTypeCommentaireADP) values ('Bug')
Insert into dbo.TypesCommentairesADPS (DescTypeCommentaireADP) values ('Amélioration')
Select * from dbo.TypesCommentairesADPS
/*
Index scan (IX_TypesCommentairesADPS_Unique)
PkTypeCommentaireADP DescTypeCommentaireADP
-------------------- --------------------------------------------------
3 Amélioration
2 Bug
1 Update
(3 ligne(s) affectée(s))
*/
ALTER TABLE dbo.TypesCommentairesADPS
drop constraint IX_TypesCommentairesADPS_Unique
GO
Select * from dbo.TypesCommentairesADPS
/*
Clustered index scan (PK_TypesCommentairesADPS)
PkTypeCommentaireADP DescTypeCommentaireADP
-------------------- --------------------------------------------------
1 Update
2 Bug
3 Amélioration
(3 ligne(s) affectée(s))
*/
drop table TypesCommentairesADPS
go
May 12, 2005 at 6:51 am
No
--
Frank Kalis
Microsoft SQL Server MVP
Webmaster: http://www.insidesql.org/blogs
My blog: http://www.insidesql.org/blogs/frankkalis/[/url]
May 12, 2005 at 6:56 am
No - to which one Frank ?
**ASCII stupid question, get a stupid ANSI !!!**
May 12, 2005 at 7:13 am
I have a table with an ID PK clustered index column...everytime I query it (regardless of whether my select includes the indexed column or any other combination of columns) I always get a "clustered index seek" and ALWAYS in the order of the clustered index column!
I've run different queries against this - used different databases and different tables ...I get the same results!
**ASCII stupid question, get a stupid ANSI !!!**
May 12, 2005 at 7:20 am
Did you run my script???????
May 12, 2005 at 7:23 am
Remi - I KNEW you would ask that & I should've addressed that first...
No - I did not since I already had the setup to run queries...I can see what's happening in your script - after all you have the results displayed each time...
I am merely trying to understand why it works in my situation is all...
**ASCII stupid question, get a stupid ANSI !!!**
May 12, 2005 at 7:26 am
No - to which one Frank ?
Did you ask more than one question in that posting?
--
Frank Kalis
Microsoft SQL Server MVP
Webmaster: http://www.insidesql.org/blogs
My blog: http://www.insidesql.org/blogs/frankkalis/[/url]
May 12, 2005 at 7:29 am
I would suggest you buy a book that explains how sql server's optimizer works under the hood... maybe inside sql can help you. But Frank would be better at suggesting a book...
Viewing 15 posts - 1 through 15 (of 34 total)
You must be logged in to reply to this topic. Login to reply