mysql分页查询优化:

在MySQL中分页很简单,直接LIMIT a,b 就可以了。
但是数据增大到千万时,limit到后面也页就相当拖拉机了。

下面给出了分页上层表方案,可以缩短到原来时间的1/pagesize
原表:
CREATE TABLE `t_wordlist` (
`id` int(11) NOT NULL auto_increment,
`qv` decimal(10,2) NOT NULL,
`name` varchar(20) NOT NULL,
`ctime` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8

原表总记录数:
mysql> select count(*) from t_wordlist;
+----------+
| count(*) |
+----------+
| 10324567 |
+----------+
1 row in set (0.00 sec)

分页上层表:
CREATE TABLE `t_wd_ids` (
`id` int(11) NOT NULL,
`wd_id` int(11) NOT NULL,
PRIMARY KEY (`id`,`wd_id`),
KEY `idx_id` (`id`),
KEY `idx_wd_id` (`wd_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;


插入分页表数据。
mysql> insert into t_wd_ids select ceil(id/20),id from t_wordlist;
Query OK, 10324567 rows affected (1 min 45.19 sec)
Records: 10324567 Duplicates: 0 Warnings: 0


试验对比:
用普通LIMIT来实现分页。
mysql> select * from t_wordlist where 1 limit 20;
20 rows in set (0.01 sec)


用分页表来实现分页:

mysql> select a.* from t_wordlist as a inner join t_wd_ids as b where a.id = b.wd_id and b.id = 1;
20 rows in set (0.00 sec)


取最后一页的数据:
mysql> select * from t_group where 1 limit 10324547,20;
20 rows in set (4.88 sec)

分页表:

mysql> select a.* from t_wordlist as a inner join t_wd_ids as b where a.id = b.wd_id and b.id = 516227;
20 rows in set (0.01 sec)