2017-04-19 99 views
1

我面臨着我的sqlcode的性能問題。使用Tableau平臺無法正確加入表格。無限時間查詢

我有大約11個表連接。當我調試以查看10個連接表的表現時,它在表格中非常快,但是當我再添加一個表billing_transaction時,即使只有一小排行,查詢也不會結束。

Here是整個數據庫的ERD。什麼可能是它創建一個長查詢的問題?

下面是SQL代碼(我註釋掉從外面表billing_transaction部分,在做它比較性能)

select 
    country.`name` as Country, 
    oper.`name` as Operator, 
    proj.`name` as Project, 
    club.`name` as Club, 
    prom.`name` as Promotor, 
    cust.`idCustomer` as SubscriberID, 
    cust_sub.`msisdn` as SubscriberMsidn, 
    cust.`customerSince` as SubscriberStartingDate, 
    cust_sub.`SubscribedDate` as SubscriberSubcribeDate, 
    cust_sub.`UnsubscribedDate` as SubscriberUnSubcribeDate, 
    cust_sub.`idCustomerSubscription` as SubscriptionID, 
    bt.`idBillingTransaction` as BillingTransactionID, 
    cust_sub.`eventId` as SubscriberEventID, 
    bt_status.`name` as BillingStatus, 
    bt.`price` as Pricefromsubscriber 



-- FROM `customersubscription` cust_sub (WITHOUT billing_transaction it WORKS FINE) 
-- LEFT JOIN `customer` cust on cust_sub.`idCustomer`=cust.`IdCustomer` 
-- LEFT JOIN `promoter` prom on cust_sub.`idPromoter`=prom.`id` 
-- LEFT JOIN `club` club on cust_sub.`idClub`=club.`idClub` 
-- LEFT JOIN `Project` proj on club.`idProject`=Proj.`idProject` 
-- LEFT JOIN `project_operator_relationships` proj_rel_oper on proj.`idProject`=proj_rel_oper.`projectId` 
-- LEFT JOIN `Operator` as oper on proj_rel_oper.`operatorId`=oper.`idOperator` 
-- LEFT JOIN `country` as country on oper.`idCountry`=country.`idCountry` 
-- LEFT JOIN `curreny_symbol` as curr_sym on country.`idCurrencySymbol`=curr_sym.`symbol`, 


FROM `billing_transaction` bt 
    LEFT JOIN `customersubscription` cust_sub on bt.`msisdn`=cust_sub.`msisdn` 
    LEFT JOIN `customer` cust on cust.`idCustomer`=cust_sub.`IdCustomer` 
    LEFT JOIN `billing_status` bt_status on bt.`idBillingStatus`=bt_status.`idBillingStatus` 
    LEFT JOIN `promoter` prom on cust_sub.`idPromoter`=prom.`id` 
    LEFT JOIN `club` club on cust_sub.`idClub`=club.`idClub` 
    LEFT JOIN `Project` proj on club.`idProject`=Proj.`idProject` 
    LEFT JOIN `project_operator_relationships` proj_rel_oper on proj.`idProject`=proj_rel_oper.`projectId` 
    LEFT JOIN `Operator` as oper on proj_rel_oper.`operatorId`=oper.`idOperator` 
    LEFT JOIN `country` as country on oper.`idCountry`=country.`idCountry` 
    LEFT JOIN `curreny_symbol` as curr_sym on country.`idCurrencySymbol`=curr_sym.`symbol` 

where proj.`idProject` IN (3, 19) 
-- where proj.`idProject` IN (3, 19, 23, 24, 27) 
    and date(cust_sub.`SubscribedDate`)>='2017-04-18 01:40:00' 
    and date(cust_sub.`SubscribedDate`)< '2017-04-18 02:00:00' 

任何建議將非常感謝。調試所有,但我不知道性能問題在哪裏。我也不能忽視這張表

我的方法(對於它來說可能是錯誤的)是,通過使用billing_transaction表,保持左連接並在行的下一列中添加信息,如客戶訂閱,客戶,運營商,俱樂部,項目......一路到國家。左連接適合這種情況?此外,where子句從billing_transaction(主表)或where子句可以來自連接表中的任何表。

的順序是這樣的

Each customer has one or more subscription 
Each Subscription has one or more billing transaction 
Each billing transaction has one or more billing status 


Other relations are: 
Each customer subscription have one or more club 
Each customer subscription has one or more promotor 
Each club has one or more project 
Each project has one or more operator 
Each operator has one or more countries 
Each country has one or more currency 
+0

建議創建一個小提琴,這樣我們可以玩 – maSTAShuFu

+0

小提琴的意思? – PythonDev

+0

sqlfiddle.com因此您可以創建您的架構 – maSTAShuFu

回答

0

更改左邊

SELECT ... 
FROM `billing_transaction` bt 
    LEFT JOIN `customersubscription` cust_sub on bt.`msisdn`=cust_sub.`msisdn` 
    ... 

加入到內部連接:

SELECT ... 
FROM `billing_transaction` bt 
    JOIN `customersubscription` cust_sub on bt.`msisdn`=cust_sub.`msisdn` 
    ... 

你留加入一個潛在的巨大的表(billing_trnsaction )您已經申請無過濾器 a高。但是這裏的left join有什麼意義?沒有客戶可以進行結算交易嗎?

同樣,查看所有您的左連接,並儘可能用INNER連接替換它們。然後適當地應用濾波器(如果索引存在的話可能的話)。

+0

我不確定哪一個內連接和哪一個左連接。 – PythonDev

+0

對於初學者,請嘗試我的建議(用JOIN替換第一個LEFT JOIN)並讓我們知道它是怎麼回事。 –

+0

好的。堅持就會試試 – PythonDev