阿里規定超過3張表,禁止join,為何?


阿里規定超過3張表,禁止join,為何?

來源:http://blog.itpub.net/30393770/viewspace-2650450/

一、 問題提出

《阿里巴巴JAVA開發手冊》裡面寫超過三張表禁止join,這是為什麼?

阿里規定超過3張表,禁止join,為何?

二、問題分析

對這個結論,你是否有懷疑呢?也不知道是哪位先哲說的不要人云亦云,今天我設計sql,來驗證這個結論。(實驗沒有從代碼角度分析,目前達不到。可以把mysql當一個黑盒,使用角度來驗證這個結論) 驗證結論的時候,會有很多發現,各位往後看。

三、 實驗環境

vmware10+centos7.4+mysql5.7.22

  • centos7內存4.5G,4核,50G硬盤。
  • mysql配置為2G,特別說明硬盤是SSD。

四、我概述下我的實驗

有4張表,student學生表,teacher老師表,course課程表,sc中間關係表,記錄了學生選修課程以及分數。具體sql腳本,看文章結尾,我附上。中間我自己寫了造數據的腳本,也在結尾。

阿里規定超過3張表,禁止join,為何?

實驗是為解決一個問題的:查詢選修“tname553”老師所授課程的學生中,成績最高的學生姓名及其成績。

查詢sql是:

<code>selectStudent.Sname,course.cname,score
fromStudent,SC,Course,Teacher
whereStudent.s_id=SC.s_idandSC.c_id=Course.c_idandsc.t_id=teacher.t_id
andTeacher.Tname='tname553'
andSC.score=(selectmax(score)fromSCwheresc.t_id=teacher.t_Id);
/<code>

我來分析一下這個語句:4張表等值join,還有一個子查詢。算是比較簡單的sql語句了(相比ERP動就10張表的哦,已經很簡單了)。我 還會分解這個語句成3個簡單的sql:

<code>selectmax(score)fromSC,Teacherwheresc.t_id=teacher.t_IdandTeacher.Tname='tname553';
selectsc.t_id,sc.s_id,scorefromSC,Teacher
wheresc.t_id=teacher.t_Id
andscore=590
andTeacher.Tname='tname553';
selectStudent.Sname,course.cname,score
fromStudent,SC,course
whereStudent.s_id=SC.s_idandsc.s_idin(20769800,48525000,26280200)andcourse.c_id=sc.c_id;
/<code>

我來分析下:第一句,就是查詢最高分,得到最高分590分。第二句就是查詢出最高分的學生id,得到

<code>20769800,48525000,26280200
/<code>

第三句就是查詢出學生名字和分數。這樣這3個語句的就可以查詢出來 成績最高的學生姓名及其成績 。

接下來我會分別造數據:1千萬選課記錄(一個學生選修2門課),造500萬學生,100萬老師(一個老師帶5個學生,挺高端的吧),1000門課,。用上面查詢語句查詢。其中sc表我測試了下有索引和沒有索引情況,具體見下表。

再接下來,我會造1億選課記錄(一個學生選修2門課),5000萬學生,1000萬老師,1000門課。然後分別執行上述語句。最後我會在oracle數據庫上執行上述語句。

五、下面兩張表是測試結果

阿里規定超過3張表,禁止join,為何?

阿里規定超過3張表,禁止join,為何?

六、仔細看上表,可以發現:

1.步驟3.1沒有在連接鍵上加索引,查詢很慢,說明:“多表關聯查詢時,保證被關聯的字段需要有索引”;

2.步驟6.1,6.2,6.3,換成簡單sql,在數據量1億以上, 查詢時間還能勉強接受。此時說明mysql查詢有些吃力了,但是仍然嫩查詢出來。

3.步驟5.1,mysql查詢不出來,4表連接,對我本機mysql來說,1.5億數據超過極限了(我調優過這個SQL,執行計劃和索引都走了,沒有問題,show profile顯示在sending data.這個問題另外文章詳談。)

4.對比1.1 和5.1 步驟sql查詢,4表連接,對我本機mysql來說 ,1.5千萬數據查詢很流利,是一個mysql數據量流利分水嶺。(這個只是現象,不太準確,需要同時計算表的容量)。

5.步驟5.1對比6.1,6.2,6.3,多表join對mysql來說,處理有些吃力。

