How to improve the performance of MySQLLimit queries

Source: Internet
Author: User
How can I improve the performance of MySQLLimit queries? We have made great efforts in mysqllimit. Of course there are other types such as data tables and database server configurations. However, as a program, we only need to optimize the performance of mysql Query statements.

How can I improve the performance of MySQL Limit queries? We have worked hard on mysql limit. of course, there are other types such as configuring data tables and database servers, however, as a program, we only need to optimize the performance of mysql Query statements.

A table with tens of millions of records on MySQL 5.0.x needs to read about 100,000 records. Common methods:


1

The code is as follows:
Select * from mytable where index_col = xxx limit offset, limit;

Experience: if there is no blob/text field, the single row record is relatively small, you can set limit to a large point, it will speed up.

Problem: The first tens of thousands of reads are fast, but the speed decreases linearly. at the same time, the mysql server cpu is 99%, which is unacceptable.

Call

The code is as follows:
Explain select * from mytable where index_col = xxx limit offset, limit;

Show type = ALL

Description of "All" written in MySQL optimization

A full table scan is done for each combination of rows from the previous tables. this is normally not good if the table is the first table not marked const, and usually very bad in all other cases. normally, you can avoid ALL by adding indexes that allow row retrieval from the table based on constant values or column values from earlier tables.

It seems that mysql uses a relatively stupid method for all, so it uses the range method instead? Because the id is incremental, the SQL statement is also well modified.

The code is as follows:

Select * from mytable where id> offset and id <offset + limit and index_col = xxx

The explicit type is set to range, and the result speed is ideal. the returned result is dozens of times faster.

Limit syntax:


1

The code is as follows:
SELECT * FROM table LIMIT [offset,] rows | rows OFFSET

The LIMIT clause can be used to force the SELECT statement to return the specified number of records. LIMIT accepts one or two numeric parameters. The parameter must be an integer constant.

If two parameters are specified, the first parameter specifies the offset of the first returned Record Row, and the second parameter specifies the maximum number of returned record rows. The offset of the initial Record Row is 0 rather than 1 ).

To be compatible with PostgreSQL, MySQL also supports syntax: LIMIT # OFFSET #.

The code is as follows:
Mysql> SELECT * FROM table LIMIT 5, 10; // retrieves records FROM 6 to 15 rows.

// To retrieve all record rows from an offset to the end of the record set, you can specify the second parameter-1

The code is as follows:

Mysql> SELECT * FROM table LIMIT 95,-1; // retrieves 96-last Records.


// If only one parameter is specified, it indicates the maximum number of record rows returned. In other words, LIMIT n is equivalent to LIMIT 0, n

The code is as follows:
Mysql> SELECT * FROM table LIMIT 5;

// Retrieve the first five record rows
MySQL limit greatly facilitates paging. However, when the data volume is large, the performance of limit decreases sharply. 10 data records are also obtained. the following two statements are not an order of magnitude.

The code is as follows:

Select * from table limit, 10

Select * from table limit 0, 10

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.

When offset is small:

The code is as follows:

Select * from table limit 10, 10

// Run multiple times, with the duration between 0.0004 and 0.0005

Select * From table Where vid> = (Select vid From table Order By vid limit 10, 1) limit 10

// Run multiple times, with the duration between 0.0005 and 0.0006, mainly 0.0006
Conclusion: When the offset is small, it is better to use limit directly. This is obviously the cause of subquery.

When offset is large:

Select * from table limit, 10

// Run multiple times, with a time of around 0.0187

Select * From table Where vid> = (Select vid From table Order By vid limit, 1) limit 10

// Run multiple times, with a time of around 0.0061, only 1/3 of the former. The larger the offset value, the better the latter.


Next, let's take a look at the mysql data paging method based on limit.


Let's create a test ipdatas table:

The code is as follows:
Create table 'ipdatas '(
'Id' INT (11) not null AUTO_INCREMENT,
'Uid' INT (8) not null default '0 ',
'Ipaddress' VARCHAR (50) not null,
'Source' VARCHAR (255) default null,
'Track' VARCHAR (255) default null,
'Enables' VARCHAR (255) default null,
'Createdtime' datetime not null default '2017-00-00 00:00:00 ',
'Createddate' date not null default '2017-00-00 ',
Primary key ('id '),
KEY 'uid' ('uid ')
) ENGINE = MYISAM AUTO_INCREMENT = 67086110 default charset = utf8;

This is the promotion ip data record table of our advertising alliance. because I am not a DBA of mysql, we are only testing
Because there are about 7015291 data records

Here, we use jdbc batch to insert 60 million pieces of data to this table, "million pieces of data inserted in JDBC: 9999297ms ";
It took about two hours. here I used a batch of about in size and submitted each time. Another point was that the data submitted each time was very small, and here I used the myisam data table, because I need to know the size of the mysql database and the size of the index data.
Ipdatas. MYD 3.99 GB (4,288,979,008 bytes)
Ipdatas. MYI 1.28 GB (1,377,600,512 bytes)
What I want to talk about here is that if big data needs to be indexed at a time, it is better to change it to a numeric field. The index size and query speed are much better than the time field.

Enter the subject:
1. Full table search
The returned structure is 67015297 data records.

