mysql分页查询优化:
在MySQL中分页很简单,直接LIMIT a,b 就可以了。
但是数据增大到千万时,limit到后面也页就相当拖拉机了。
下面给出了分页上层表方案,可以缩短到原来时间的1/pagesize
原表:
CREATE TABLE
1 | 1` |
t_wordlist
1 | 1` |
(
1 | 1` |
id
1 | 1` |
int(11) NOT NULL auto_increment,
1 | 1` |
qv
1 | 1` |
decimal(10,2) NOT NULL,
1 | 1` |
name
1 | 1` |
varchar(20) NOT NULL,
1 | 1` |
ctime
1 | 1` |
timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
PRIMARY KEY (
1 | 1` |
id
1 | 1` |
)
) ENGINE=MyISAM DEFAULT CHARSET=utf8
原表总记录数:
mysql> select count(*) from t_wordlist;
+———-+
| count(*) |
+———-+
| 10324567 |
+———-+
1 row in set (0.00 sec)
分页上层表:
CREATE TABLE
1 | 1` |
t_wd_ids
1 | 1` |
(
1 | 1` |
id
1 | 1` |
int(11) NOT NULL,
1 | 1` |
wd_id
1 | 1` |
int(11) NOT NULL,
PRIMARY KEY (
1 | 1` |
id
1 | 1` |
,
1 | 1` |
wd_id
1 | 1` |
),
KEY
1 | 1` |
idx_id
1 | 1` |
(
1 | 1` |
id
1 | 1` |
),
KEY
1 | 1` |
idx_wd_id
1 | 1` |
(
1 | 1` |
wd_id
1 | 1` |
)
) 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)