I have a large database table. I need to to know exactly how many rows the table contains in very short time. The user will request the number everytime, on every page of my website. Its not a rare request.
I have following methods:
- Using primary key, so by using MAX(id) I would already have the row-count. But it is O(n). On my table with > 1mio rows its slow.
- I could increase a number in a text file each time an Item gets inserted
- I could switch to MyISAM since it saves the row-COUNT, but problem is its slower in selecting. In my app I mostly select large amounts.
- SELECT COUNT(*) FROM table / SELECT * FROM table ORDER BY id DESC LIMIT 1; But they are very slow or messy for very large database tables.
Which method should I use, or are there better methods?