遠程支援完成大幅SQL TUNING,sqltuning

來源:互聯網
上載者:User

遠程支援完成大幅SQL TUNING,sqltuning

   前幾天,一個朋友找到我,說一個SQL效能有問題,看看能不能最佳化,下面為過程:

雪豹 9:35:10

在嗎

蘭花島主 15:07:39

忙忘了,有事兒?

雪豹 15:07:49

雪豹 15:07:54

資料庫最佳化問題

蘭花島主 15:08:04

哦,你說。

雪豹 15:09:09

 select distincta.suite_no,b.bd_nm,b.crt_date from (select suite_no  from all_suite where cus_id =1)a left join

    (select  b.suite_no,b.bd_nm,b.crt_date from building  b,customer c where c.cus_no=b.cus_no

    and c.cus_serial='75806001113513'and b.back_date is null ) b on b.suite_no=a.suite_no;

雪豹 15:09:35



雪豹 15:09:58

如果資料在千萬層級時候 會很慢

雪豹 15:10:35

有沒有更好寫法那

蘭花島主 15:10:58

哪個是千萬級啊?

雪豹 15:11:10

building表

蘭花島主 15:11:25

customer呢?

雪豹 15:11:26

clustomer表很小

雪豹 15:11:36

在萬條資料

雪豹 15:12:00

All_suite 這個表也是幾萬條資料

蘭花島主 15:13:38

你這個building表上的索引呢?

蘭花島主 15:13:40

我看看?

雪豹 15:15:10

 building_idx1(bd_nm,area_id,cus_no,back_date,suite_no)

building_idx2(crt_date)

building_idx3(back_date)

building_pkey(sid)

雪豹 15:16:27

building的索引是否可以在最佳化一下

蘭花島主 15:16:36

這個列的選擇性怎麼樣?

蘭花島主 15:16:48

也就是重複值多不?

雪豹 15:17:12

crt_date 不多

蘭花島主 15:17:42

不是這個,cus_serial

雪豹 15:17:57

area_id, cus_id, suite_no 這個幾個重複值多

雪豹 15:18:27

這個沒有重複

雪豹 15:18:30

都是唯一的

雪豹 15:18:44

這個是customer的表裡嗎

雪豹 15:18:48

他不是索引

蘭花島主 15:19:17

最終結果多少?

雪豹 15:19:37

查詢出來資料嗎

蘭花島主 15:19:51

對,結果集。

雪豹 15:21:22

每一個cus_serial 查詢所有對應的all_suite所有房間 對應最後入住人員

雪豹 15:21:52

這個房間個數不會多

蘭花島主 15:22:29

一個cum_serial大概對應多少個customer?

蘭花島主 15:23:38

大概?

雪豹 15:23:57

一對一

蘭花島主 15:24:21

一個cus_serial對應一個customer?

蘭花島主 15:25:38

building_idx1這個索引列太多了。

雪豹 15:26:02

可以刪除

雪豹 15:26:37

保留幾個索引

蘭花島主 15:26:54

不用,單獨在cus_no上建個索引吧。

蘭花島主 15:27:03

現在多久出結果?

雪豹 15:28:01

30s左右

蘭花島主 15:28:12

嗯。

蘭花島主 15:28:39

按照我說的見個索引吧,估計不會超過1s

蘭花島主 15:28:44

建。

雪豹 15:29:10

ok

蘭花島主 15:29:16

這樣的話,你這個sql最佳化的空間還是比較大的。

蘭花島主 15:29:48

應該在最多幾百ms出結果。

蘭花島主 15:29:53

最佳化好了的話。

雪豹 15:32:57

是的

雪豹 15:33:04

0.5秒

雪豹 15:33:22

比以前快了

蘭花島主 15:34:11

你剛才這個0.5s,是按照我說的方法建索引後的嗎?

雪豹 15:34:25

建索引後

雪豹 15:34:37

是的

蘭花島主 15:34:51

哦。

蘭花島主 15:35:12

其他,不太瞭解你那邊資料的情況,不太好精細最佳化。

蘭花島主 15:35:44

應該還能快。

雪豹 15:36:34

好的

雪豹 15:36:36

我看看

蘭花島主 15:37:16

比如:另外兩個表的索引

雪豹 15:37:31

蘭花島主 15:37:43

那個全表掃描,雖然表小,但對 0.5s,應該也是不小的比例。

雪豹 15:37:55

明白了

蘭花島主 15:38:04

對吧。

雪豹 15:41:30

最佳化到0.18秒了

雪豹 15:44:52

0.07秒

蘭花島主 15:45:00

All_suite?

蘭花島主 15:45:07

嗯,這就差不多了。

蘭花島主 15:45:20

幾十ms

雪豹 15:45:28

All_suite suit_no 做成索引了

雪豹 15:45:40

這回差不多了

蘭花島主 15:45:44

嗯,差不多了。

至此,對方還算比較滿意,效能也有了大幅提升,鑒於多方面因素,對以文進行了必要處理,記錄於此,以和各位同行共勉。

 

 

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.