MySQL limit optimization, fast paging of millions to tens of millions of pages,-reference of composite indexes and Application in lightweight...

Source: Internet
Author: User
Tags mysql index

How high is MySQL performance? I have been using PHP for more than half a year. I think this problem from the day before yesterday. Have been suffering, have been desperate, and are full of confidence now! MySQL is definitely suitable for DBA-level Masters. Generally, you can write 11 thousand pieces of news in a small system, and you can achieve rapid development using the XX framework. However, if the data volume reaches 0.1 million or tens of millions, can the performance be so high? A small mistake may cause the whole system to be rewritten, or even the system cannot run normally! Okay, not that much nonsense.

Let's talk about the facts and look at the example:
The data table collect (ID, title, info, vtype) has these four fields, where title is fixed length, info is text, ID is gradual, vtype is tinyint, and 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 table occupies 1.6 GB of hard disk space. OK. Check the following SQL statement:
Select ID, title from collect limit 0.01, 10; very fast; basically seconds on OK, then look at the following

Select ID, title from collect limit 90 thousand, 10; pagination starts from. 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. Let's look at the following statement:

Select ID from collect order by ID limit 0.04, 10; soon, seconds OK. Why? Because it is faster to use the id Primary Key for indexing. The online modification method 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 90000,10; very slow, 8-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 is good for indexing. You can directly
Select ID from collect where vtype = 1 limit 0.05, 10; is very fast, basically 90 thousand seconds, but 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 pagination, and retrieve the results by page in 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. Big, starting from 0.9 million
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, MySQL should be able to calculate the 0.9 million position? 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, you can complete the solution 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.


Coming:
Select * from collect where ID in (9000,12, 50,7000); 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 output
    • 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 to clear the page 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 while ($ rs = $ db-> fetch_array ():?>
    • <Tr>
    • <TD> & nbsp; <? PHP echo $ Rs ['id'];?> </TD>
    • <TD> & nbsp; <? PHP echo $ Rs ['url'];?> </TD>
    • <TD> & nbsp; <? PHP echo $ Rs ['stime'];?> </TD>
    • <TD> & nbsp; <? PHP echo $ Rs ['gtime'];?> </TD>
    • <TD> & nbsp; <? PHP echo $ Rs ['vtype'];?> </TD>
    • <TD> & nbsp; <a href = "? Act = show & id = <? PHP echo $ Rs ['id'];?>" Target = "_ blank"> <? PHP echo $ Rs ['title'];?> </A> </TD>
    • <TD> & nbsp; <? PHP echo $ Rs ['tag'];?> </TD>
    • </Tr>
    • <? PHP endwhile;?>
    • </Table>
    • <? PHP
    • Echo $ strpage;

Copy code



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 the rapid development of small applications. For ERP, OA, large websites, data layer, and logic layer, nothing can use the framework. 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!

PS: After actual tests, 1 million of the data, 1.6 million of the data, 15 GB tables, and m indexes are obtained. Even if the index is used, the limit takes 0.49 seconds. Therefore, it is best not to let others see 0.1 million rows of post-record data, or else it will be slow! Use indexes. After such optimization, MySQL reaches the limit of millions of pages! However, this is already a good result. If you use sqlserver, you will be stuck! While 1.6 million of the data uses ID in (STR) very quickly, basically 0 seconds. In this case, MySQL should easily handle tens of millions of data records.

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.