On 02/08/2016 10:21 PM, Venkatesan, Sekhar wrote:
>
> Hi Tom,
>
> You can disregard the "TOP 10" modifier. That was added by me to bring
> down the huge number of results being returned.
>
> Even without the TOP modifier, SQL server is returning rows in sorted
> order (sorting columns based on the r_object_id (1^st ) column I
> think) but PostgreSQL doesn’t.
>
> Is this anything to do with indexes?
>
> So from what I understand, you say in postgres, if the sort order is
> not specified, postgres returns results in any order. Am I right?
>
> -----Original Message-----
> From: Tom Lane [mailto:tgl(at)sss(dot)pgh(dot)pa(dot)us]
> Sent: Tuesday, February 09, 2016 10:30 AM
> To: Venkatesan, Sekhar
> Cc: pgsql-sql(at)postgresql(dot)org
> Subject: Re: [SQL] Question on PostgreSQL DB behavior w.r.t JOIN and
> sort order.
>
> "Venkatesan, Sekhar" <sekhar(dot)venkatesan(at)emc(dot)com
> <mailto:sekhar(dot)venkatesan(at)emc(dot)com>> writes:
>
> > I am seeing this behavior change in postgreSQL DB when compared to
> SQL Server DB when JOIN is performed. The sort order is not retained
> when JOIN is performed in PostgreSQL DB.
>
> What sort order? You did not specify any ORDER BY clause, so the DBMS
> is entitled to return rows in any order it feels like.
>
> I do not know anything about this "top 10" modifier you've got in the
> SQL Server version, but I suspect it's implying a sort order. In
> Postgres, if you want a specific row ordering, you need to say ORDER BY.
>
> regards, tom lane
>
In my experience, this is ofter termed "disc order", implying what ever
order the resultant tuples were discovered while processing the data.
If MSSQL server is giving an order without explicit instruction to do so
you may be incurring an unwanted sort operation. Is (any of) the data
in a "clustered index": iirc that implies an on-disc ordering and the
result set my be reflecting that.