FirebirdSQL / firebird

Firebird server, client and tools
https://firebirdsql.org
1.26k stars 217 forks source link

Cost-based decision between ORDER and SORT plans #8316

Closed dyemanov closed 1 week ago

dyemanov commented 1 week ago

Implement cost calculation for the ORDER vs SORT choice when ordering is required for the query. The visible effects are:

1) ORDER ... INDEX plan is almost never used, being inferior to SORT in most cases -- maybe except some queries with OPTIMIZE FOR FIRST ROWS hint, explicit or implicit (e.g. SELECT FIRST). This is already proven in practice to work better. 2) SORT plan may be preferred to ORDER for the default OPTIMIZE FOR ALL ROWS scenario if its estimated as being cheaper. 3) Queries should not imply that ORDER plan will be used always if a suitable index exists. We have a number of QA tests that are affected by this change.

The cost calculation will be adjusted once more statistics will be available to the optimizer (e.g. clustering factor).