Improved MySQL count function