Can someone kindly explain to me why when I put order by date asc or desc to any query statement. It will slows down the performance by significantly a lot?
Usually the statement will be like this
select * from table where ... order by modified_data asc
what is the best way to mitigate this issue? The moment I remove the order by date, the performance will improve a lot.
It depends on the query plan, whether
modified_datein indexed, and what other predicates you have. In general, though, adding anORDER BYrequires that Oracle sort the results before returning them. That, in turn, generally requires that Oracle fully materialize the result set before it can return any rows. If you are measuring the time required to return the first row, rather than the time required to return the last row, that magnifies the effect.This isn’t really an issue that you mitigate in general. If you need a sorted result set, you incur the overhead of sorting that result set. Otherwise, you don’t. In specific cases, there may be indexes you can add that would allow Oracle to fetch the result in sorted order rather than physically sorting the result. But that depends on a host of very query-specific factors that you haven’t mentioned.