6.超過三張表禁止join,這個規則是針對mysql來說的。後續會看到我用同樣機器,同樣數據量,同樣內存,可以完美計算 1.5億數據量join。針對這樣一個規則,對開發來說 ,需要把一些邏輯放到應用層去查詢。

總結: 這個規則 超過三張表禁止join ,由於數據量太大的時候,mysql根本查詢不出來,導致阿里出了這樣一個規定。(其實如果表數據量少,10張表也不成問題,你自己可以試試)而我們公司支付系統朝著大規模高併發目標設計的,所以,遵循這個規定。

在業務層面來講,寫簡單sql,把更多邏輯放到應用層,我的需求我會更瞭解,在應用層實現特定的join也容易得多。

七、讓我們來看看oracle數據庫的優秀表現:

阿里規定超過3張表,禁止join,為何?

看步驟7.1,就是沒有索引,join表很多的情況下,oracle仍然26秒查詢出結果來。所以我會說mysql的join很弱。那麼問題來了,為什麼現在使用很多人使用mysql呢?這是另外一個問題,我會另外說下我的思考。

看完本篇文章,另外我還附加贈送,所謂摟草打兔子。就是快速造數據。你可以自己先寫腳本造數據,看看我是怎麼造數據的,就知道我的技巧了。

八、附上部分截圖

阿里規定超過3張表,禁止join,為何?

阿里規定超過3張表,禁止join,為何?

阿里規定超過3張表,禁止join,為何?

阿里規定超過3張表,禁止join,為何?

九、附上sql語句和造數據腳本

<code>usestu;
droptableifexistsstudent;
createtablestudent
(s_idint(11)notnullauto_increment,
snoint(11),
snamevarchar(50),
sageint(11),
ssexvarchar(8),
father_idint(11),
mather_idint(11),
notevarchar(500),
primarykey(s_id),
uniquekeyuk_sno(sno)
)engine=innodbdefaultcharset=utf8mb4;
truncatetablestudent;
delimiter$$
dropfunctionifexistsinsert_student_data$$
createfunctioninsert_student_data()
returnsintdeterministic
begin
declareiint;
seti=1;
whilei<50000000do
insertintostudentvalues(i,i,concat('name',i),i,casewhenfloor(rand()*10)%2=0then'f'else'm'end,floor(rand()*100000),floor(rand()*1000000),concat('note',i));
seti=i+1;
endwhile;
return1;
end$$
delimiter;
selectinsert_student_data();
selectcount(*)fromstudent;
usestu;
createtablecourse
(
c_idint(11)notnullauto_increment,
cnamevarchar(50)
notevarchar(500),primarykey(c_id)
)engine=innodbdefaultcharset=utf8mb4;
truncatetablecourse;
delimiter$$
dropfunctionifexistsinsert_course_data$$
createfunctioninsert_course_data()
returnsintdeterministic
begin
declareiint;

seti=1;
whilei<=1000do
insertintocoursevalues(i,concat('course',i),floor(rand()*1000),concat('note',i));
seti=i+1;
endwhile;
return1;
end$$
delimiter;
selectinsert_course_data();
selectcount(*)fromcourse;
usestu;
droptableifexistssc;
createtablesc
(
s_idint(11),
c_idint(11),
t_idint(11),
scoreint(11)
)engine=innodbdefaultcharset=utf8mb4;
truncatetablesc;
delimiter$$
dropfunctionifexistsinsert_sc_data$$
createfunctioninsert_sc_data()
returnsintdeterministic
begin
declareiint;
seti=1;
whilei<=50000000do
insertintoscvalues(i,floor(rand()*1000),floor(rand()*10000000),floor(rand()*750));
seti=i+1;
endwhile;
return1;
end$$
delimiter;
selectinsert_sc_data();
commit;
selectinsert_sc_data();
commit;
createindexidx_s_idonsc(s_id);
createindexidx_t_idonsc(t_id);
createindexidx_c_idonsc(c_id);
selectcount(*)fromsc;
usestu;
droptableifexiststeacher;
createtableteacher
(
t_idint(11)notnullauto_increment,
tnamevarchar(50),
notevarchar(500),primarykey(t_id)
)engine=innodbdefaultcharset=utf8mb4;


truncatetableteacher;
delimiter$$
dropfunctionifexistsinsert_teacher_data$$
createfunctioninsert_teacher_data()
returnsintdeterministic
begin
declareiint;
seti=1;
whilei<=10000000do
insertintoteachervalues(i,concat('tname',i),concat('note',i));
seti=i+1;
endwhile;
return1;
end$$
delimiter;
selectinsert_teacher_data();
commit;
selectcount(*)fromteacher;
/<code>

