I need to handle pagination in PHP and Mysql, Problem statement is as follows:
I have a table with hundreds of thousands records of relationship of 2 foreign keys say "User" and "Item", and this table changes frequently having adding or removing Item for a particular User. Now I would like to list all the Items to the User with pagination and total count in heading. My table is MyISAM and use SQL_CALC_FOUND_ROWS as well. Table structure is as follows with huge cardinality.
CREATE TABLE `USER_ITEMS` (
`ID` int(11) NOT NULL,
`ITEMS` int(11) unsigned NOT NULL DEFAULT '0',
`USER` int(11) unsigned NOT NULL DEFAULT '0',
`TYPE` char(1) NOT NULL DEFAULT '',
`TIME` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`SEEN` char(1) NOT NULL,
`FILTERED` char(1) NOT NULL,
PRIMARY KEY (`ID`),
UNIQUE KEY `IND1` (`USER`,`ITEMS`),
KEY `USER` (`USER`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
Solution I gave a try:
- I perform a select on Mysql using limit for all pagination,but we get either duplicate Item if removed on real time or missed Item if any added while pagination.Also many a times it is very slow query due to order by and other conditions leading performance hit. This is simple Select query with where, order by and limit .
- I used Memcache to store the Items of the pages he viewed and get Items excluding the ones in Memcache during pagination, but this leads to slow query as well.This is Select query with where with ITEMS NOT IN , order by and limit.
Issue are mainly accuracy of data, performance in terms of query execution time and network choking, php memory exhaust.
I would like to know if these are the general practises for the same, or do we have something better either implementation/ engine/ cache for such scenarios.