Home >Database >Mysql Tutorial >MySQL paging query method for millions of data and its optimization suggestions
Database SQL optimization is a commonplace issue. When faced with paging queries with millions of data volumes, what are some good optimization suggestions? Some commonly used methods are listed below for your reference and learning!
Method 1: Directly use the SQL statement provided by the database
Statement style: In MySQL, the following methods can be used: SELECT * FROM table name WHERE id_pk > (pageNum*10) LIMIT M
Adapted to scenarios: Suitable for situations with large amounts of data (number of tuples) 10,000)Statement style: In MySQL, the following methods are available: SELECT * FROM table Name WHERE id_pk > (pageNum*10) ORDER BY id_pk ASC LIMIT M
Suitable for scenarios: Suitable for situations with large amounts of data (tens of thousands of tuples). It is best that the column object after ORDER BY is the primary key Or the only reason why the ORDERBY operation can use the index to be eliminated but the result set is stable (for the meaning of stability, see method 1)The first question mark represents pageNum, the second one? Indicates the number of tuples per page
Statement style: In MySQL, the following method can be used: PREPARE stmt_name FROM SELECT * FROM table name WHERE id_pk > (?* ?) ORDER BY id_pk ASC LIMIT MAdapt to scenarios: large data volumeFor example: Read tuples in rows 1000 to 1019 (pk is the primary key/unique key).SELECT * FROM your_table WHERE pk>=1000 ORDER BY pk ASC LIMIT 0,20
Method 6: Use subquery/join index to quickly locate the tuple, and then Read the tuple again.
For example (id is the primary key/unique key, variable in blue font)Example of using subquery:
SELECT * FROM your_table WHERE id <= (SELECT id FROM your_table ORDER BY id desc LIMIT ($page-1)*$pagesize ORDER BY id desc LIMIT $pagesize
Use connection example:
SELECT * FROM your_table AS t1 JOIN (SELECT id FROM your_table ORDER BY id desc LIMIT ($page-1)*$pagesize AS t2 WHERE t1.id <= t2.id ORDER BY t1.id desc LIMIT $pagesize;
Mysql uses limit paging for large amounts of data. As the page number increases, the query efficiency becomes lower.
Test experiment
1. Directly use limit start, count paging statements, which is also the method used in my program:
select * from product limit start, count
When the starting page is small, there is no performance problem with the query. Let's look at the execution time of paging starting from 10, 100, 1000, and 10000 (20 entries per page). is as follows:
select * from product limit 10, 20 0.016秒 select * from product limit 100, 20 0.016秒 select * from product limit 1000, 20 0.047秒 select * from product limit 10000, 20 0.094秒
We have seen that as the starting record increases, the time also increases. This shows that the paging statement limit is closely related to the starting page number. Then let’s change the starting record to 40w and take a look (that is, the average record)
select * from product limit 400000, 20 3.229秒
Let’s look at the time when we took the last page of records
select * from product limit 866613, 20 37.44秒
The largest page number for this kind of paging Page Apparently this kind of time is intolerable.
We can also conclude two things from this:
The query time of the limit statement is proportional to the position of the starting recordMysql’s limit statement is very Convenient, but it is not suitable for direct use for tables with many records.Use the covering index of the table to speed up paging queryWe As we all know, if the statement using an index query only contains that index column (covering index), then the query will be very fast.
Because there is an optimization algorithm for index search, and the data is on the query index, there is no need to find the relevant data address, which saves a lot of time. In addition, there is also a related index cache in Mysql. It is better to use the cache when the concurrency is high.
In our example, we know that the id field is the primary key, and naturally includes the default primary key index. Now let's see how the query using the covering index performs.
This time we query the data of the last page (using a covering index, including only the id column), as follows:
select id from product limit 866613, 20 0.2秒
Compared with the 37.44 seconds of querying all columns, it has improved by about More than 100 times faster
那么如果我们也要查询所有列,有两种方法,一种是id>=的形式,另一种就是利用join,看下实际情况:
SELECT * FROM product WHERE ID > =(select id from product limit 866613, 1) limit 20
查询时间为0.2秒!
另一种写法
SELECT * FROM product a JOIN (select id from product limit 866613, 20) b ON a.ID = b.id
查询时间也很短!
3. 复合索引优化方法
MySql 性能到底能有多高?MySql 这个数据库绝对是适合dba级的高手去玩的,一般做一点1万篇新闻的小型系统怎么写都可以,用xx框架可以实现快速开发。可是数据量到了10万,百万至千万,他的性能还能那么高吗?一点小小的失误,可能造成整个系统的改写,甚至更本系统无法正常运行!好了,不那么多废话了。
用事实说话,看例子:
数据表 collect ( id, title ,info ,vtype) 就这4个字段,其中 title 用定长,info 用text, id 是逐渐,vtype是tinyint,vtype是索引。这是一个基本的新闻系统的简单模型。现在往里面填充数据,填充10万篇新闻。最后collect 为 10万条记录,数据库表占用硬1.6G。
OK ,看下面这条sql语句:
select id,title from collect limit 1000,10;
很快;基本上0.01秒就OK,再看下面的
select id,title from collect limit 90000,10;
从9万条开始分页,结果?
8-9秒完成,my god 哪出问题了?其实要优化这条数据,网上找得到答案。看下面一条语句:
select id from collect order by id limit 90000,10;
很快,0.04秒就OK。为什么?因为用了id主键做索引当然快。网上的改法是:
select id,title from collect where id>=(select id from collect order by id limit 90000,1) limit 10;
这就是用了id做索引的结果。可是问题复杂那么一点点,就完了。看下面的语句
select id from collect where vtype=1 order by id limit 90000,10;
很慢,用了8-9秒!
到了这里我相信很多人会和我一样,有崩溃感觉!vtype 做了索引了啊?怎么会慢呢?vtype做了索引是不错,你直接
select id from collect where vtype=1 limit 1000,10;
是很快的,基本上0.05秒,可是提高90倍,从9万开始,那就是0.05*90=4.5秒的速度了。和测试结果8-9秒到了一个数量级。
从这里开始有人提出了分表的思路,这个和dis #cuz 论坛是一样的思路。思路如下:
建一个索引表:t (id,title,vtype) 并设置成定长,然后做分页,分页出结果再到 collect 里面去找info 。是否可行呢?实验下就知道了。
10万条记录到 t(id,title,vtype) 里,数据表大小20M左右。用
select id from collect where vtype=1 limit 1000,10;
很快了。基本上0.1-0.2秒可以跑完。为什么会这样呢?我猜想是因为collect 数据太多,所以分页要跑很长的路。limit 完全和数据表的大小有关的。其实这样做还是全表扫描,只是因为数据量小,只有10万才快。OK, 来个疯狂的实验,加到100万条,测试性能。加了10倍的数据,马上t表就到了200多M,而且是定长。还是刚才的查询语句,时间是0.1-0.2秒完成!分表性能没问题?
错!因为我们的limit还是9万,所以快。给个大的,90万开始
select id from t where vtype=1 order by id limit 900000,10;
看看结果,时间是1-2秒!why ?
分表了时间还是这么长,非常之郁闷!有人说定长会提高limit的性能,开始我也以为,因为一条记录的长度是固定的,mysql 应该可以算出90万的位置才对啊?可是我们高估了mysql 的智能,他不是商务数据库,事实证明定长和非定长对limit影响不大?怪不得有人说discuz到了100万条记录就会很慢,我相信这是真的,这个和数据库设计有关!
难道MySQL 无法突破100万的限制吗???到了100万的分页就真的到了极限?
答案是:NO 为什么突破不了100万是因为不会设计mysql造成的。下面介绍非分表法,来个疯狂的测试!一张表搞定100万记录,并且10G 数据库,如何快速分页!
好了,我们的测试又回到 collect表,开始测试结论是:
30万数据,用分表法可行,超过30万他的速度会慢道你无法忍受!当然如果用分表+我这种方法,那是绝对完美的。但是用了我这种方法后,不用分表也可以完美解决!
答案就是:复合索引!有一次设计mysql索引的时候,无意中发现索引名字可以任取,可以选择几个字段进来,这有什么用呢?
开始的
select id from collect order by id limit 90000,10;
这么快就是因为走了索引,可是如果加了where 就不走索引了。抱着试试看的想法加了 search(vtype,id) 这样的索引。
然后测试
select id from collect where vtype=1 limit 90000,10;
非常快!0.04秒完成!
再测试:
select id ,title from collect where vtype=1 limit 90000,10;
非常遗憾,8-9秒,没走search索引!
再测试:search(id,vtype),还是select id 这个语句,也非常遗憾,0.5秒。
综上:如果对于有where 条件,又想走索引用limit的,必须设计一个索引,将where 放第一位,limit用到的主键放第2位,而且只能select 主键!
完美解决了分页问题了。可以快速返回id就有希望优化limit , 按这样的逻辑,百万级的limit 应该在0.0x秒就可以分完。看来mysql 语句的优化和索引时非常重要的!
Recommendation: "mysql tutorial"
The above is the detailed content of MySQL paging query method for millions of data and its optimization suggestions. For more information, please follow other related articles on the PHP Chinese website!