這個是oracle的測試和造數據腳本

<code>createtablespacescott_datadatafile'/home/oracle/oracle_space/sitpay1/scott_data.dbf'size1024mautoextendon;
createtablespacescott_indexdatafile'/home/oracle/oracle_space/sitpay1/scott_index.dbf'size64mautoextendon;
createtemporarytablespacescott_temptempfile'/home/oracle/oracle_space/sitpay1/scott_temp.dbf'size64mautoextendon;
dropuserscottcascade;
createuserscottidentifiedbytigerdefaulttablespacescott_datatemporarytablespacescott_temp;
grantresource,connect,dbatoscott;
droptablestudent;
createtablestudent
(s_idnumber(11),
snonumber(11),
snamevarchar2(50),
sagenumber(11),
ssexvarchar2(8),
father_idnumber(11),
mather_idnumber(11),
notevarchar2(500)
)nologging;
truncatetablestudent;
createorreplaceprocedureinsert_student_data
is
qnumber(11);
begin
q:=0;
foriin1..50loop
insert/*+append*/intostudentselectrownum+qass_id,rownum+qassno,concat('sutdent',rownum+q)assname,floor(dbms_random.value(1,100))assage,'f'asssex,rownum+qasfather_id,rownum+qasmather_id,concat('note',rownum+q)asnotefromdualconnectbylevel<=1000000;
q:=q+1000000;

commit;
endloop;
endinsert_student_data;
/
callinsert_student_data();
altertablestudentaddconstraintpk_studentprimarykey(s_id);
commit;
selectcount(*)fromstudent;
createtablecourse
(
c_idnumber(11)primarykey,
cnamevarchar2(50),
notevarchar2(500)
);
truncatetablecourse;
createorreplaceprocedureinsert_course_data
is
qnumber(11);
begin

foriin1..1000loop
insert/*+append*/intocoursevalues(i,concat('name',i),concat('note',i));
endloop;
endinsert_course_data;
/
callinsert_course_data();
commit;
selectcount(*)fromcourse;
createtablesc
(
s_idnumber(11),
c_idnumber(11),
t_idnumber(11),
scorenumber(11)
)nologging;
truncatetablesc;
createorreplaceprocedureinsert_sc_data
is
qnumber(11);
begin
q:=0;
foriin1..50loop
insert/*+append*/intoscselectrownum+qass_id,floor(dbms_random.value(0,1000))asc_id,floor(dbms_random.value(0,10000000))t_id,floor(dbms_random.value(0,750))asscorefromdualconnectbylevel<=1000000;
q:=q+1000000;
commit;
endloop;
endinsert_sc_data;
/
callinsert_sc_data();
createindexidx_s_idonsc(s_id);

createindexidx_t_idonsc(t_id);
createindexidx_c_idonsc(c_id);
selectcount(*)fromsc;
createtableteacher
(
t_idnumber(11),
tnamevarchar2(50),
notevarchar2(500)
)nologging;
truncatetableteacher;
createorreplaceprocedureinsert_teacher_data
is
qnumber(11);
begin
q:=0;
foriin1..10loop
insert/*+append*/intoteacherselectrownum+qast_id,concat('teacher',rownum+q)astname,concat('note',rownum+q)asnotefromdualconnectbylevel<=1000000;
q:=q+1000000;
commit;
endloop;
endinsert_teacher_data;
/
callinsert_teacher_data();
altertableteacheraddconstraintpk_teacherprimarykey(t_id);
selectcount(*)fromteacher;/<code>




小編經過多年積累整理出來了很多Java電子書,有很多電子書我覺質量還是非常高的,由於電子書太多我也是用業餘時間挑著看的,這麼多資源自己保存著也是浪費,就想著現在把資源分享出來,希望能真正幫到大家;

資源我都整理在網盤了,之前分享出來的鏈接沒過幾天就自動取消,需要資料私信我發送【電子書】,都是免費領取的,不然沒幾天我就要重新更新一次鏈接,也沒有那麼多時間;

阿里規定超過3張表,禁止join,為何?

阿里規定超過3張表,禁止join,為何?

阿里規定超過3張表,禁止join,為何?


分享到:


相關文章: