"mac_man2008@yahoo.it" <mac_man2008@yahoo.it> wrote:
> I need to know, from an algorithmic point of view, in which cases
> sorting is invoked.
Well, I think the only accurate answer to that is "when the
estimated cost of a plan using a sort is lower than the estimated
cost of any alternatives". There are cases where you can be sure a
sort will be used, like when you have an ORDER BY clause and there
is no index which can scan rows in that order; but there are a lot
of situations where plans which involve sorts are in competition
with plans which don't. The plan chosen can depend on such things
as how bloated your table is, how close the order of the tuples in
the heap corresponds to the order of a particular index, how many
rows are in which tables, what work_mem is set to, etc.
Are your really looking to categorize the types of queries where
sorting is *invoked*, or the ones where it is *considered*? Or
perhaps only those where it is *required*, since there are no
possible plans without sorting?
-Kevin