mysqlÊý¾Ý¿âlimit²éѯÓÅ»¯
MYSQLµÄÓÅ»¯ÊǷdz£ÖØÒªµÄ¡£ÆäËû×î³£ÓÃÒ²×îÐèÒªÓÅ»¯µÄ¾ÍÊÇlimit¡£mysqlµÄlimit¸ø·ÖÒ³´øÀ´Á˼«´óµÄ·½±ã£¬µ«Êý¾ÝÁ¿Ò»´óµÄʱºò£¬limitµÄÐÔÄܾͼ±¾çϽµ¡£
ͬÑùÊÇÈ¡10ÌõÊý¾Ý
select * from yanxue8_visit limit 10000,10 ºÍ
select * from yanxue8_visit limit 0,10
¾Í²»ÊÇÒ»¸öÊýÁ¿¼¶±ðµÄ¡£
ÍøÉÏÒ²ºÜ¶à¹ØÓÚlimitµÄÎåÌõÓÅ»¯×¼Ôò£¬¶¼ÊÇ·Òë×ÔmysqlÊֲᣬËäÈ»ÕýÈ·µ«²»ÊµÓ᣽ñÌì·¢ÏÖһƪÎÄÕÂдÁËЩ¹ØÓÚlimitÓÅ»¯µÄ£¬ºÜ²»´í¡£
ÎÄÖв»ÊÇÖ±½ÓʹÓÃlimit£¬¶øÊÇÊ×ÏÈ»ñÈ¡µ½offsetµÄidÈ»ºóÖ±½ÓʹÓÃlimit sizeÀ´»ñÈ¡Êý¾Ý¡£¸ù¾ÝËûµÄÊý¾Ý£¬Ã÷ÏÔÒªºÃÓÚÖ±½ÓʹÓÃlimit¡£ÕâÀïÎÒ¾ßÌåʹÓÃÊý¾Ý·ÖÁ½ÖÖÇé¿ö½øÐвâÊÔ¡££¨²âÊÔ»·¾³win2033+p4Ë«ºË (3GHZ) +4GÄÚ´æ mysql 5.0.19£©
1¡¢offset±È½ÏСµÄʱºò¡£
select * from yanxue8_visit limit 10,10
¶à´ÎÔËÐУ¬Ê±¼ä±£³ÖÔÚ0.0004-0.0005Ö®¼ä
Select * from yanxue8_visit Where vid £¾=(
Select vid from yanxue8_visit Order By vid limit 10,1
) limit 10
¶à´ÎÔËÐУ¬Ê±¼ä±£³ÖÔÚ0.0005-0.0006Ö®¼ä£¬Ö÷ÒªÊÇ0.0006
½áÂÛ£ºÆ«ÒÆoffset½ÏСµÄʱºò£¬Ö±½ÓʹÓÃlimit½ÏÓÅ¡£Õâ¸öÏÔÈ»ÊÇ×Ó²éѯµÄÔÒò¡£
2¡¢offset´óµÄÊ ......
MYSQLºÍORACLEµÄһЩ²Ù×÷Çø±ð
ÓкܶàÓ¦ÓÃÏîÄ¿, ¸ÕÆð²½µÄʱºòÓÃMYSQLÊý¾Ý¿â»ù±¾ÉÏÄÜʵÏÖ¸÷ÖÖ¹¦ÄÜÐèÇó£¬Ëæ×ÅÓ¦ÓÃÓû§µÄÔö¶à£¬Êý¾ÝÁ¿µÄÔö¼Ó£¬MYSQL½¥½¥µØ³öÏÖ²»¿°ÖظºµÄÇé¿ö£ºÁ¬½ÓºÜÂýÉõÖÁå´»ú£¬ÓÚÊǾÍÓаÑÊý¾Ý´ÓMYSQLǨµ½ORACLEµÄÐèÇó£¬Ó¦ÓóÌÐòÒ²ÒªÏàÓ¦×öһЩÐ޸ġ£±¾ÈË×ܽá³öÒÔϼ¸µã×¢ÒâÊÂÏϣÍû¶Ô´ó¼ÒÓÐËù°ïÖú¡£
1.×Ô¶¯Ôö³¤µÄÊý¾ÝÀàÐÍ´¦Àí
MYSQLÓÐ×Ô¶¯Ôö³¤µÄÊý¾ÝÀàÐÍ£¬²åÈë¼Ç¼ʱ²»ÓòÙ×÷´Ë×ֶΣ¬»á×Ô¶¯»ñµÃÊý¾ÝÖµ¡£ORACLEûÓÐ×Ô¶¯Ôö³¤µÄÊý¾ÝÀàÐÍ£¬ÐèÒª½¨Á¢Ò»¸ö×Ô¶¯Ôö³¤µÄÐòÁкţ¬²åÈë¼Ç¼ʱҪ°ÑÐòÁкŵÄÏÂÒ»¸öÖµ¸³ÓÚ´Ë×ֶΡ£
CREATE SEQUENCE ÐòÁкŵÄÃû³Æ (×îºÃÊDZíÃû+ÐòÁкűê¼Ç) INCREMENT BY 1 START WITH 1 MAXVALUE 99999 CYCLE NOCACHE;
ÆäÖÐ×î´óµÄÖµ°´×ֶεij¤¶ÈÀ´¶¨, Èç¹û¶¨ÒåµÄ×Ô¶¯Ôö³¤µÄÐòÁкŠNUMBER(6) , ×î´óֵΪ999999
INSERT Óï¾ä²åÈëÕâ¸ö×Ö¶ÎֵΪ: ÐòÁкŵÄÃû³Æ.NEXTVAL
2. µ¥ÒýºÅµÄ´¦Àí
MYSQLÀï¿ÉÒÔÓÃË«ÒýºÅ°üÆð×Ö·û´®£¬ORACLEÀïÖ»¿ÉÒÔÓõ¥ÒýºÅ°üÆð×Ö·û´®¡£ÔÚ²åÈëºÍÐÞ¸Ä×Ö·û´®Ç°±ØÐë×öµ¥ÒýºÅµÄÌæ»»£º°ÑËùÓгöÏÖµÄÒ»¸öµ¥ÒýºÅÌæ»»³ÉÁ½¸öµ¥ÒýºÅ¡£
3. ·Ò³µÄSQLÓï¾äµÄ´¦Àí
MYSQL´¦Àí·Ò³µÄSQLÓï¾ä±È½Ï¼òµ¥£¬ÓÃLIMIT ¿ªÊ¼Î»ÖÃ, ¼Ç¼¸öÊý£»PHPÀï» ......
MYSQLºÍORACLEµÄһЩ²Ù×÷Çø±ð
ÓкܶàÓ¦ÓÃÏîÄ¿, ¸ÕÆð²½µÄʱºòÓÃMYSQLÊý¾Ý¿â»ù±¾ÉÏÄÜʵÏÖ¸÷ÖÖ¹¦ÄÜÐèÇó£¬Ëæ×ÅÓ¦ÓÃÓû§µÄÔö¶à£¬Êý¾ÝÁ¿µÄÔö¼Ó£¬MYSQL½¥½¥µØ³öÏÖ²»¿°ÖظºµÄÇé¿ö£ºÁ¬½ÓºÜÂýÉõÖÁå´»ú£¬ÓÚÊǾÍÓаÑÊý¾Ý´ÓMYSQLǨµ½ORACLEµÄÐèÇó£¬Ó¦ÓóÌÐòÒ²ÒªÏàÓ¦×öһЩÐ޸ġ£±¾ÈË×ܽá³öÒÔϼ¸µã×¢ÒâÊÂÏϣÍû¶Ô´ó¼ÒÓÐËù°ïÖú¡£
1.×Ô¶¯Ôö³¤µÄÊý¾ÝÀàÐÍ´¦Àí
MYSQLÓÐ×Ô¶¯Ôö³¤µÄÊý¾ÝÀàÐÍ£¬²åÈë¼Ç¼ʱ²»ÓòÙ×÷´Ë×ֶΣ¬»á×Ô¶¯»ñµÃÊý¾ÝÖµ¡£ORACLEûÓÐ×Ô¶¯Ôö³¤µÄÊý¾ÝÀàÐÍ£¬ÐèÒª½¨Á¢Ò»¸ö×Ô¶¯Ôö³¤µÄÐòÁкţ¬²åÈë¼Ç¼ʱҪ°ÑÐòÁкŵÄÏÂÒ»¸öÖµ¸³ÓÚ´Ë×ֶΡ£
CREATE SEQUENCE ÐòÁкŵÄÃû³Æ (×îºÃÊDZíÃû+ÐòÁкűê¼Ç) INCREMENT BY 1 START WITH 1 MAXVALUE 99999 CYCLE NOCACHE;
ÆäÖÐ×î´óµÄÖµ°´×ֶεij¤¶ÈÀ´¶¨, Èç¹û¶¨ÒåµÄ×Ô¶¯Ôö³¤µÄÐòÁкŠNUMBER(6) , ×î´óֵΪ999999
INSERT Óï¾ä²åÈëÕâ¸ö×Ö¶ÎֵΪ: ÐòÁкŵÄÃû³Æ.NEXTVAL
2. µ¥ÒýºÅµÄ´¦Àí
MYSQLÀï¿ÉÒÔÓÃË«ÒýºÅ°üÆð×Ö·û´®£¬ORACLEÀïÖ»¿ÉÒÔÓõ¥ÒýºÅ°üÆð×Ö·û´®¡£ÔÚ²åÈëºÍÐÞ¸Ä×Ö·û´®Ç°±ØÐë×öµ¥ÒýºÅµÄÌæ»»£º°ÑËùÓгöÏÖµÄÒ»¸öµ¥ÒýºÅÌæ»»³ÉÁ½¸öµ¥ÒýºÅ¡£
3. ·Ò³µÄSQLÓï¾äµÄ´¦Àí
MYSQL´¦Àí·Ò³µÄSQLÓï¾ä±È½Ï¼òµ¥£¬ÓÃLIMIT ¿ªÊ¼Î»ÖÃ, ¼Ç¼¸öÊý£»PHPÀï» ......
MySQLÊý¾Ý¿âÈÕÖ¾
selectÈÕÖ¾
slow selectÈÕÖ¾
±ä¸üÈÕÖ¾
¶þ½øÖƱä¸üÈÕÖ¾(binlog)
¸æ¾¯ÈÕÖ¾
´íÎóÈÕÖ¾¡£
¿ÉÒÔÔÚmy.cnfÖÐÅäÖÃ
²ÎÊý
˵Ã÷
log
Îı¾selectÈÕÖ¾£¬¼ÇÏÂËùÓеÄMySQLµÄÃüÁî²Ù×÷£¬
log-update
Îı¾±ä¸üÈÕÖ¾
log-bin
Õâ¸ö¶¼ÖªµÀÁË£¬Êý¾Ý¿â¸´ÖƵÄʱºò±Ø±¸
binlog_cache_size
ÁÙʱ´æ·Åij´ÎÊÂÎñµÄSQLÓï¾ä»º³å³¤¶È
max_binlog_cache_szie
×î´ó¶þ½øÖÆCacheÈÕÖ¾»º³åÇø³ß´ç
max_binlog_size
×î´óµÄ¶þ½øÖÆÈÕÖ¾³ß´ç
log-error
ÑÏÖØ´íÎóÈÕÖ¾
log-warnings
¾¯¸æ¼¶±ðµÄÈÕÖ¾£¬µÍÓÚerror
long_query_time
³¬¹ý¶àÉÙÃë¾ÍÊÇÂý²éѯÓï¾ä
log-queries-not-using-indexes
ûÓÐʹÓÃË÷Òý²éѯµÄÈÕÖ¾,
log-slow-queri
Âý²éѯ£¬Õâ¸öÒ»¶à¾ÍÒªÓÅ»¯ÁË ......
ÓÅ»¯MySQLÊý¾Ý¿â²éѯ
ÔÚÓÅ»¯²éѯÖУ¬Êý¾Ý¿âÓ¦ÓÃ(ÈçMySQL)¼´Òâζ×ŶԹ¤¾ßµÄ²Ù×÷ÓëʹÓá£Ê¹ÓÃË÷Òý¡¢Ê¹ÓÃEXPLAIN·ÖÎö²éѯÒÔ¼°µ÷ÕûMySQLµÄÄÚ²¿ÅäÖÿɴﵽÓÅ»¯²éѯµÄÄ¿µÄ
¡¡¡¡ÈκÎһλÊý¾Ý¿â³ÌÐòÔ±¶¼»áÓÐÕâÑùµÄÌå»á£º¸ßͨÐÅÁ¿µÄÊý¾Ý¿âÇý¶¯³ÌÐòÖУ¬Ò»ÌõÔã¸âµÄSQL²éѯÓï¾ä¿É¶ÔÕû¸öÓ¦ÓóÌÐòµÄÔËÐвúÉúÑÏÖصÄÓ°Ï죬Æä²»½öÏûºÄµô¸ü¶àµÄÊý¾Ý¿âʱ¼ä£¬ÇÒËü½«¶ÔÆäËûÓ¦ÓÃ×é¼þ²úÉúÓ°Ïì¡£
¡¡¡¡ÈçͬÆäËüѧ¿Æ£¬ÓÅ»¯²éѯÐÔÄܴܺó³Ì¶ÈÉϾö¶¨ÓÚ¿ª·¢ÕßµÄÖ±¾õ¡£ÐÒÔ˵ÄÊÇ£¬ÏñMySQLÕâÑùµÄÊý¾Ý¿â×Ô´øÓÐһЩÐÖú¹¤¾ß¡£±¾ÎļòÒªÌÖÂÛÖî¶à¹¤¾ßÖ®ÈýÖÖ£ºÊ¹ÓÃË÷Òý£¬Ê¹ÓÃEXPLAIN·ÖÎö²éѯÒÔ¼°µ÷ÕûMySQLµÄÄÚ²¿ÅäÖá£
¡¡¡¡1: ʹÓÃË÷Òý
¡¡¡¡MySQLÔÊÐí¶ÔÊý¾Ý¿â±í½øÐÐË÷Òý£¬ÒÔ´ËÄÜѸËÙ²éÕҼǼ£¬¶øÎÞÐèÒ»¿ªÊ¼¾ÍɨÃèÕû¸ö±í£¬ÓÉ´ËÏÔÖøµØ¼Ó¿ì²éѯËٶȡ£Ã¿¸ö±í×î¶à¿ÉÒÔ×öµ½16¸öË÷Òý£¬´ËÍâMySQL»¹Ö§³Ö¶àÁÐË÷Òý¼°È«ÎļìË÷¡£
¡¡¡¡¸ø±íÌí¼ÓÒ»¸öË÷Òý·Ç³£¼òµ¥£¬Ö»Ðèµ÷ÓÃÒ»¸öCREATE INDEXÃüÁΪË÷ÒýÖ¸¶¨ËüµÄÓò¼´¿É¡£ÁбíA¸ø³öÁËÒ»¸öÀý×Ó£º
¡¡¡¡Áбí A
¡¡¡¡mysql£¾ CREATE INDEX idx_username ON users(username);
¡¡¡¡Query OK, 1 row affected (0.15 sec)
¡¡¡¡Records: 1 Duplicates: 0 Warnin ......
MySQLÊý¾Ý¿ârootȨÏÞ¶ªÊ§½â¾ö·½°¸
Ò»Ì첻СÐÄ°ÑROOTµÄȨÏ޸ĵ½×îСÁË(Ö»ÄܵǼ,ʲô¶¼×ö²»ÁË),Õâ¿É¼±ËÀÎÒÁË.ÖØ×°µÄ»°Ì«Âé·³£¬¶øÇÒÀïÃæÓкܶàµÄÓû§£¬Ò»¸ö¸öÖØÐÂŪ²»ÖªµÀµ½Ê²Ã´Ê±ºò¡£
ºóÀ´ÎÒÏëÁËÒ»¸ö°ì·¨£¬ÏȰѵ±Ç°·þÎñÆ÷µÄMySQL·þÎñÍ£Ö¹£¬°ÑMySQL DATaĿ¼ÏµÄmysqlĿ¼¸ÄÃûΪmysql_OLD,µ½ÁíÒ»¸ö·þÎñÆ÷Ï°ÑmysqlĿ¼ÏµÄ/data/mysqlĿ¼¸´ÖƵ½¶ÔӦĿ¼£¬È»ºóÆô¶¯MySQL·þÎñ£¬Ê¹ÓÃÁíÒ»·þÎñÆ÷ϵÄroot µÄÕʺŵǼµ½PHPmyadmin£¬½øÈ¥Ö®ºó¿ÉÒÔ¿´µ½ÓÐÈ«²¿µÄ²Ù×÷ȨÏÞÁË£¬µ«Õâ¸öÊý¾Ý±íÖÐûÓе±Ç°·þÎñÆ÷ÖеÄÊý¾Ý¿âÏîÄ¿ºÍÓû§ÐÅÏ¢¡£ÕâʱÔÙÕÒµ½mysql_OLDÊý¾Ý¿â£¬½øÈ¥Ö®ºóµ½user±í£¬ÕÒµ½µÚÒ»¸ö root localhost±í¶Î£¬ÐÞ¸Ä:°ÑËùÓеÄN¸ÄΪY¡£È»ºóÔÙÍ£Ö¹mysql·þÎñ£¬°Ñи´ÖƹýÀ´µÄmysqlɾ³ý£¬°Ñmysql_old¸üÃûΪmysql£¬ÔÙÆô¶¯·þÎñÆ÷ºóÓÃrootµÇ¼£¬·¢ÏÖȨÏÞÒѾȫ²¿»ØÀ´ÁË¡£ ......
ǰ׺Ë÷Òý
(1)Èç¹ûÒÔ¶à¸ö×Ö¶Î×÷ΪË÷Òý£¨a,b£¬c)×÷Ϊһ¸ökey,ÔÚ²éѯʱ·Ö¸ù¾ÝÆ¥ÅäÇé¿öÊÇ·ñʹÓÃË÷Òý£¬ËùÒÔ£¬a/ a,b/ a,b,cÕâÑùµÄ²éѯ¶¼ÄÜʹÓõ½£¬µ«ÊÇb,a/a,c/c,aµÈ²»·û֪ǰ׺ƥÅä¾Í²»ÄÜʹÓã¬ËùÒÔÔÚдsqlʱÐèҪעÒâ
(2)¶ÔÓںܳ¤µÄ×Ö·û´®ÐèÒªÉú³Ékeyʱ£¬¿ÉÒÔÕÒ³ö×ʹÓõÄ×Ö·û´®µÄ×î³£¼ûǰ׺(Ç°N¸ö×ÖÌõ×÷Ϊkey,¶ø²»Êǽ«È«¸ö×Ö·û´®×÷Ϊkey)£¬ÕâÑùÒ»·½ÃæÎþÉüÉÙÉٵľ«È·ÐÔ£¨ÓжàÓàÊý¾Ý£¬ÔÙ¹ýÂË£©£¬ÁíÒ»·½Ãæ´ó´óÌá¸ßÁ˼ìË÷ЧÂÊ£¬Í¬Ê±¼õÉÙÁËË÷ÒýµÄά»¤¿ªÏú£¨´æ´¢¿Õ¼ä£¬¸üеȣ©
ÈçÓÐ200wµÄurl,ͳ¼Æ½á¹ûÈçÏ£º
select count(*) as cnt, url from iUrl group by url order by cnt desc limit 10;
+-----+-------------------+
| cnt | url |
+-----+-------------------+
| 380 | http://www.f.com, |
| 354 | http://www.r.com, |
| 340 | http://www.h.com, |
| 336 | http://www.u.com, |
| 336 | http://www.e.com, |
| 336 | http://www.i.com, |
| 332 | http://www.k.com, |
| 330 | http://www.t.com, |
| 330 | http://www.w.com, |
| 326 | http://www ......
×ܼǼÊý:2220; ×ÜÒ³Êý:370; ÿҳ6 Ìõ;
Ê×Ò³ ÉÏÒ»Ò³ [3] [4] [5] [6] 7
[8] [9] [10] [11] [12] ÏÂÒ»Ò³ βҳ