優(yōu)化了一個(gè)MySQL的語(yǔ)句,沒(méi)有什么技術(shù)含量,說(shuō)一下過(guò)程:

創(chuàng)新互聯(lián)公司主要從事做網(wǎng)站、成都網(wǎng)站制作、網(wǎng)頁(yè)設(shè)計(jì)、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務(wù)。立足成都服務(wù)資中,十余年網(wǎng)站建設(shè)經(jīng)驗(yàn),價(jià)格優(yōu)惠、服務(wù)專(zhuān)業(yè),歡迎來(lái)電咨詢(xún)建站服務(wù):18980820575
優(yōu)化前語(yǔ)句如下,大約執(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)
對(duì)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
修改語(yǔ)句把binary去掉,再次查詢(xún)?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)
不過(guò)修改表結(jié)構(gòu)需謹(jǐn)慎。
當(dāng)前名稱(chēng):優(yōu)化一個(gè)mysql語(yǔ)句
瀏覽地址:http://chinadenli.net/article40/ppcpho.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供外貿(mào)建站、品牌網(wǎng)站設(shè)計(jì)、網(wǎng)站排名、網(wǎng)站制作、品牌網(wǎng)站制作、移動(dòng)網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)