After some further research on issue 98 (#98), it looks like it's just doing the same thing Rails' version is doing, but adding some special cases for CPK.
However, I didn't find out how/why Rails was not using the subquery.
Instead, I just created a case like "if we're only selecting 1 column, there's no need for the subquery, so don't use it"
Not sure if you're interested in including that or not, but in case you are, I've submitted this pull request.
I don't like to hack around this kind of thing -- it makes the code uglier and harder to follow -- but if everyone else is seeing 5-10 seconds to count records using the subquery vs. almost instantaneous results without the subquery, then I guess it's useful.
If it's just me, well then I guess I'll have to monkey patch it in my project. =)
Thanks for the patch. Do tests pass with this? Conceptually I think this makes sense, the subquery solution was meant to deal with more than 1 field (see my comment to your github issue). Seems like you could special case the 1 field. I'll give it a try on postgresql.
It passes the tests on mysql. I have postgresql and sqlite3 handy now, and the tests pass on those as well.
I did see an error that didn't affect the test output though: