Mysqllimit is optimized to reference and apply compound indexes of millions to tens of millions of pages quickly _ MySQL

Source: Internet
Author: User
Mysqllimit is optimized. the reference of a compound index with millions to tens of millions of pages is applied to the lightweight framework bitsCN. the database comMySql is definitely suitable for DBAs. 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. the title uses a fixed length, and the info uses text and id.
Gradually, 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;
It took 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 performs an index. you can directly select id from
Collect where vtype = 1 limit 1000,10;
It is very fast, basically 0.05 seconds, but it is increased by 90 times. from 90 thousand, it 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 as discuz.
Forums share the same idea. 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
It is completely related to the data table size. 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 limit performance. At first I thought that because the length of a record is fixed, mysql
It should be possible to calculate the location of 0.9 million, right? 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 someone says
When discuz reaches 1 million records, it will be very slow. 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! One table handles 1 million records and 10 GB
Database, how to quickly paging!


Now, our test goes back to the collect table and the test conclusion is:
0.3 million data. it is feasible to use the table sharding method. if it exceeds 0.3 million, it 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, the index will not be taken. I added the idea of trying it out.
Search (vtype, id) indexes. 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;
Sorry, I did not go through the search index in 8-9 seconds!

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
Put first, the primary key used by limit is put 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 (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 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 )");
Fetch_array ():?>






"
Target = "_ blank">
$ Rs ['tag'];?>

?>

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 with mysql
Mysql must be a professional dba to achieve its optimal 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 of table data, and M indexes are obtained. even if the index is used, the limit takes 0.49 seconds. So it is best not to let others see 0.1 million data records after the page, 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! 1.6 million of data is used
Id in (str) is very fast, basically 0 seconds. In this case, mysql should easily handle tens of millions of data records.

BitsCN.com

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.