MySQL allows storage results to be paged for complex queries. (Welcome to explore)

Source: Internet
Author: User
Tags array mysql query
mysql| paging MySQL lets you store results paging for complex queries.

It seems that few people are talking about paging, are we all addicted to limit m,n?
In the case of indexing, limit m,n speed is sufficient, but when searching for complex conditions,
where somthing ORDER by Somefield+somefield
MySQL searches the database for "all" qualifying records, and then takes out the M,n record.
If you have hundreds of thousands of data, and the user searches for some very popular words,
Then read the last few pages in turn wonderment ... MySQL should be very solemn and stirring operation of the hard drive.

So, you can try to let MySQL also store pagination, of course, to program with.
(Here is just an idea, welcome to discuss it together)

ASP's paging: In the ASP system has a Recordset object to achieve paging, but a large number of data in memory, and do not know when the failure (please ASP expert advice).
SQL database paging: Using stored procedures + cursor way paging, the implementation of the principle is not very clear, imagine if you use a query to get the desired results, or the ID set, need to follow the page as long as the results of the IDs read the relevant records. This allows for a small space to keep all IDs of this query. (Query results in SQL do not know how to clear expired garbage?)

This allows MySQL to simulate a storage paging mechanism:
1. Select id from $table where $condition order by $field limit $max _pages* $count;
Query for IDs that match the criteria.
Limit the number of records that meet the maximum criteria, or you can not add them.
2. Because all variables are lost after the execution of PHP, you can consider:
Programme A. Creates a temporary table in MySQL, and the query results are inserted with a single time or random number as the unique flag.
It establishes Page1~pagen fields, each of which saves the IDs required in the page, such that an ID is on a record.
Programme B. If you open the session, you can also save it in the session, which is actually stored in the file.
Creates a $ids array, $IDs [1]~ $IDs [$max _pages]. Considering that sometimes the user will open a few
Windows query, to make a unique flag for the $ids, to avoid the query results cover each other. Two-dimensional array
and $ $var are good ways.
3. In each page of the request, directly find the corresponding IDs, in the middle with "," Interval:
SELECT * from $table where ID in ($ids); The speed is absolutely fast
4. The closure to consider the results of the automatic clarity of the query, you can set the timing or the proportion of random clear. If you use a MySQL temporary table to add a Time Flag field, the session to join the "$ids["]=time (); Do not manipulate after a certain period of time as expired data.

5. If you want to optimize, you can consider using the statements in 1 and 2.a to merge into a select ....

Note:
1. The above is only for the MySQL repair solution, I hope that MySQL will be able to add these functions in
2. Other databases can also be applied.
3. If there are more advanced paging methods in other databases, please tell me or mailto:steeven@kali.com.cn
4. If there is really a lot of data to query, or MySQL goodbye, Sql,oracle has provided more advanced keyword indexing query.

Excellence, the above is just a discussion, welcome to discuss the paging problem together. (can also be about other databases)
I hope that one day we can sort out all kinds of pagination for beginners to refer to.

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.