程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 數據庫知識 >> MYSQL數據庫 >> MySQL綜合教程 >> mysql鎖訂單個表的辦法

mysql鎖訂單個表的辦法

編輯:MySQL綜合教程

mysql鎖訂單個表的辦法。本站提示廣大學習愛好者:(mysql鎖訂單個表的辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是mysql鎖訂單個表的辦法正文


奶奶的,為啥如今五一節只放3天,5月的氣象最合適出游了,不外俺們這些苦逼的IT男是沒法享用了。
一離開公司,項目司理就找到開辟leader,說我們網站 頁面很慢,讓他排查緣由。
一據說 網站慢,頁面慢哥就來精力了,哥的成本行就是 處理“慢”的成績。
開辟leader 很愁悶的說,我們曾經加了 memcache了,20分鐘 cache一次,咋個照樣慢呢,
因而哥就問,誰人網頁跑了哪些SQL? 能抓出來讓我看看嗎? 開辟Leader 武斷的把SQL 抓了出來。
經由排查,我們發明了一個SQL確切跑得慢。該SQL 以下

select *
from (select u.NAME UniversityName,
u.id UniversityId,
count(a.SIGNUPNUMBER) playercnt
from T_B_UNIVERSITY u
left join T_D_EDUCATION e
on e.UNIVERSITY_ID = u.id
left join T_D_VIDEO_PLAYER a
on a.USER_ID = e.user_id
and e.ISDEFAULT = 1
and e.ISVALID = 1
and a.AUDITSTATUS = 1
and a.ISVALID = 1
left join T_D_USER c
on a.USER_ID = c.id
and c.ISVALID = 1
where u.REGION_CODE like '43%'
group by u.NAME, u.id)
order by playercnt desc;

履行籌劃以下

履行籌劃
----------------------------------------------------------
Plan hash value: 3938743742
--------------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 142 | 10366 | 170 (3)| 00:00:03 |
| 1 | SORT ORDER BY | | 142 | 10366 | 170 (3)| 00:00:03 |
| 2 | HASH GROUP BY | | 142 | 10366 | 170 (3)| 00:00:03 |
|* 3 | HASH JOIN RIGHT OUTER| | 672 | 49056 | 168 (2)| 00:00:03 |
|* 4 | TABLE ACCESS FULL | T_D_USER | 690 | 5520 | 5 (0)| 00:00:01 |
| 5 | NESTED LOOPS OUTER | | 672 | 43680 | 162 (1)| 00:00:02 |
|* 6 | HASH JOIN OUTER | | 672 | 37632 | 14 (8)| 00:00:01 |
|* 7 | TABLE ACCESS FULL | T_B_UNIVERSITY | 50 | 2050 | 8 (0)| 00:00:01 |
| 8 | TABLE ACCESS FULL | T_D_EDUCATION | 672 | 10080 | 5 (0)| 00:00:01 |
| 9 | VIEW | | 1 | 9 | 0 (0)| 00:00:01 |
|* 10 | FILTER | | | | | |
|* 11 | TABLE ACCESS FULL| T_D_VIDEO_PLAYER | 1 | 15 | 3 (0)| 00:00:01 |
--------------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
3 - access("A"."USER_ID"="C"."ID"(+))
4 - filter("C"."ISVALID"(+)=1)
6 - access("E"."UNIVERSITY_ID"(+)="U"."ID")
7 - filter("U"."REGION_CODE" LIKE '43%')
10 - filter("E"."ISVALID"=1 AND "E"."ISDEFAULT"=1)
11 - filter("A"."USER_ID"="E"."USER_ID" AND "A"."AUDITSTATUS"=1 AND
"A"."ISVALID"=1)

年夜家能發明這個SQL 的成績嗎? 這個 SQL 之所以跑得慢是由於開辟人員把SQL的前提寫錯地位了
准確的寫法應當是 上面如許的

select *
from (select u.NAME UniversityName,
u.id UniversityId,
count(a.SIGNUPNUMBER) playercnt
from T_B_UNIVERSITY u
left join T_D_EDUCATION e
on e.UNIVERSITY_ID = u.id
and e.ISDEFAULT = 1
and e.ISVALID = 1
left join T_D_VIDEO_PLAYER a
on a.USER_ID = e.user_id
and a.AUDITSTATUS = 1
and a.ISVALID = 1
left join T_D_USER c
on a.USER_ID = c.id
and c.ISVALID = 1
where u.REGION_CODE like '43%'
group by u.NAME, u.id)
order by playercnt desc;

履行籌劃以下

履行籌劃
----------------------------------------------------------
Plan hash value: 2738827747
---------------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
---------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 142 | 11218 | 25 (16)| 00:00:01 |
| 1 | SORT ORDER BY | | 142 | 11218 | 25 (16)| 00:00:01 |
| 2 | HASH GROUP BY | | 142 | 11218 | 25 (16)| 00:00:01 |
|* 3 | HASH JOIN RIGHT OUTER | | 301 | 23779 | 23 (9)| 00:00:01 |
|* 4 | TABLE ACCESS FULL | T_D_USER | 690 | 5520 | 5 (0)| 00:00:01 |
|* 5 | HASH JOIN RIGHT OUTER| | 301 | 21371 | 17 (6)| 00:00:01 |
|* 6 | TABLE ACCESS FULL | T_D_VIDEO_PLAYER | 78 | 1170 | 3 (0)| 00:00:01 |
|* 7 | HASH JOIN OUTER | | 301 | 16856 | 14 (8)| 00:00:01 |
|* 8 | TABLE ACCESS FULL | T_B_UNIVERSITY | 50 | 2050 | 8 (0)| 00:00:01 |
|* 9 | TABLE ACCESS FULL | T_D_EDUCATION | 301 | 4515 | 5 (0)| 00:00:01 |
---------------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
3 - access("A"."USER_ID"="C"."ID"(+))
4 - filter("C"."ISVALID"(+)=1)
5 - access("A"."USER_ID"(+)="E"."USER_ID")
6 - filter("A"."AUDITSTATUS"(+)=1 AND "A"."ISVALID"(+)=1)
7 - access("E"."UNIVERSITY_ID"(+)="U"."ID")
8 - filter("U"."REGION_CODE" LIKE '43%')
9 - filter("E"."ISDEFAULT"(+)=1 AND "E"."ISVALID"(+)=1)

之前SQL要跑至多5秒以上,如今0.1秒能出成果。
列位童鞋,SQL 有外銜接的時刻,要留意過濾前提的地位,記住啦!!!
有SQL 須要優化的 迎接參加 QQ 群 220761024 請求注明 來自CSDN
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved