Optimize statistics queries. See: #45