SQL Segment Extensions
You can generate a Segment Extension using Snowflake SQL queries of Snowflake data. SQL can help you unlock new segment use cases because it offers the flexibility to describe the relationships between data in ways that aren’t achievable through other segmentation features.
Like standard Segment Extensions, you can query events from up to the past two years (730 days) in your SQL Segment Extension.
Types of SQL Segment Extensions
There are two types of SQL editors to choose from when creating your SQL Segment Extension: the SQL Editor, and the Incremental SQL Editor.
- Creating extensions with SQL Editor (full refresh): Each time your segment refreshes, Braze will query all available data to update your segment, which will use more credits than incremental refreshes. Full refresh extensions can automatically regenerate membership daily, but can’t be refreshed using incremental refresh.
- Creating extensions with Incremental SQL Editor (incremental refresh): Incremental refresh calculates only the last two days’ worth of data, which is more cost-efficient and uses up fewer credits each time. When you create an incremental refresh SQL segment, you can set it to automatically regenerate membership daily.
The main benefit of extensions with incremental refresh is that you can set your segment to automatically refresh membership daily. Segments created with our regular SQL editor can only have their membership refreshed manually. This helps reduce the cost of a daily data refresh for SQL Segment Extensions.
You can do a manual full refresh on all SQL Segments created in either SQL editor.
Creating SQL Segment Extensions
SQL queries that take longer than 20 minutes to run will time out.
When the extension finishes processing, you can create a segment using your Segment Extension and target this new segment with your campaigns and Canvases.
Writing SQL
Your SQL query should be written using Snowflake syntax. Consult the table reference for a full list of tables and columns available to be queried.
Note that the tables available to query contain only event data. If you wish to query for user attributes, you should combine your SQL segment with custom attribute filters from the classic segmenter.
Your SQL must additionally adhere to the following rules:
- Write a single SQL statement. Do not include any semicolons.
- Your SQL must select only one column: the
user_id
column. This means your SQL must contain:
1
SELECT DISTINCT user_id FROM "INSERT TABLE NAME"
- It isn’t possible to query for users with zero events, which means any query for users that have done an event less than X times would need to follow this workaround:
- Write a query to select users who have the event MORE than X times.
- When referencing your Segment Extension in your segment, select
doesn't include
to invert the result.
All incremental refresh queries consist of two parts: a query, and schema details.
- In the editor, write a query that selects
user_id
s from your desired table. - Add schema details by selecting an Operator, Number of times, and Time period from the fields above the editor. The query will check if the sum of the aggregate column meets a certain condition specified by the
{{operator}}
and{{number of times}}
placeholders. This functions similarly to the workflow for creating classic Segment Extensions.
- Operator: Indicate if the event has happened more than, less than, or equal to a number of occurrences.
- Number of times: How many times you would like to evaluate the event in relation to the operator.
- Time period: Number of days from 1 to 730 in which you want to check instances of the event. This time period refers to past days relative to the current day. The following example shows querying for users that performed the event more than 5 times in the past 365 days.
- Operator: Indicate if the event has happened more than, less than, or equal to a number of occurrences.
In the following example, the resulting segment would contain users that performed the favorited
event more than 3 times during the last 30 days, after a specified date.
Additional rules
Your incremental refresh query must additionally adhere to the following rules:
- Write a single SQL statement. Do not include any semicolons.
- Your incremental SQL segment would be able to refer to just one single event. Your dropdowns for date and count are in reference to your chosen event.
- Your SQL must have the following columns:
user_id
,$start_date
, and an aggregation function (such asCOUNT
). Any SQL saved without these three fields will result in an error.
Incremental refresh segments take into account late events, which are events that occurred more than 2 days ago (for example, SDK events that weren’t sent at the time they were captured).
Previewing results
Before saving, you can run a preview of your query. Query previews are automatically limited to 100 rows and will timeout after 60 seconds. The user_id
column requirement does not apply when running a preview.
For incremental SQL Segment Extensions, the preview will not include the additional criteria from your operator, number of times, and time period fields.
Managing SQL Segment Extensions
On the Segment Extensions page, segments generated using SQL are denoted with next to their name.
Select a SQL Segment Extension to view where the extension is being used, archive the extension, or manually refresh the segment membership.
Refreshing segment membership
To refresh the segment membership of any Segment Extension created using SQL, open the Segment Extension and select Refresh. Only incremental refresh SQL Segment Extensions can automatically regenerate (if selected).
If you created a segment where you expect users to enter and exit regularly, manually refresh the Segment Extension it uses before targeting that segment in a campaign or Canvas.
Monitoring your SQL Segments usage
Each Braze workspace has 5 Snowflake credits available per month. If you need more credits, contact your account manager. Credits are used whenever you refresh, or save and refresh, a SQL Segment’s membership. Credits are not used when you run previews within a SQL Segment or save or refresh a classic Segment Extension.
Snowflake credits are not shared between features. For example, credits across SQL Segment Extensions and Query Builder are independent of each other.
Credit usage is correlated to the run time of your SQL query. The longer the run time is, the more credits a query will cost. Run time can vary depending on the complexity and size of your queries over time. The more complex and frequent queries you run, the larger your resource allocation and the faster your run time becomes.
To save on credits, preview your query to ensure it is correct before saving the SQL Segment Extension.
Your credits will reset to 5 on the first of each month at 12 am UTC. You can monitor your credit usage throughout the month within the credits usage panel. From the Segment Extensions page, click View SQL Credit Usage.
The following will happen when your credits reach zero:
- Any SQL Segment Extensions set up to automatically refresh stop refreshing, impacting the membership of these segments and any campaigns or Canvases that target these segments.
- You can only save new SQL Segment Extensions as drafts for the remainder of the month.
All company users who created a SQL Segment and your company admins will receive a notification email when you have used up 50%, 80%, and 100% of your credits. After your credits reset at the start of the next month, you can create more SQL Segments, and automatic refreshes will resume.
If you want to purchase more SQL Segment credits or additional Segment Extensions, please contact your account manager.
Troubleshooting
Your query may fail for any of the following reasons:
- Syntax errors in your SQL query
- SQL does not adhere to the SQL rules
- Processing timeout (after 20 minutes)