Ò׽ؽØͼÈí¼þ¡¢µ¥Îļþ¡¢Ãâ°²×°¡¢´¿ÂÌÉ«¡¢½ö160KB

ÓÅ»¯MySQLÊý¾Ý¿â²éѯ

ÓÅ»¯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 Warnings: 0
¡¡¡¡ÕâÀ¶Ôusers±íµÄusernameÓò×öË÷Òý£¬ÒÔÈ·±£ÔÚWHERE»òÕßHAVING×Ó¾äÖÐÒýÓÃÕâÒ»ÓòµÄSELECT²éѯÓï¾äÔËÐÐËٶȱÈûÓÐÌí¼ÓË÷ÒýʱҪ¿ì¡£Í¨¹ýSHOW INDEXÃüÁî¿ÉÒԲ鿴Ë÷ÒýÒѱ»´´½¨(ÁбíB)¡£
¡¡¡¡Áбí B
¡¡¡¡mysql> SHOW INDEX from users;
¡¡¡¡| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment |
¡¡¡¡| users | 1 | idx_username | 1 | username | A | NULL | NULL | NULL | YES | BTREE | |
¡¡¡¡1 row in set (0.00 sec)
¡¡¡¡ÖµµÃ×¢ÒâµÄÊÇ£ºË÷Òý¾ÍÏñÒ»°ÑË«Èн£¡£¶Ô±íµÄÿһÓò×öË÷Òýͨ³£Ã»ÓбØÒª£¬ÇҺܿÉÄܵ¼ÖÂÔËÐÐËٶȼõÂý£¬ÒòΪÏò±íÖвåÈë»òÐÞ¸ÄÊý¾Ýʱ£¬MySQL²»µÃ²»Ã¿´Î¶¼ÎªÕâЩ¶îÍâµÄ¹¤×÷ÖØн¨Á¢Ë÷Òý¡£ÁíÒ»·½Ã棬±ÜÃâ¶Ô±íµÄÿһÓò×öË÷ÒýͬÑù²»ÊÇÒ»¸ö·Ç³£ºÃµÄÖ÷Ò⣬ÒòΪÔÚÌá¸ß²åÈë¼Ç¼µÄËÙ¶Èʱ£¬µ¼Ö²éѯ²Ù×÷µÄËٶȼõÂý¡£Õâ¾ÍÐèÒªÕÒµ½Ò»¸öƽºâµã£¬±ÈÈçÔÚÉè¼ÆË÷Òýϵͳʱ£¬¿¼ÂDZíµÄÖ÷Òª¹¦ÄÜ(Êý¾ÝÐÞ¸´¼°±à¼­)²»Ê§ÎªÒ»ÖÖÃ÷ÖǵÄÑ¡Ôñ¡£
¡¡¡¡2: ÓÅ»¯²éѯÐÔÄÜ
¡¡¡¡ÔÚ·ÖÎö²éѯÐÔÄÜʱ£¬¿¼ÂÇEXPLAIN¹Ø¼ü×ÖͬÑùºÜ¹ÜÓá£EXPLAIN¹Ø¼ü×ÖÒ»°ã·ÅÔÚSELECT²éѯÓï¾äµÄÇ°Ã棬ÓÃÓÚ


Ïà¹ØÎĵµ£º

LinuxÏÂmysql°²×°ÅäÖÃÈëÃÅ֪ʶ

1¡¢ÏÂÔØMySQLµÄLinux°²×°Îļþ
¡¡¡¡LinuxÏ°²×°MySQLÐèÒªÏÂÃæÁ½¸öÎļþ£º
 ¡¡¡¡MySQL-server-5.1.7-0.i386.rpm
¡¡¡¡ÏÂÔصØַΪ£ºhttp://dev.mysql.com/downloads/mysql/5.1.html£¬´ò¿ª´ËÍøÒ³£¬ÏÂÀ­ÍøÒ³ÕÒµ½“Linux x86 generic RPM (statically linked against glibc 2.2.5) downloads”ÏÕÒµ½“Ser ......

mysqlÖÐunionºÍunion allµÄÇø±ðºÍ×¢Òâµã


×òÌìÓиö¹¦ÄÜҪʵÏÖÅÅÐÐЧ¹û£¬¶øÇÒÊÇËæ»ú°´ÕÕijЩ×ֶεÄ×ÜÖµ£¬µ«ÊǵÚÒ»¸öÓÖÒª²»Ò»Ñù£¬ÒòΪµÚÒ»¸öÈ˸øÁËÇ®£¬ËùÒÔÒªÅŵÚÒ»¡£
´ò¸ö±È·½°É£¬±í(userinfo)ÖÐÓÐÕ⼸¸ö×ֶΣºusername,isvalid,givedmoney,sumip,dayip,monthip,visitcount,regdate
ÏÖÔÚҪʵÏÖÏÔʾÅÅÐÐÇ°10λÓû§£¬
     ³öÏÖÔÚÅÅÐеÚһλµ ......

mysqlµ÷Óúó

ÓÃmysql_query£¬mysql_fetch_array()ºóÓ¦¸ÃÒª
free_result().function count_admin($where = '')
{
if($where) $where = " WHERE $where";
$result = $this->db->get_one("SELECT count(*) as num from $this->table_admin_role $where");
return $result['num'];
} ......

Ò»¸ö¸´ÔÓµÄmysql²éѯÓï¾ä(case,when,then,left join )

select `a`.`id` AS `id`,`a`.`UserName` AS `UserName`,
(case when (`a`.`sRegDate` = _utf8'1990-01-01 00:00:00.0') then _utf8'' else cast(date_format(`a`.`sRegDate`,_utf8'%Y-%m-%d %H:%i:%S') as char charset utf8) end) AS `sRegDate`,(case when (`a`.`feeendtime` = _utf8'1990-01-01 00:00:00.0') then _ut ......
© 2009 ej38.com All Rights Reserved. ¹ØÓÚE½¡ÍøÁªÏµÎÒÃÇ | Õ¾µãµØͼ | ¸ÓICP±¸09004571ºÅ