Mysql limit large data volume paging Optimization Method

Source: Internet
Author: User
Tags mysql manual mysql tutorial mysql index

Mysql limit is an important method for optimizing the paging of large data volumes. The other most commonly used and most needed optimization is limit. Mysql limit greatly facilitates paging. However, when the data volume is large, the performance of limit decreases sharply. Similarly, selecting * from yanxue8_visit limit, 10 and select * from yanxue8_visit limit is not an order of magnitude. There are also many five limit optimization guidelines on the Internet, which are translated from the Mysql manual. They are correct but not practical. Today I found an article about limit optimization, which is quite good. Instead of using limit directly, we first get the offset id and then use limit size to get data. Based on his data, it is much better to use limit directly. Here I use data in two cases for testing. (Test environment win2033 + p4 dual-core (3 GHZ) + 4G memory Mysql 5.0.19) 1. When the offset is small. Select * from yanxue8_visit limit for 10 or 10 times, with the duration between 0.0004 and 0.0005 http://www.zhutiai.com Select * From yanxue8_visit Where vid> = (Select vid From yanxue8_visit Order By vid limit 10, 1) limit is run for more than 10 times, and the time is between 0.0005 and 0.0006. Conclusion: when the offset is small, it is better to use limit directly. This is obviously the cause of subquery. 2. When the offset value is large. Select * from Batch limit failed, run for more than 10 times, and keep the time around 0.0187 Select * From yanxue8_visit Where vid >=( Select vid From yanxue8_visit Order By vid limit, 1) limit more than 10 times, the time is kept at around 0.0061, only 1/3 of the former. It can be predicted that the larger the offset, the higher the latter. In the future, pay attention to correct your limit statement and optimize the four fields in the Mysql Data Table collect (id, title, info, vtype). The title is fixed with a fixed length, info uses text, id is gradual, vtype is tinyint, vtype is index. This is a simple model of a basic news system. Now fill in the data and 0.1 million news articles. Finally, collect contains 0.1 million records, and the database tutorial table occupies 1.6 GB of hard disk space. OK. Let's take a look at the following SQL statement: select id, title from collect limit 0.01, 10; very soon; basically, seconds is OK. Then let's look at the following select id, title from collect limit, 10; paging starts from 90 thousand. What is the result? 8-9 seconds. What's wrong with my god ???? In fact, we need to optimize this data and find the answer online. See the following statement: select id from collect order by id limit 0.04, 10; soon, seconds is OK. Why? Because it is faster to use the id Primary Key for indexing. The solution on the internet is: select id, title from collect where id >=( select id from collect order by id limit 90000,1) limit 10; this is the result of indexing with id. But the problem is a little complicated, and it's all done. See the following statement select id from collect where vtype = 1 order by id limit, 10; very slow, it takes 8 to 9 seconds! At this point, I believe many people will feel the same as me! What if the vtype has been indexed? How can it be slow? Vtype indexes are good. You directly select id from collect where vtype = 1 limit 0.05; it is very fast, basically 90 thousand seconds, but it is increased by 90 times, starting from, that is the speed of 0.05*90 = 4.5 seconds. And the test result is 8-9 seconds to an order of magnitude. Some people have put forward the idea of table sharding from here. This is the same idea as the discuz forum. The idea is as follows: Create an index table: t (id, title, vtype) and set it to a fixed length. Then, perform paging and retrieve info from collect. Is it feasible? The experiment is complete. 0.1 million records are recorded in t (id, title, vtype), and the data table size is about 20 mb. Use select id from t where vtype = 1 order by id limit 90000,10; soon. It can basically be completed in 0.1-0.2 seconds. Why? I guess it is because there is too much collect data, so paging takes a long time. Limit is completely related to the size of the data table. In fact, this is still a full table scan, only because the data volume is small and only 0.1 million is fast. OK. Here is a crazy experiment, which adds 1 million entries to test the performance. After adding 10 times of data, the t table will reach more than 200 MB and the length is fixed. Or the query statement just now. The time is 0.1-0.2 seconds! Is table sharding performance okay? Error! Because our limit is still 90 thousand, so fast. For a large one, 0.9 million starts to select id from t where vtype = 1 order by id limit 900000,10; check the result. The time is 1-2 seconds! Why ?? The table sharding time is still so long and depressing! Some people say that the fixed length will improve the performance of limit. At first I thought that because the length of a record is fixed, the mysql tutorial should be able to calculate the position of 0.9 million? However, we overestimated the intelligence of mysql. It is not a business database. It turns out that fixed length and non-fixed length have little impact on limit? No wonder some people say that discuz will be slow when it reaches 1 million records. I believe this is true. This is related to database design! Can't MySQL exceed the 1 million limit ??? When the page reaches 1 million, the limit is reached ??? The answer is: NO !!!! Why can't I break through 1 million because I won't design mysql. Next we will introduce the non-Table sharding Method for a crazy test! How to quickly split a table with 1 million records and 10 Gb databases! Now, our test is back to the collect table. The test conclusion is: 0.3 million data, which can be used as a table sharding method. The speed of over 0.3 million data will slow down and you cannot bear it! Of course, if you use the sub-table + me method, it is absolutely perfect. However, after using this method, it can be perfectly solved without table sharding! The answer is: Composite Index! When I designed a mysql index, I accidentally found that the index name can be any one, and several fields can be selected. What is the purpose? Select id from collect order by id limit, 10; so fast is because the index is taken, but if the where clause is added, no index will be taken. I tried to add an index like search (vtype, id. Then test select id from collect where vtype = 1 limit 90000,10; very fast! Finished in 0.04 seconds! Test again: select id, title from collect where vtype = 1 limit 90000,10; very sorry, 8-9 seconds, did not go to the search index! Test again: search (id, vtype) or select id statement, also very sorry, 0.5 seconds. To sum up, if you want to reference limit with the where condition, you must design an index and place where first. The primary key used by limit is placed at 2nd bits, and only the select primary key can be used! Solved the paging problem perfectly. If you can quickly return the id, there is hope to optimize the limit. According to this logic, millions of limit can be completed in seconds. It seems that mysql statement optimization and indexing are very important! Now, back to the original question, how can we quickly apply the above research to development? If a composite query is used, my Lightweight Framework will be useless. You have to write the paging string by yourself. How much trouble is that? Let's look at another example. The idea is coming out: select * from collect where id in (, 12, 50,); you can check it in 0 seconds! Mygod and mysql indexes are equally valid for in statements! It seems that it is wrong to say that in cannot use indexes on the Internet! With this conclusion, we can easily apply it to a lightweight framework: the code is as follows: $ db = dblink (); $ db-> pagesize = 20; $ SQL = "select id from collect where vtype = $ vtype"; $ db-> execute ($ SQL); $ strpage = $ db-> strpage (); // Save the paging string in a temporary variable to facilitate the output of while ($ rs = $ db-> fetch_array () {$ strid. = $ rs ['id']. ',';} $ strid = substr ($ strid, 0, strlen ($ strid)-1); // construct the id string $ db-> pagesize = 0; // It is critical that the page is cleared without canceling the class. In this way, you only need to connect to the database once and do not need to open it again. $ db-> execute ("select id, title, url, sTime, gTime, Vtype, tag from collect where id in ($ strid) "); <? Php tutorial while ($ rs = $ db-> fetch_array ():?> <Tr> <td> <? Php echo $ rs ['id'];?> </Td> <? Php echo $ rs ['url'];?> </Td> <? Php echo $ rs ['stime'];?> </Td> <? Php echo $ rs ['gtime'];?> </Td> <? Php echo $ rs ['vtype'];?> </Td> <a href = "? Act = show & id = <? Php echo $ rs ['id'];?> "Target =" _ blank "> <? Php echo $ rs ['title'];?> </A> </td> <? Php echo $ rs ['tag'];?> </Td> </tr> <? Php endwhile;?> </Table> <? Php echo $ strpage;?> Through simple transformation, the idea is actually very simple: 1) through the optimization of the index, find the id, and spell it into a string like "12000. 2) 2nd queries to find the results. With a small index and a few changes, mysql can support efficient paging with millions or even tens of millions of pages! Through the example here, I have reflected on one point: for large systems, PHP cannot use frameworks, especially those that cannot even be seen by SQL statements! At first, I almost collapsed my Lightweight Framework! It is only suitable for rapid development of small applications. For ERP, OA, and large websites, the data layer, including the logic layer, cannot use frameworks. If programmers lose control over SQL statements, the risk of the project will increase exponentially! Especially when using mysql, mysql must be a professional dba to achieve its best performance. A single index may result in a performance difference of thousands of times! Performance Optimization: Based on the high performance of limit in MySQL5.0, I have a new understanding of data paging. 1. select * From cyclopedia Where ID> = (Select Max (ID) From (Select ID From cyclopedia Order By ID limit 90001) As tmp) limit 100; 2. select * From cyclopedia Where ID> = (Select Max (ID) From (Select ID From cyclopedia Order By ID limit 90000,1) As tmp) limit 100; is it faster to get 90000 records after 100, 1st sentences or 2nd sentences? The first 1st records are obtained first, and the largest ID value is used as the start identifier, then, it can be used to quickly locate the next 100 records. The first sentence is only the last one after the first 2nd records. Then, the ID value is used as the start identifier to locate the next 90000 records and the execution results of the 100 sentences. 100 rows in set (0.23) sec 2nd statement execution result. 100 rows in set (0.19) sec obviously won 2nd sentences. it seems that limit does not seem as much as I previously imagined to do a full table scan and return limit offset + length records, so it seems that limit is much better than the Top performance of the MS-SQL. in fact, the first sentence can be simplified to Select * From cyclopedia Where ID> = (Select ID From cyclopedia limit 90000,1) limit 2nd; the ID of the first 100 records is directly used without the Max operation, theoretically, this is more efficient. But in actual use, there is almost no effect, because the positioning ID returns a record, and Max can get the result without any operation, but this is clearer, saving the trouble of snake painting. however, since MySQL has a limit that can directly control the location where records are retrieved, why not simply use Select * From cyclopedia limit, 1? Isn't it more concise? I thought it would be wrong. After I tried it, I knew that the result was: 1 row in set (8.88) sec. How is it so scary, it reminds me of the "high score" I had in 4.1 yesterday ". select * it is best not to use it casually. Based on the principle of "what to use" and "what to choose", the more Select fields, the larger the field data volume, the slower the speed. which of the above two paging methods is much better than the single-write method? Although it seems that the number of queries is more, it is actually at a lower cost in exchange for efficient performance, it is very worthwhile. 1st schemes can also be used for MS-SQL, and may be the best. because the primary key ID is always the fastest way to locate the start segment. select Top 100 * From cyclopedia Where ID> = (Select Top 90001 Max (ID) From (Select ID From cyclopedia Order By ID) As tmp) but whether the implementation method is stored in the process or the direct code, the bottleneck is always that the TOP of the MS-SQL is always to return the first N records, this situation in the amount of data is not deep, but if hundreds of thousands, efficiency will definitely be low. in contrast, MySQL limit has many advantages. Execute: Select ID From cyclopedia limit 90000 Select ID From cyclopedia limit 90000,1 and the result is: 90000 rows in set (0.36) sec 1 row in set (0.06) sec and MS-SQL can only use Select Top 90000 ID From cyclopedia for execution time is 390 ms, the same operation time is less than MySQL 360 ms.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.