Niccolo Rigacci wrote:

I get the results in about 108 seconds (8060 rows).

If I issue the SELECT alone (without the CURSOR) I get the same results in less than 1 second.

By trial and error I discovered that adding an "ORDER BY toponimo" clause to the SELECT, boosts the CURSOR performances so that they are now equiparable to the SELECT alone.

Is there some documentation on how an ORDER can affect the CURSOR in a different way than the SELECT?

I think you're misunderstanding exactly what's happening here. If you ask for a cursor, PG assumes you aren't going to want all the results (or at least not straight away). After all, most people use them to work through results in comparatively small chunks, perhaps only ever fetching 1% of the total results.

So - if you ask for a cursor, PG weights things to give you the first few rows as soon as possible, at the expense of fetching *all* rows quickly. If you're only going to fetch e.g. the first 20 rows this is exactly what you want. In your case, since you're immediately issuing FETCH ALL, you're not really using the cursor at all, but PG doesn't know that.

So - the ORDER BY means PG has to sort all the results before returning the first row anyway. That probably means the plans with/without cursor are identical.

Of course, all this assumes that your configuration settings are good and statistics adequate. To test that, try fetching just the first row from your cursor with/without the ORDER BY. Without should be quicker.

  Richard Huxton
  Archonet Ltd

---------------------------(end of broadcast)---------------------------
TIP 1: subscribe and unsubscribe commands go to [EMAIL PROTECTED]

Reply via email to