I'm just wondering when you do a query and you use ORDER BY
should the fields you use in the ORDER BY
clause always be included in the SELECT
list even if you don't need them? Is this beneficial in some way?
-
2It's completely down to your individual requirements. Do you need the column? Yes/No.– PhilᵀᴹJul 6, 2016 at 10:53
Add a comment
|
1 Answer
There is no benefit to including the ORDER BY columns in the SELECT list. On the contrary, having unrequired columns in the SELECT list incurs a fractional overhead in run time and a larger one in maintenance.
-
Ok thanks. I was thinking by including the data in the result set it lessened the work MySQL needed to do to get the data to know how to sort the results.– BrettJul 6, 2016 at 11:20
-
3@Brett the query needs to read all the columns used, no matter the clause. The SELECT clause only specifies which columns will be sent to the client.– jkavalikJul 6, 2016 at 20:37