SQL中的SQL连接

时间:2013-09-09 14:36:21

标签: sql sybase

我有一个名为mc_spot_customer_viewing_capped_thin4的表,它有533,523,823行查看数据。现在我要为每个客户添加一些分析属性。我正在运行以下代码:

SELECT A.*
,b.cb_key_household
,b.viewing_panel_id
,b.mirror_men
,b.mirror_women
,b.mirror_has_children
,b.demographic
,b.financial_outlook
,b.h_affluence
,b.homeowner
,b.h_lifestage
,b.region
,b.sky_go_reg
,b.value_segment
,b.sports_downgrade
,b.movies_downgrade
,b.household_composition
,b.current_package
,b.tenure
,b.social_class
,b.TV_PREMIUMS
,b.TV_PACKAGE
,b.TENURE_IN_MONTHS
,b.DOWNLOADS_CAMPAIGN_PERIOD
,b.ON_DEMAND_CAMPAIGN_PERIOD_FLAG
,b.MOSAIC_GROUP
,b.FINANCIAL_STRATEGY_SEGMENT
,b.ENGAGEMENT_SEGMENT
,b.PANELISTS_THAT_VIEWED_SPOTS
,b.NEW_SKY_GO_USERS
,b.SKY_GO_USERS_BEFORE_CAMPAIGN
,b.SKY_GO_USER_TYPE_BEFORE_CAMPAIGN
INTO #FINAL_ALL_SPOTS_PROFILE
FROM (SELECT * FROM mc_spot_customer_viewing_capped_thin4 WHERE SPOT_VIEWED_DURATION = SPOT_DURATION) AS A
LEFT JOIN PROFILING_LOOKUP_TABLE_V9 AS B
ON A.ACCOUNT_NUMBER = B.ACCOUNT_NUMBER

这需要很长时间才能运行。是否有更优化的方式可以运行代码以更快地获得输出。请提出建议,我对SQL领域几乎是新手。

提前谢谢你, 的问候,

SD

1 个答案:

答案 0 :(得分:2)

我不知道它是否会改变性能,但你可以删除子查询:

SELECT A.*
...
INTO #FINAL_ALL_SPOTS_PROFILE
FROM mc_spot_customer_viewing_capped_thin4 AS A
LEFT JOIN PROFILING_LOOKUP_TABLE_V9 AS B
ON A.ACCOUNT_NUMBER = B.ACCOUNT_NUMBER
WHERE A.SPOT_VIEWED_DURATION = A.SPOT_DURATION

您还可以在SPOT_VIEWED_DURATION = SPOT_DURATION

上添加索引

另一种选择是:

FROM mc_spot_customer_viewing_capped_thin4 AS A
LEFT JOIN PROFILING_LOOKUP_TABLE_V9 AS B
ON A.ACCOUNT_NUMBER = B.ACCOUNT_NUMBER
WHERE A.SPOT_VIEWED_DURATION - A.SPOT_DURATION = 0

索引为SPOT_VIEWED_DURATION - SPOT_DURATION

这两个在你的情况下是等价的,但是如果你想要在一个范围内产生差异,第二个会更快,例如:

WHERE A.SPOT_VIEWED_DURATION - A.SPOT_DURATION BETWEEN -1 AND 1