Home > Database > Mysql Tutorial > mysql分页优化

mysql分页优化

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-07 15:34:16
Original
1228 people have browsed it

参考:mysql分页优化 大家都知道分页肯定会用到这两种类型的sql: (1) select count(*) from table where 条件 (2) select * from table where 条件 (页码数-1)*每页数 当数据量一大(几百w),不管你是用什么存储引擎,这两种sql都会很恶心了。 对于

参考:mysql分页优化

大家都知道分页肯定会用到这两种类型的sql:

(1) select count(*) from table where 条件
(2) select * from table where 条件 (页码数-1)*每页数
当数据量一大(几百w),不管你是用什么存储引擎,这两种sql都会很恶心了。

对于第一种:
我表示无解,如果你单表几百万、几千万,即使走覆盖索引也要很长时间,带了where条件,无论是MYISAM还是INNODB都会全表扫描,如果你对结果并不是非要精确,走cache吧,因为被坑了很多次,所以我坚持分表处理,尽量保持单表不过百万。

对于第二种:
(1)首先当然是建立索引了,让查询结果在索引中进行;
(2)只返回需要的自动
(3)先获取到offset的id后,再直接使用limit size来获取数据。
随便创建了一张表,插了一百万的数据

[sql] view plaincopy

  1. CREATE TABLE IF NOT EXISTS `article` (  
  2.   `id` int(11) NOT NULL AUTO_INCREMENT,  
  3.   `category_id` int(11) NOT NULL,  
  4.   `namechar(16) NOT NULL,  
  5.   `content` text NOT NULL,  
  6.   PRIMARY KEY (`id`),  
  7. ) ENGINE=InnoDB  DEFAULT CHARSET=utf8  


看看优化效果:

[sql] view plaincopy

  1. #查询花费 38.6875 秒  
  2. SELECT SQL_NO_CACHE *   
  3. FROM  `article`   
  4. LIMIT 800000 , 20  
  5.   
  6. #查询花费 0.9375 秒  
  7. SELECT SQL_NO_CACHE id, category_id  
  8. FROM  `article`   
  9. LIMIT 800000 , 20  
  10.   
  11. #查询花费 0.3594 秒  
  12. SELECT SQL_NO_CACHE id, category_id  
  13. FROM  `article`   
  14. WHERE id >= (SELECT id FROM  `article` LIMIT 800000 , 1)   
  15. LIMIT 20  
  16.   
  17. #查询花费 0.0000 秒  
  18. SELECT SQL_NO_CACHE id, category_id  
  19. FROM  `article`   
  20. WHERE id  
  21. BETWEEN 800000   
  22. AND 800020   


windows下测试可能存在一定误差,当然还有很多其他的方法如建立索引表等待。

最后:对于大型平台或系统,用框架啊、什么ORM就行不通了,会让你尴尬的!


来源: http://www.lai18.com/content/312862.html

Related labels:
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
MySQL stops process
From 1970-01-01 08:00:00
0
0
0
Error when installing mysql on linux
From 1970-01-01 08:00:00
0
0
0
phpstudy cannot start mysql?
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template