mysql 大数据 分页 优化
博客专区 > tystys 的博客 > 博客详情
mysql 大数据 分页 优化
tystys 发表于5个月前
mysql 大数据 分页 优化
  • 发表于 5个月前
  • 阅读 2
  • 收藏 0
  • 点赞 0
  • 评论 0

腾讯云 技术升级10大核心产品年终让利>>>   

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]

 

共有 人打赏支持
粉丝 3
博文 72
码字总数 82272
×
tystys
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: