mysql 大数据 分页 优化

1. 不用多扫描数据

Paging without discarding records

尽量避免物理删除. 

select id, name, address, phone
FROM customers
WHERE id > 990
ORDER BY id LIMIT 10;

(保存最后一个|上一页的id)

前提:根据ID分页.

用户名分页:

[code]
SELECT id, username
FROM customers
WHERE username > ‘shull@iheavy.com’
ORDER BY username LIMIT 10;
[/code]

  

2. 延迟关联

Try using a Deferred Join

用户分页.

每页十人.

分第100th 页时,   LIMIT 10,起始点是 OFFSET 990.

服务器需要先去扫描这990条记录, 

 

[code]
SELECT id, name, address, phone FROM customers ORDER BY name LIMIT 10 OFFSET 990;
[/code]

解决方案:延迟关联

deferred join.

The inside piece just uses the primary key. An explain plan shows us “using index” which we love!

只去取主键id

[code]
SELECT id
FROM customers
ORDER BY name
LIMIT 10 OFFSET 990;

[/code]

Now combine this using an INNER JOIN to get the ten rows and data you want:

[code]
SELECT id, name, address, phone
FROM customers
INNER JOIN (
SELECT id
FROM customers
ORDER BY name
LIMIT 10 OFFSET 990)
AS my_results USING(id);

[/code]

That’s pretty cool!

3.保存页面| 

Maintain a Page or Place column

Another way to trick the optimizer from retrieving rows it doesn’t need is to maintain a column for the page, place or position.

Yes you need to update that column whenever you (a) INSERT a row (b) DELETE a row ( c) move a row with UPDATE.

This could get messy with page, but a straight place or position might work easier.

保存页面数据

[code]
SELECT id, name, address, phone
FROM customers
WHERE page = 100
ORDER BY name;

[/code]

Hiring? MySQL DBA Interview Guide for Candidates & Managers.

Or with place column something like this:

保存页面列数

[code]
SELECT id, name, address, phone
FROM customers
WHERE place BETWEEN 990 AND 999
ORDER BY name;

[/code]

 

转载于:https://my.oschina.net/u/3255899/blog/1486578

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值