Heim  >  Artikel  >  Datenbank  >  网站数据多了分页慢该怎么办?

网站数据多了分页慢该怎么办?

WBOY
WBOYOriginal
2016-06-07 16:16:451102Durchsuche

网站数据多了分页慢该怎么办?在使用 MySQL 数据库时大偏移量的数据查询是非常慢的,如何通过优化SQL语句来加速分页查询呢? 工具/原料MySQL 数据库Apache (WEB服务器软件)方法/步骤分析传统分页SQL语句 select * from table limit $offset, 10,当$offset非常

   网站数据多了分页慢该怎么办?在使用 MySQL 数据库时大偏移量的数据查询是非常慢的,如何通过优化SQL语句来加速分页查询呢?

  工具/原料MySQL 数据库Apache (WEB服务器软件)方法/步骤分析传统分页SQL语句 select * from table limit $offset, 10,当$offset非常大时,例如980000,这时MySQL数据库就要查询980010条数据,然后扔掉前面980000条,这样速度肯定慢了。考虑这样的SQL语句:select `id` from table limit $offset,10 (id为主键),因为ID字段为主键,的以mysql数据库会使用索引,所以即使要查询980010条数据,速度也是相当快的。即然使用索引可以大大提高mysql数据库查询的速度,,考虑下面的SQL语句:

  select * from table where id >= (select id from table limit $offset,1) limit 10解释上面的SQL语句:

  首先通过带主键索引的查询获取出$offset后的id号,因为使用了索引所以这个子查询很快,然后通过条件查询出 id>=$offset 的10条数据。

  这样的分页方法在大数据量时比传统分页方法快 N 倍。注意事项低版本的MYSQL数据库不支持子查询

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn