I have read a lot of answers about performance of counting records, for example https://stackoverflow.com/a/1332730/7906257, and all says there is no simple solution. I use InnoDB and there is CL_GAME
table with PK
primary key. And the most important thing that I have a special use case, which definitely can be optimized: records are never deleted from the table, but only added. Is it possible to say MySQL to skip internal validations (like explained in the above link) while counting records:
SELECT COUNT(pk) FROM CL_GAME WHERE pk <= 1072370;
There are some ideas, which are not perfect:
One obvious trick is to normalize table and remove any gaps between pk's. So pk reflects count of records before. But it looks dangerous, because some reverted transaction can break it (ok, there are some non-trivial solutions like https://www.percona.com/blog/2011/11/29/avoiding-auto-increment-holes-on-innodb-with-insert-ignore/)
Another idea to have additional table, that contains pairs <pk, count> where count is number of records with pk <= pk from the pair. Therefore it's possible to count only part of table and use pre-calculated values. But I'd prefer to avoid any additional structures/caches, because it takes time for implementing, validating and supporting them