Avoid ordering when counting.

This not only optimizes the query, but also avoid errors caused when
ordering by fields set in the WHERE clause, because count() overrides
them by default.
Merge pull request #3157 from bar/3.0-order-in-count

Avoid ordering when counting.