優(yōu)化了一個(gè)mysql的語句,沒有什么技術(shù)含量,說一下過程:
成都創(chuàng)新互聯(lián)公司是一家業(yè)務(wù)范圍包括IDC托管業(yè)務(wù),雅安服務(wù)器托管、主機(jī)租用、主機(jī)托管,四川、重慶、廣東電信服務(wù)器租用,溫江服務(wù)器托管,成都網(wǎng)通服務(wù)器托管,成都服務(wù)器租用,業(yè)務(wù)范圍遍及中國大陸、港澳臺以及歐美等多個(gè)國家及地區(qū)的互聯(lián)網(wǎng)數(shù)據(jù)服務(wù)公司。優(yōu)化前語句如下,大約執(zhí)行時(shí)間為1-2秒
select u.id ,u.s_user_name,u.s_user_img,(select count(a.n_copy_count) from t_fonts a join t_practicerecords b on b.n_font_id=a.id where a.n_user_id=u.id and b.n_delete=0 group by a.n_user_id)as n_copy_count,case when (select count(id) from t_friendships where n_user_id=11311 and n_friend_id=u.id)>0 then 'true' else 'false' end as is_collected from t_users u where BINARY `s_user_name` = '做字小助手' order by LENGTH(s_user_name) asc ,id asc limit 0,10相關(guān)的執(zhí)行計(jì)劃如下:
mysql> EXPLAIN select u.id ,u.s_user_name,u.s_user_img,(select count(a.n_copy_count) from t_fonts a join t_practicerecords b on b.n_font_id=a.id where a.n_user_id=u.id and b.n_delete=0 group by a.n_user_id)as n_copy_count,case when (select count(id) from t_friendships where n_user_id=11311 and n_friend_id=u.id)>0 then 'true' else 'false' end as is_collected from t_users u where BINARY `s_user_name` = '做字小助手' order by LENGTH(s_user_name) asc ,id asc limit 0,10; +----+--------------------+---------------+------------+--------+-------------------------------------------------------------+---------------------------------+---------+------------------+---------+----------+-----------------------------+ | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | +----+--------------------+---------------+------------+--------+-------------------------------------------------------------+---------------------------------+---------+------------------+---------+----------+-----------------------------+ | 1 | PRIMARY | u | NULL | ALL | NULL | NULL | NULL | NULL | 1360606 | 100.00 | Using where; Using filesort | | 3 | DEPENDENT SUBQUERY | t_friendships | NULL | eq_ref | PRIMARY,PK__T_friend__3213E83F571DF1D5,user_index,n_user_id | PRIMARY | 8 | const,sykdb.u.id | 1 | 100.00 | NULL | | 2 | DEPENDENT SUBQUERY | a | NULL | ref | PRIMARY,n_user_index | n_user_index | 4 | sykdb.u.id | 7 | 100.00 | Using index condition | | 2 | DEPENDENT SUBQUERY | b | NULL | ref | idx_fid_score_delete_uploadtime | idx_fid_score_delete_uploadtime | 5 | sykdb.a.id,const | 5 | 100.00 | Using index | +----+--------------------+---------------+------------+--------+-------------------------------------------------------------+---------------------------------+---------+------------------+---------+----------+-----------------------------+ 4 rows in set, 4 warnings (0.00 sec)對t_users表的s_user_name字段創(chuàng)建索引,并修改表結(jié)構(gòu)
mysql> create index idx_user_name on t_users (s_user_name); Query OK, 0 rows affected (5.55 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> alter table t_users modify s_user_name varchar(100) binary; Query OK, 1474778 rows affected (45.23 sec) Records: 1474778 Duplicates: 0 Warnings: 0修改語句把binary去掉,再次查詢?nèi)缦拢?/p>mysql> select u.id ,u.s_user_name,u.s_user_img,(select count(a.n_copy_count) from t_fonts a join t_practicerecords b on b.n_font_id=a.id where a.n_user_id=u.id and b.n_delete=0 group by a.n_user_id)as n_copy_count,case when (select count(id) from t_friendships where n_user_id=11311 and n_friend_id=u.id)>0 then 'true' else 'false' end as is_collected from t_users u where `s_user_name` = '做字小助手' order by LENGTH(s_user_name) asc ,id asc limit 0,10; +----+-----------------+--------------------------------+--------------+--------------+ | id | s_user_name | s_user_img | n_copy_count | is_collected | +----+-----------------+--------------------------------+--------------+--------------+ | 2 | 做字小助手 | /0/1/photo/20160729035328.jpeg | NULL | false | +----+-----------------+--------------------------------+--------------+--------------+ 1 row in set (0.00 sec)
不過修改表結(jié)構(gòu)需謹(jǐn)慎。
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)cdcxhl.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。