I'm paginating a list view for a model with many fields, so it takes a lot of time to execute MyModel.objects.filter('some filter').count()
, because on SQL level it runs
SELECT COUNT(*) FROM mytable
instead of:
SELECT COUNT(id) FROM mytable
even if I write explicitly
MyModel.objects.only('id').count()
How can I make Django run COUNT(id)
on .count()
?
Update:
I'm using PostgreSQL.