The code is as follows:
Select count (id) FROM ipdatas;
Select count (uid) FROM ipdatas;
Select count (*) FROM ipdatas;

First, the two full table data queries are very fast. the data dictionary contained in mysql should keep the maximum number of data entries in the database.
Query index conditions

The code is as follows:
Select count (*) FROM ipdatas WHERE uid = 1; return result Time: 2 minutes 31 seconds 594
Select count (id) FROM ipdatas WHERE uid = 1; return result Time: 1 minute 29 seconds 609
Select count (uid) FROM ipdatas WHERE uid = 1; return result Time: 2 minutes 41 seconds 813

The second query is faster. because mysql has a cache area, increasing the cache area size can solve a lot of query optimization. it can be said that the cache is everywhere. in program development, cache is also used layer by layer.
Query data

The code is as follows:

The first query starts.
SELECT * FROM ipdatas order by id desc limit 1, 10; 31 ms
SELECT * FROM ipdatas LIMIT 1, 10; 15 ms

10,000th start queries
SELECT * FROM ipdatas order by id asc limit, 10; 266 milliseconds
SELECT * FROM ipdatas LIMIT, 10; 16 ms

Query starts with 500th million entries
SELECT * FROM ipdatas LIMIT 11.312 seconds, 10; seconds
SELECT * FROM ipdatas order by id asc limit 221.985 seconds, 10; seconds
The two returned results are exactly the same, that is, the default mysql mechanism is the id forward order, but the time is quite different.

Query starts with 5,000th million entries
SELECT * FROM ipdatas LIMIT 60000000,10; 66.563 seconds (compared to the test below)
SELECT * FROM ipdatas order by id asc limit 1060.000, 10; seconds
SELECT * FROM ipdatas order by id desc limit 17015307,10; 434.937 seconds

The third and second results are the same, but they differ a lot in order. it seems that this is not as good as many commercial databases, such as oracle and sqlserver, it seems that mysql is the slower the start row and the slower the start row. in this case, you can skip sorting without sorting. The performance gap is huge, and the difference is more than 20 times.

Returns the ID list of the queried data.

The code is as follows:

The first query starts.
Select id from ipdatas order by id asc limit 1, 10; 31 ms
SELECT id FROM ipdatas LIMIT 1, 10; 0 ms

10,000th
SELECT id FROM ipdatas order by id asc limit, 10; 68 ms
Select id from ipdatas limit, 10; 0 ms

Query starts with 500th million entries
SELECT id FROM ipdatas LIMIT 1.750 seconds, 10; s
SELECT id FROM ipdatas order by id asc limit 14.328 seconds, 10; s

Start querying 6,000th million records
SELECT id FROM ipdatas LIMIT 116.406, 10; s
SELECT id FROM ipdatas order by id asc limit 60000000,10; 136.391 s

Select id from ipdatas limit 29.032; s
Select id from ipdatas limit 20000002,10; 24.594 s
Select id from ipdatas limit 24.812 limit; s
Select id from ipdatas limit 28.750 limit 84.719; s
Select id from ipdatas limit 30.797 limit 108.042; s
Select id from ipdatas limit 600100002, 10; 133.012 s 122.328 s

Select * from ipdatas limit 27.328; s
Select * from ipdatas limit 20000002,10; 15.188 s
Select * from ipdatas limit 45.218 limit; s
Select * from ipdatas limit 49.250 limit 50.531; s
Select * from ipdatas limit 73.297 limit 56.781; s
Select * from ipdatas limit 600100002, 10; 67.891 s 75.141 s

Select id from ipdatas order by id asc limit 29.438; s
Select id from ipdatas order by id asc limit 20000002,10; 24.719 s
Select id from ipdatas order by id asc limit 25.969 limit 2, 10; s
Select id from ipdatas order by id asc limit 40000002, 10; 29.860d
Select id from ipdatas order by id asc limit 32.844 limit 2, 10; s
Select id from ipdatas order by id asc limit 600100002, 10; 34.047 s

As for SELECT * ipdatas order by id asc, it takes about 10 minutes to test it.
It can be seen that the gap is not big when the SELECT id is not sorted, and the sorting gap is large.
Next let's take a look at this statement.

The code is as follows:
SELECT * FROM ipdatas WHERE id IN (records, 100000, 500000,000000, 5000000,0000000, 2000000,30000000, 40000000,50000000, 60000000,67015297 );
0.094 ms

It can be seen that the in query on the id can be ignored. after all, it is more than 60 million records. Therefore, why do many lucene or solr searches return IDs for the database to obtain data again, of course lucene/solr + mysql is a good solution, which is very suitable for front-end search technology. for example, the front-end paging search can achieve very good performance. it can also support good grouping search result sets, and then obtain the real data of the data records through the id to show the effect is really good, not to mention that the tens of millions is no problem, it is really recommended to vomit blood.

To sum up, the most important sentence is:

For more information about how to fix the problem, see.

The code is as follows:
SELECT SQL _no_cache * FROM table WHERE id> = (SELECTsql_no_cache id FROM table where conditon ORDER BY id DESC LIMIT 126380,1) limit 20;

You can analyze and optimize mysql Query statements based on your situation.

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.