800万条记录查询性能

时间:2016-03-22 15:14:53

标签: mysql sql ssms performance-testing

我的查询概述如下。目前,由于zt_Arrival_Data表中有超过800万条记录,因此运行需要8分钟以上,而zt_Tpl_Tuple_Stats_2故事只有9774条记录,总输出只有6946条唯一记录。

我可以用什么方式构建此查询以提高性能?

SELECT  distinct b.Tuple_ID
    ,   LTRIM(RTRIM(a.ORIGIN_CITY)) + ', ' + LTRIM(RTRIM(a.ORIGIN_STATE))   AS Origin_TX
    ,   LTRIM(RTRIM(a.DESTINATION_CITY)) + ', ' + LTRIM(RTRIM(a.DESTINATION_STATE)) AS Destination_TX
    ,   LTRIM(RTRIM(a.ORIGIN_CITY)) + ' - ' + LTRIM(RTRIM(a.CUSTOMER_NAME)) AS Origin_Customer_TX
    ,   LTRIM(RTRIM(a.ORIGIN_CITY)) + ' - ' + LTRIM(RTRIM(a.DESTINATION_CITY)) AS Origin_Destination_TX
    ,   LTRIM(RTRIM(a.CUSTOMER_NAME))   AS Customer_Name
    ,   LTRIM(RTRIM(a.CUSTOMER_NAME)) + ', ' + LTRIM(RTRIM(a.CUSTOMER_NO))  AS Customer_TX
    ,   CASE 
            WHEN LTRIM(RTRIM(a.CUSTOMER_TYPE)) = 'C' THEN 'Customer'
            WHEN LTRIM(RTRIM(a.CUSTOMER_TYPE)) = 'I' THEN 'Internal'
            WHEN LTRIM(RTRIM(a.CUSTOMER_TYPE)) = 'S' THEN 'Shop'
            WHEN LTRIM(RTRIM(a.CUSTOMER_TYPE)) = '' THEN 'zUnkown'
            ELSE LTRIM(RTRIM(a.CUSTOMER_TYPE))
        END AS Customer_Type
    ,   CASE
            WHEN a.CARE_OF_NAME = '' THEN 'zUnknown'
            ELSE a.CARE_OF_NAME
        END AS Care_of_Name
    ,  LTRIM(RTRIM(a.ORIGIN_CITY        ))  AS Origin_City
    ,  LTRIM(RTRIM(a.ORIGIN_STATE       ))  AS Origin_State
    ,  LTRIM(RTRIM(a.DESTINATION_CITY   ))  AS Destination_City
    ,  LTRIM(RTRIM(a.DESTINATION_STATE  ))  AS Destination_State
    ,  LTRIM(RTRIM(b.BusinessGroup_TX   ))  AS BusinessGroup_TX
    ,   b.Fleet_TX AS Fleet_TX
    ,   c.Leg_TX AS Leg_TX
FROM         zt_Arrival_Data a
INNER JOIN   zt_Tpl_Tuple_Stats_2       b
            ON LTRIM(RTRIM(a.ORIGIN_CITY)) + ', ' + LTRIM(RTRIM(a.ORIGIN_STATE)) = b.ORIGIN_TX
            AND LTRIM(RTRIM(a.DESTINATION_CITY)) + ', ' + LTRIM(RTRIM(a.DESTINATION_STATE)) = b.DESTINATION_TX
            AND a.CUSTOMER_NO = b.CUSTOMER_CD
            AND a.BUSINESS_GROUP = b.BusinessGroup_TX
            AND a.[FLEET_ID (GEN PLANT)] = b.Fleet_TX
    JOIN zt_LegMap c ON c.Leg_CD = b.Leg_CD

2 个答案:

答案 0 :(得分:0)

最好只修改数据条目上的数据,而不是仅仅针对select中的大表执行此操作。

这是一个特别糟糕的设计,你必须连接才能加入。在执行这些操作时,您将无法使用索引。在SQL Server中,我会创建一个Ci可以加入的计算持久列,而不确定mysql是否有这样的东西。但你应该调查一下这个。

答案 1 :(得分:0)

根据我的经验,我已经了解到,当您需要格式化字段以加入表格时,您应该格式化表格的列,这些列具有较少的行以匹配具有更多行的字段,必须进行未比较的比较

从一开始就有一些想法:

FROM         zt_Arrival_Data a
INNER JOIN   zt_Tpl_Tuple_Stats_2       b
        ON a.ORIGIN_CITY = <format the b table columns to match a.ORIGIN_CITY>
        AND a.DESTINATION_STATE = <format the b table columns to match a.DESTINATION_STATE>
        AND a.DESTINATION_CITY = <format the b table columns to match a.DESTINATION_CITY>
        AND a.ORIGIN_STATE = <format the b table columns to match a.ORIGIN_STATE>
        AND a.CUSTOMER_NO = b.CUSTOMER_CD
        AND a.BUSINESS_GROUP = b.BusinessGroup_TX
        AND a.[FLEET_ID (GEN PLANT)] = b.Fleet_TX