{
  "source_file": "0930",
  "source_platform": "Ghost",
  "author": "Deep Learning Nerds",
  "title_listed": "How to Use dbt Snapshots with a Practical Example",
  "keywords": "",
  "domain": "数据处理",
  "content_type": "技术教程",
  "is_paid_flag": "否",
  "url": "https://www.deeplearningnerds.com/how-to-use-dbt-snapshots-with-a-practical-example/",
  "publish_date": "2026-06",
  "corpus_stage": "",
  "notes": "",
  "id": "49135dd07dc9",
  "collected_at": "2026-10-02T12:25:03",
  "title": "How to Use dbt Snapshots with a Practical Example",
  "body_markdown": "📘 Introduction\ndbt snapshots help you keep track of changes in source data over time. They are especially useful when a source table overwrites records instead of storing historical versions.\nWe use the same\ndlnerds_university\nMedallion Architecture example project from previous dbt posts, so the project structure stays familiar.\nIn this tutorial, you will learn how to use dbt snapshots with a practical example. We will create a snapshot for\nstudent_raw\n, a raw source table in\n01_bronze\n, and track changes with the recommended\ntimestamp\nstrategy.\nThe goal is simple: preserve historical versions of student records when values such as\ncountry\nchange in the source table.\n💡 What are we implementing?\nWe will implement this workflow:\nstudent_raw -> student_snapshot -> historical student analysis\nThe source table\nstudent_raw\ncontains the current version of each student. The snapshot table\nstudent_snapshot\nstores historical versions by adding dbt metadata columns such as\ndbt_valid_from\nand\ndbt_valid_to\n.\nThis is useful when you want to answer questions like: Which country was a student assigned to last month? When did a student record change? What was the previous value?\n✅ Prerequisites\n☑️ A working dbt project\n☑️ Raw student data already loaded into the warehouse or local database\n☑️ The raw source table defined in\nmodels/01_bronze/sources.yml\n☑️ A reliable\nupdated_at\ncolumn in the source table\n☑️ Basic knowledge of running dbt commands\n🧱1️⃣ Understand the current project setup\nFor this tutorial, we start from a small excerpt of the\ndlnerds_university\nproject:\ndlnerds_university/\n├── dbt_project.yml\n├── models/\n│   └── 01_bronze/\n│       └── sources.yml\n└── snapshots/\nThe\nsnapshots\nfolder is where we will add the snapshot definition.\n🔎2️⃣ Check the Bronze source definition\nBefore creating the snapshot, make sure the raw source table is defined in\nmodels/01_bronze/sources.yml\n:\nversion: 2\n\nsources:\n  - name: bronze\n    schema: 01_bronze\n    tables:\n      - name: student_raw\n        description: Raw student data loaded into the Bronze layer.\n        columns:\n          - name: student_id\n            description: Unique identifier of the student.\n            data_tests:\n              - not_null\n              - unique\n          - name: student_name\n            description: Full student name.\n          - name: country\n            description: Current country of the student.\n          - name: updated_at\n            description: Timestamp when the source record was last updated.\nThe most important field for this tutorial is\nupdated_at\n. dbt uses this column to detect whether a row changed when we use the\ntimestamp\nstrategy.\n🧾3️⃣ Are dbt snapshots SQL files?\nYes, dbt snapshots can be SQL files. The older style uses a\n.sql\nfile inside the\nsnapshots\nfolder with a\nsnapshot\nblock and a\nselect\nstatement.\n{% snapshot student_snapshot %}\n\n{{\n    config(\n        target_schema='snapshots',\n        unique_key='student_id',\n        strategy='timestamp',\n        updated_at='updated_at'\n    )\n}}\n\nselect *\nfrom {{ source('bronze', 'student_raw') }}\n\n{% endsnapshot %}\nIn current dbt versions, the recommended approach for new snapshots is to define the snapshot in a YAML property file, for example\nsnapshots/student_snapshot.yml\n. That is why this tutorial uses YAML for the implementation.\n💡\nThe SQL snapshot style is still useful to recognize when you see older dbt projects. For new snapshots, prefer the YAML configuration style unless your project has a specific reason to use the legacy SQL block.\n🎓 Want to go deeper with dbt?\nIf you want to understand dbt project structure, snapshots, models, and Medallion Architecture workflows in more detail, the Academy and the dbt book go deeper into hands-on dbt projects with practical examples.\nIf you’d like to dive deeper into\ndbt (data build tool)\n, our book\nBuilding Modern Data Pipelines with dbt: From Raw Data to Gold Standard with the Medallion Architecture\nprovides a hands-on guide to designing modern data pipelines. It covers dbt’s core concepts and best practices, including building Bronze, Silver, and Gold layers with the\nMedallion Architecture\n. It also serves as a hands-on study guide for the\ndbt Analytics Engineering Certification\n.\nView on Amazon\nYou can view this post with the tier:\n      Academy Membership\nJoin academy now to read the post and get access to the full library of premium posts for academy members only.\nJoin Academy\nAlready have an account?\nSign In",
  "metadata": {
    "title": "How to Use dbt Snapshots with a Practical Example",
    "description": "Learn how to use dbt snapshots with a practical student example, the timestamp strategy, updated_at, and historical records.",
    "keywords": null,
    "author": null,
    "og:site_name": "Deep Learning Nerds | The ultimate Learning Platform for AI and Data Science",
    "og:type": "article",
    "og:title": "How to Use dbt Snapshots with a Practical Example",
    "og:description": "Learn how to use dbt snapshots with a practical student example, the timestamp strategy, updated_at, and historical records.",
    "og:url": "https://www.deeplearningnerds.com/how-to-use-dbt-snapshots-with-a-practical-example/",
    "og:image": "https://storage.ghost.io/c/5b/26/5b26e983-a59f-4f4b-a5f2-bd8c5a0a38e2/content/images/size/w1200/2026/06/dbt---How-to-Use-dbt-Snapshots-with-a-Practical-Example.png",
    "og:image:width": "1200",
    "og:image:height": "839",
    "twitter:card": "summary_large_image",
    "twitter:title": "How to Use dbt Snapshots with a Practical Example",
    "twitter:description": "Learn how to use dbt snapshots with a practical student example, the timestamp strategy, updated_at, and historical records.",
    "twitter:url": "https://www.deeplearningnerds.com/how-to-use-dbt-snapshots-with-a-practical-example/",
    "twitter:image": "https://storage.ghost.io/c/5b/26/5b26e983-a59f-4f4b-a5f2-bd8c5a0a38e2/content/images/size/w1200/2026/06/dbt---How-to-Use-dbt-Snapshots-with-a-Practical-Example.png",
    "twitter:label1": "Written by",
    "twitter:data1": "Data Engineer",
    "twitter:label2": "Filed under",
    "twitter:data2": "dbt, Data Engineering, dbt Analytics Engineering Certification",
    "twitter:site": "@DLNerds",
    "article:published_time": "2026-06-01T15:48:16.000Z",
    "article:modified_time": "2026-06-01T17:39:10.000Z",
    "article:tag": "dbt Analytics Engineering Certification",
    "article:publisher": "https://www.facebook.com/deeplearningnerds/",
    "article:author": "https://www.facebook.com/deeplearningnerds/"
  },
  "paywall": {
    "listed_paid": false,
    "detected": false,
    "marker": "",
    "public_only": false,
    "policy": "付费/会员内容只保留页面公开可见部分，不绕过付费墙"
  },
  "crawl": {
    "success": true,
    "status_code": 200,
    "error": ""
  },
  "code_blocks": [
    {
      "file": "attachments/49135dd07dc9/code/body-000.txt",
      "chars": 62,
      "language": "text"
    },
    {
      "file": "attachments/49135dd07dc9/code/body-001.txt",
      "chars": 109,
      "language": "text"
    },
    {
      "file": "attachments/49135dd07dc9/code/body-002.txt",
      "chars": 605,
      "language": "text"
    },
    {
      "file": "attachments/49135dd07dc9/code/body-003.txt",
      "chars": 258,
      "language": "text"
    }
  ],
  "images": [
    {
      "status": "ok",
      "url": "https://storage.ghost.io/c/5b/26/5b26e983-a59f-4f4b-a5f2-bd8c5a0a38e2/content/images/2026/03/Mockup-Transparent-PNG-1.png",
      "bytes": 1130793,
      "reused": true,
      "local_path": "attachments/49135dd07dc9/images/005.png",
      "src": "https://storage.ghost.io/c/5b/26/5b26e983-a59f-4f4b-a5f2-bd8c5a0a38e2/content/images/2026/03/Mockup-Transparent-PNG-1.png",
      "alt": "CTA Image",
      "downloaded": true
    }
  ],
  "audio": [],
  "videos": [],
  "links": [
    {
      "href": "https://amzn.eu/d/04HPLJMo?ref=deeplearningnerds.com",
      "label": "",
      "type": "a"
    },
    {
      "href": "https://www.deeplearningnerds.com/membership/",
      "label": "Join Academy",
      "type": "a"
    },
    {
      "href": "https://www.deeplearningnerds.com/signin/",
      "label": "Sign In",
      "type": "a"
    }
  ],
  "repair_version": 2,
  "repaired_at": "2026-10-14T10:47:43Z",
  "source_url": "https://www.deeplearningnerds.com/how-to-use-dbt-snapshots-with-a-practical-example/",
  "papers": [],
  "actual_video_files": [],
  "body_chars": 4467,
  "expected_body_images": 1,
  "discovery_status": "ok",
  "delivery_status": "ok",
  "missing_reason": "",
  "raw_html": "attachments/49135dd07dc9/pages/source.html",
  "local_html": "attachments/49135dd07dc9/pages/article.html",
  "body_base_path": "attachments/49135dd07dc9/pages/",
  "body_markdown_path": "attachments/49135dd07dc9/pages/article.md"
}