Be careful with queries that call functions for each row; they rarely scale.
This may be a problem for small datasets, but will be if they become large. This should be monitored by regularly conducting tests upon request. Database optimization is just the “Lock and Forget” operation if your data never changes (very rarely).
Sometimes it’s better to enter an artificial primary sort column, for example:
select 1 as art_id, mydate, col1, col2 from mytable where mydate is null union all select 2 as art_id, mydate, col1, col2 from mytable where mydate is not null order by art_id, mydate desc
Then use only result_set["everything except art_id"] in your programs.
This way you do not introduce (possibly) slow functions for each row; instead, you rely on a quick index search in the mydate column. And advanced execution engines can actually run these two queries at the same time, combining them when they are both finished.
source share