无法正确加入表格。查询所花费的无限时间

时间:2017-04-19 03:32:51

标签: mysql sql join left-join sqlperformance

我遇到了sqlcode的性能问题。使用Tableau平台

我有大约11个表连接。当我调试以查看10个连接表的性能时,它在表格中非常快,但当我再添加一个billing_transaction表时,即使有一小组行,查询也永远不会结束。

Here是整个数据库的ERD。它创建一个长查询的问题可能是什么?

这是SQL代码(我已经注释掉了没有表billing_transaction的FROM部分,正在用它来比较性能)

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

1 个答案:

答案 0 :(得分: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),您已经在其上应用了无过滤器。但是left join在这里有什么意义呢?没有客户可以进行结算交易吗?

同样,审核所有LEFT JOINS ,如果可能,请用INNER JOINS替换它们。然后适当地应用过滤器(如果可能,则存在索引)。