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)
 Trying to order by date column (I'm desperate)

Author  Topic 

cesark
Posting Yak Master

215 Posts

Posted - 2005-03-17 : 12:06:54
I am trying to order this SP for paging by date, but I don’ t know why the normal way of ordering the results it doesn’ t work. I am becoming crazy!

I tried this:

USE market
GO
ALTER PROCEDURE offers_list
@CurrentPage int, @PageSize int, @TotalItems int output
AS
SET NOCOUNT ON

DECLARE @v_FirstOID bigint
DECLARE @v_LastOID bigint
DECLARE @v_FirstDate datetime
DECLARE @v_LastDate datetime
DECLARE @v_MaxRowNumber int

SET @v_MaxRowNumber = @CurrentPage * @PageSize
SET @TotalItems = 0
SELECT @TotalItems = @TotalItems + 1
,@v_FirstOID = CASE WHEN (@TotalItems = @v_MaxRowNumber - (@PageSize - 1)) THEN abt.Offer_id
ELSE @v_FirstOIDEND
,@v_FirstDate = CASE WHEN (@TotalItems = @v_MaxRowNumber - (@PageSize - 1)) THEN abt.theDate
ELSE @v_FirstDate END
,@v_LastOID = CASE WHEN @TotalItems <= @v_MaxRowNumber THEN abt.Offer_id
ELSE @v_LastOID END
,@v_LastDate = CASE WHEN @TotalItems <= @v_MaxRowNumber THEN abt.theDate
ELSE @v_LastDate END
FROM
(
SELECT Offer_id, theDate
FROM Offers AS ofe
INNER JOIN TheUsers As us On ofe.User_num = us.User_id
INNER JOIN Cities As city On ofe.city_num = city.City_id
) AS abt

ORDER BY abt.theDate Desc

SELECT Offer_id, theDate, Title, us.Company_name, city.City_name FROM Offers AS ofe
INNER JOIN TheUsers As us On ofe.User_num = us.User_id
INNER JOIN Cities As city On ofe.city_num = city.City_id
Where (theDate <= @v_FirstDate) AND (theDate >= @v_LastDate)

ORDER BY theDate Desc
SET NOCOUNT OFF
GO


But in this way any row is returned. Instead, if I order by Offer_id then works perfect:

...
ORDER BY abt.Offer_id Desc

SELECT Offer_id, theDate, Title, us.Company_name, city.City_name FROM Offers AS ofe
INNER JOIN TheUsers As us On ofe.User_num= us.User_id
INNER JOIN Cities As city On ofe.city_num = city.City_id
WHERE (theDate <= @v_FirstDate) AND (theDate >= @v_LastDate)

ORDER BY Offer_id Desc
...



And also works in this way:

...
ORDER BY abt.Offer_id Desc

SELECT Offer_id, theDate, Title, us.Company_name, city.City_name FROM Offers AS ofe
INNER JOIN TheUsers As us On ofe.User_num= us.User_id
INNER JOIN Cities As city On ofe.city_num = city.City_id
WHERE (theDate <= @v_FirstDate) AND (theDate >= @v_LastDate)

ORDER BY theDate Desc
...



And if I make a simple query in Offers table, ordering by date, also works.
Somebody can help me please? I don’ t know why I can’ t order by date in this SP. What it may happen?

Thank you

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2005-03-17 : 12:21:40
what you are trying to do appears to be very similiar (but unneedlessly more complicated and less efficient) then a technique I discuss here:

http://weblogs.sqlteam.com/jeffs/archive/2003/12/22/672.aspx

try that out and see if it helps.

remember -- when you do paging in a table, you must order on columns that are unique for every row in the table. if each row doesn't have a unique date, you need a secondary sort (i.e., by ID) that will allow each row to be uniquely identified. Otherwise, the paging will be random and not work reliably. THis is a very important concept to make sure you understand when working with relational databases.

- Jeff
Go to Top of Page

cesark
Posting Yak Master

215 Posts

Posted - 2005-03-18 : 05:06:57
Thank you so much for your suggestion, although I already tested it some days ago without success, your article and explanation posted has given to me more points of view than before, and thanks to it now I know how to use the method of paging you proposed. But I continue without knowing how to order by date in the SP I posted.

On the other hand, I am very confused about which method of paging has the best performance. I am not an expert in Sql, and I don’ t know how to test which is more efficient. But I am very surprised about the opinions that some ‘experts’ has about the same method. Another person, who seems to have a lot of experience, proposed to me the method of paging I posted, and has told me about the ‘rowcount’ method the follow: (literal)

quote:
I have run some preliminary tests and found this.

potential resultset i.e. rowcount <1000
The solution I posted is twice as quick for the same resultset.

On resultset rowcount > 10000
The solution I posted was over 2.5 times quicker.

Another thing to note, I tried this out with one of our more complex queries which contains mutliple subqueries and a function call and I noticed this.
Rowcount is applied to the inner resultsets aswell, meaning it can potentially skew the results.
So it goes down to again, whatever suits you and is easiest to work with.



And you said about the method of this person:

quote:
what you are trying to do appears to be very similiar (but unneedlessly more complicated and less efficient) then a technique I discuss here:
(rowcount)


As you can see, for a person who doesn’ t know how to test which is more efficient, it is very confusing.

Let me know what do you think about my doubts.

Again, thank you very much,
Cesar
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2005-03-18 : 10:32:18
when you implement any kind of technique, it is important to take a minute to analyze it and make sure it meets your needs and works as you expect.

the main thing to understand is how many rows of data each solution needs to process to return the results for particular pages. Also, to be sure that it returns reliable results. As mentioned here and in my article, you CANNOT page on just the date column alone to return deterministic results if the date column is not unique for every row.

So just fitlering and sorting on 1 column is certainly "faster" than doing it on two columns (date first and then ID next) but you are not returning reliable or consistent results. this is a very, very important concept to understand.

The main point is: If you are comparing method A to method B, and method A works and method B doesn't, it doesn't matter if method B is 1,000 times faster than method A.

- Jeff
Go to Top of Page
   

- Advertisement -