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.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Incomplete records fetched

Author  Topic 

Bien
Starting Member

12 Posts

Posted - 2003-09-25 : 03:40:08
My table has a PK, 3 indexes and more then 50million records.
The PK has 8fields (pd_CtrlEntity, pd_BkUnit, pd_GLCode, pd_ProdCode, pd_BaseCurr, pd_SourceCurr, pd_EffectiveDate, pd_JEGrpNumber, pd_JEGrpDate)

Index 1 is pd_CtrlEntity, pd_JEGrpNumber, pd_EffectiveDate
Index 2 is pd_CtrlEntity, pd_BkUnit, pd_EffectiveDate
Index 3 is pd_CtrlEntity, pd_GLCode, pd_EffectiveDate


When I use this statement:
Select Top 300 * FROM GLtPostedDetails WITH (INDEX(PK_GLtPostedDetails)) WHERE pd_CtrlEntity = 'EBRB' and pd_Bkunit >= '0101' and pd_EffectiveDate >= '07/01/2001' and pd_EffectiveDate <= '07/07/2001' and pd_JEGrpNumber = 'JESI02109A' and pd_JEGrpDate = '07/04/2001'

the records fetched are okay.

When I use this:
Select Top 300 * FROM GLtPostedDetails WHERE pd_CtrlEntity = 'EBRB' and pd_Bkunit >= '0101' and pd_EffectiveDate >= '07/01/2001' and pd_EffectiveDate <= '07/07/2001' and pd_JEGrpNumber = 'JESI02109A' and pd_JEGrpDate = '07/04/2001'

It used index 2 and the records fetched are incomplete.

What seems to be the problem?

Please help me. Thanks!



**** **** ****

Page47
Master Smack Fu Yak Hacker

2878 Posts

Posted - 2003-09-25 : 07:11:45
What do you mean by "incomplete"? Do you not get a 300 rows in your rowset?

Jay White
{0}
Go to Top of Page

samsekar
Constraint Violating Yak Guru

437 Posts

Posted - 2003-09-25 : 07:14:52
It is because of ordering of data, which uses different indexes. Try count(*) instead Top 300, it should return equal counts Or try order by anycolumn

Sekar
~~~~
Success is not a destination that you ever reach. Success is the quality of your journey.
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2003-09-25 : 09:28:05
It's gotta be that damn miracle thing again....

Did you bounce the box?

What's the total length of the row?

Do you let SQL Server allocate it's memory, or did you put an upper boundry to it?

Are there multiple applications on the box?

What interface are using to return your results? QA? VB? Java?



Brett

8-)

SELECT @@POST FROM Brain ORDER BY NewId()

That's correct! It's an AlphaNumeric!
Go to Top of Page

Bien
Starting Member

12 Posts

Posted - 2003-09-25 : 20:51:18
quote:
Originally posted by Page47

What do you mean by "incomplete"? Do you not get a 300 rows in your rowset?

Jay White
{0}



I get 300rows but it returned the wrong set of results..

for example:
When I use the statement where I put the PK, the result is:

A
B
C
D
E
F
G
etc..

But when I don't specify which index to use, the result is:
A
B
K
L
M
N
etc..

the records, C, D, E, F, G, H, I, J are missing.

What seems to be the problem? Please help! Thanks!

---------------------------------------------------------

**** **** ****
Go to Top of Page

MichaelP
Jedi Yak

2489 Posts

Posted - 2003-09-25 : 21:00:31
Is it me, or is this caused by not having an order by on the queries??

Oh Sekar already said this. I bet if you did an ORDER BY pd_CtrlEntity on your queries, it would come out right.

Let us know!

Michael




<Yoda>Use the Search page you must. Find the answer you will.</Yoda>
Go to Top of Page

Bien
Starting Member

12 Posts

Posted - 2003-09-25 : 21:05:59
quote:
Originally posted by samsekar

It is because of ordering of data, which uses different indexes. Try count(*) instead Top 300, it should return equal counts Or try order by anycolumn

Sekar
~~~~
Success is not a destination that you ever reach. Success is the quality of your journey.



Yup, if I put order by, the results are okay. but, I can't put order by in the select statement because it takes longer to fetch the records even though there are indexes..

**** **** ****
Go to Top of Page

robvolk
Most Valuable Yak

15732 Posts

Posted - 2003-09-25 : 21:15:23
Bien-

There's no magic here:

TOP WITHOUT AN ORDER BY CLAUSE IS MEANINGLESS.

Index hint or not, you WILL NOT get consistent results using TOP without an ORDER BY. Sorry if I sound harsh, but it's the truth. It doesn't matter if it is slow or not, you need the ORDER BY clause if you need consistent results.
Go to Top of Page

Bien
Starting Member

12 Posts

Posted - 2003-09-25 : 21:50:41
robvolk, thanks. Now I know why I get inconsistent results.

But, how does sql server get the results? If my records are physically ordered like this:
A
B
C
D
E
F
G
up to Z

Doesn't it get the records based on the physical order?

**** **** ****
Go to Top of Page

robvolk
Most Valuable Yak

15732 Posts

Posted - 2003-09-25 : 22:14:20
NO.

Physical order is meaningless in a relational data store. Only the VALUE of a piece of data matters.

Think of musical chairs. The people are the same, no matter which chair they sit in. Their names, hair color, weight, height, etc. do not change simply because they sit in another chair. Therefore, a row of data is the same no matter where it may be physically stored on the disk, or in relation to other rows of data.
Go to Top of Page

SamC
White Water Yakist

3467 Posts

Posted - 2003-09-25 : 22:19:52
Great smileys Bien.

Sam

Go to Top of Page

Bien
Starting Member

12 Posts

Posted - 2003-09-26 : 01:59:31
Okay...

Another question: Assuming I would like to get the TOP 300 records only. Does sql server get all the records that satisfy a certain criteria (WHERE clause), then just take the TOP 300 records, or does it check row by row, then when it gets to the 300th record, it stops?

I appreciate all your replies. Thanks.



**** **** ****
Go to Top of Page

AndrewMurphy
Master Smack Fu Yak Hacker

2916 Posts

Posted - 2003-09-26 : 06:37:09
I think the former is true....get all records that satisfy the where condition and then only display the required amount. putting an "order by" gets the data into a predictable order (and to do that it has to process ALL the data) and then TOP reduces the amount of data returned to the user.

In a way TOP is acting like a "SET ROWCOUNT x" statement.
"Sometimes (but not always)" they are swopable in the effects they can cause depending on the SQL statement that is used.
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2003-09-26 : 09:54:49
Ok...Good stuff...but I gotta ask...

What's 300 out of 50 million?

Isn't there a target population you want....

It seems all pretty arbitrary to me...

What are you really trying to do...

In business terms...no tech talk...




Brett

8-)

SELECT @@POST FROM Brain ORDER BY NewId()

That's correct! It's an AlphaNumeric!
Go to Top of Page
   

- Advertisement -