您的位置:首页 > 大数据

mysql 大数据 分页 优化

2017-07-24 00:00 169 查看

1. 不用多扫描数据

Paging without discarding records

尽量避免物理删除.

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

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

前提:根据ID分页.

用户名分页:

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


2. 延迟关联

Try using a Deferred Join

用户分页.

每页十人.

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

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

SELECT id, name, address, phone FROM customers ORDER BY name LIMIT 10 OFFSET 990;


解决方案:延迟关联

deferred join.

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

只去取主键id

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


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

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);


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.

保存页面数据

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


Hiring? MySQL DBA Interview Guide for Candidates & Managers.

Or with place column something like this:

保存页面列数

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


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: