MySQL limit usage and MySQL limit optimization

Source: Internet
Author: User
Tags mysql tutorial

MySQL tutorial limit usage and MySQL limit optimization
/*
MySQL limit usage November 08, 2007 14:291. SELECT * FROM tablename < conditional statement > limit 100,15

Start with 100 records after 15 (the actual fetch is 第101-115条 data)

2. Select * FROM tablename < conditional statement > limit 100,-1

Start after 100th-record of the last article

3. SELECT * FROM tablename < conditional statement > limit 15

Equivalent to limit 0,15. Query results take the first 15 data

To see the Limit optimization method, first save 1 million records below.
*/  
Set_time_limit (0);
mysql_connect (' localhost ', ' root ', ' root ');
mysql_select_db (' ACCC ');
mysql_query ("Set names ' gb2312 '");
Echo is importing data ... ';
for ($i =0 $i <=1000000; $i + +)
{
$sql = ' Insert Cn_loupan_news (Cityid,zid,tid,did,title,body,click, Litpic,sendname,header,recommend,hot,flash,setindex,opencomment,cityname,keywords,desciption,color,b,senddate, LID,NFROM,TITLE2,ZT) VALUES (' 11 ', ' 2 ', ' 1 ', ' 1 ', ' Test data ', ' test data ', ' the ', '/aa/gigf.gif ', ' aaaa ', ' 0 ', ' 0 ', ' 0 ', ' 0 ', ' 0 ', ' 0 ', ' 222 ', ' 222 ', ' Gsfdsgfds ', ' 1 ', ' 1 ', ' 124567124 ', ' 0 ', ' fdafda ', ' f ', ' Fdafa ');
mysql_query ($sql) or Die (Mysql_error ());
}
Echo ' successfully imported 1 million data ';
/*
Select ID
from ' cn_loupan_news '
ORDER BY id desc
Limit 800001, 20
Sort query time by primary keyword is displayed row 0-19 (20 total, query takes 0.2712 seconds)

Select *
from ' cn_loupan_news '
ORDER BY id desc
Limit 800001

Show Row 0-19 (20 total, query takes 4.3221 seconds)

From here to see how to select the ID Master index, the sorting speed is also ideal, if it is the * number again with the sort query time more than 4 seconds ah, not advisable method.


SELECT *
From Cn_loupan_news
Where ID >= (
Select ID
From Cn_loupan_news
Limit 800000, 1)
Limit 10

Show Row 0-9 (10 total, query takes 0.2456 seconds)

A little bit better.

Original articles reproduced in this site annotated from http://www.111cn.net/database/database.html

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.