Split large queries into smaller components to use with Automation Studio’s query
activity.
Learn how to divide a single query into multiple, more efficient queries. In this
example, a query designed to find Opens in the last 30 days based on an EmailID consistently
times out. Don't use this code in a query activity.
SELECT
j.JobID,
j.EmailName,
j.DeliveredTime AS SendTime,
o.EventDate AS OpenTime,
s.EmailAddress,
s.SubscriberKey
FROM [_Job] j
JOIN [_Open] o
ON j.JobID = o.JobID
JOIN [_Subscribers] s
ON o.SubscriberID = s.SubscriberID
WHERE
o.IsUnique = 1 AND
j.EmailID = 123456
This query may time out because it tries to retrieve data from
multiple sources in one query. To make sure that this query consistently returns data in a
timely manner, divide it into four smaller queries on separate, consecutive steps of your
automation. Use these instructions to set up four data extensions and corresponding queries
called Jobs, Opens, Subscriber, and Target Data Extension. The Target Data Extension unites
and stores the data retrieved by the preceding queries.
Create a Jobs Table Data Extension
Create a data extension, then
select standard data extension.
| Data Extension Name |
JobTable |
| External Key |
JobTable |
Fields
| Name |
Data Type |
Length |
Primary Key |
Nullable |
Default Value |
| JobID |
Number |
|
|
|
|
| EmailName |
Text |
100 |
|
|
|
| DeliveredTime |
Date |
|
|
|
|
Create a Query Activity to Populate the Job Table
| Query Name |
JobTable |
| External Key |
JobTable |
Query Syntax
SELECT
j.JobID,
j.EmailName,
j.DeliveredTime,
FROM [_Job] j
WHERE
j.EmailID = 123456
| Target Data Extension Name |
JobTable |
| Data Action |
Overwrite |
Create an Opens Data Extension
Create a data extension, then select
standard data extension.
| Data Extension Name |
OpenTable |
| External Key |
OpenTable |
Fields
| Name |
Data Type |
Length |
Primary Key |
Nullable |
Default Value |
| JobID |
Number |
|
|
|
|
| SubscriberID |
Number |
|
|
|
|
| OpenTime |
Date |
|
|
|
|
Create an Opens Query Activity
| Query Name |
OpenTable |
| External Key |
OpenTable |
Query Syntax
SELECT
o.JobID,
o.SubscriberID,
o.EventDate AS OpenTime
FROM [_Open] o
WHERE
o.IsUnique = 1
| TargetData Extension |
OpenTable |
| Data Action |
Overwrite |
Create a Subscriber Data Extension
Create a data extension, then
select standard data extension.
| Target Data Extension Name |
SubscriberTable |
| External Key |
SubscriberTable |
Fields
| Name |
Data Type |
Length |
Primary Key |
Nullable |
Default Value |
| SubscriberID |
Number |
|
|
|
|
| EmailAddress |
EmailAddress |
254 |
|
|
|
| SubscriberKey |
Text |
254 |
|
|
|
Set Up a Subscriber Query Activity
| Query Name |
SubscriberTable |
| External Key |
SubscriberTable |
Query Syntax
SELECT
s.SubscriberID,
s.EmailAddress,
s.SubscriberKey
FROM [_Subscribers] s
JOIN [OpenTable] o
ON s.SubscriberID = o.SubscriberID
| Target Data Extension |
SubscriberTable |
| Data Action |
Overwrite |
Create a Data Extension to Store Opens Data
Create a data extension,
then select standard data extension.
| Target Data Extension |
OpensIn30Days |
| External Key |
OpensIn30Days |
Fields
| Name |
Data Type |
Length |
Primary Key |
Nullable |
Default Value |
| JobID |
Number |
|
|
|
|
| EmailName |
Text |
100 |
|
|
|
| SendTime |
Date |
|
|
|
|
| OpenTime |
Date |
|
|
|
|
| EmailAddress |
EmailAddress |
254 |
|
|
|
| SubscriberKey |
Text |
254 |
|
|
|
Create a Query to Send Opens Data to Target Data Extension
| Query Name |
OpensIn30Days |
| External Key |
OpensIn30Days |
Query Syntax
SELECT
j.JobID,
j.EmailName,
j.DeliveredTime AS SendTime,
o OpenTime,
s.EmailAddress,
s.SubscriberKey
FROM [JobTable] j
JOIN [OpenTable] o
ON j.JobID = o.JobID
JOIN [SubscriberTable] s
ON o.SubscriberID = s.SubscriberID
| Target Data Extension |
OpensIn30Days |
| Update Type |
Overwrite |