|
| 1 | +# Materialized Views |
| 2 | + |
| 3 | +Materialized Views in Synapse allow you to create queryable views that store the |
| 4 | +results of a Synapse SQL statement. These views are useful for combining, filtering, or |
| 5 | +transforming data from multiple tables into a single, queryable entity. |
| 6 | + |
| 7 | +This tutorial will walk you through the basics of working with Materialized Views |
| 8 | +using the Synapse Python client. |
| 9 | + |
| 10 | +## Tutorial Purpose |
| 11 | +In this tutorial, you will: |
| 12 | + |
| 13 | +1. Log in, get your project, and create tables with data |
| 14 | +2. Create and query a Materialized View |
| 15 | +3. Create and query a Materialized View with a JOIN clause |
| 16 | +4. Create and query a Materialized View with a LEFT JOIN clause |
| 17 | +5. Create and query a Materialized View with a RIGHT JOIN clause |
| 18 | +6. Create and query a Materialized View with a UNION clause |
| 19 | + |
| 20 | +## Prerequisites |
| 21 | +* This tutorial assumes that you have a Synapse project. |
| 22 | +* Pandas must also be installed as shown in the [installation documentation](../installation.md). |
| 23 | + |
| 24 | +## 1. Log in, get your project, and create tables with data |
| 25 | + |
| 26 | +Before creating Materialized Views, we need to log in to Synapse, retrieve your project, |
| 27 | +and create the tables with data that will be used in the views. |
| 28 | + |
| 29 | +You will want to replace `"My uniquely named project about Alzheimer's Disease"` with |
| 30 | +the name of your project. |
| 31 | + |
| 32 | +```python |
| 33 | +{!docs/tutorials/python/tutorial_scripts/materializedview.py!lines=3-72} |
| 34 | +``` |
| 35 | + |
| 36 | +## 2. Create and query a Materialized View |
| 37 | + |
| 38 | +First, we will create a simple Materialized View that selects all rows from a table and |
| 39 | +then query it to retrieve the results. |
| 40 | + |
| 41 | +```python |
| 42 | +{!docs/tutorials/python/tutorial_scripts/materializedview.py!lines=75-97} |
| 43 | +``` |
| 44 | + |
| 45 | +<details class="example"> |
| 46 | + <summary>The result of querying your Materialized View should look like:</summary> |
| 47 | +``` |
| 48 | +Results from the materialized view: |
| 49 | + sample_id patient_id age diagnosis |
| 50 | +0 S1 P1 70 Alzheimer's |
| 51 | +1 S2 P2 65 Healthy |
| 52 | +2 S3 P3 72 Alzheimer's |
| 53 | +3 S4 P4 68 Healthy |
| 54 | +4 S5 P5 75 Alzheimer's |
| 55 | +5 S6 P6 80 Healthy |
| 56 | +``` |
| 57 | +</details> |
| 58 | + |
| 59 | +## 3. Create and query a Materialized View with a JOIN clause |
| 60 | + |
| 61 | +Next, we will create a Materialized View that combines data from two tables using a JOIN |
| 62 | +clause and then query it to retrieve the results. |
| 63 | + |
| 64 | +```python |
| 65 | +{!docs/tutorials/python/tutorial_scripts/materializedview.py!lines=100-130} |
| 66 | +``` |
| 67 | + |
| 68 | +<details class="example"> |
| 69 | + <summary>The result of querying your Materialized View with a JOIN clause should look |
| 70 | + like:</summary> |
| 71 | +``` |
| 72 | +Results from the materialized view with JOIN: |
| 73 | + sample_id patient_id age diagnosis gene expression_level |
| 74 | +0 S1 P1 70 Alzheimer's APOE 2.5 |
| 75 | +1 S2 P2 65 Healthy APP 1.8 |
| 76 | +2 S3 P3 72 Alzheimer's PSEN1 3.2 |
| 77 | +3 S4 P4 68 Healthy MAPT 2.1 |
| 78 | +4 S5 P5 75 Alzheimer's APP 3.5 |
| 79 | +``` |
| 80 | +</details> |
| 81 | + |
| 82 | +## 4. Create and query a Materialized View with a LEFT JOIN clause |
| 83 | + |
| 84 | +We can also create a Materialized View that includes all rows from one table and matches |
| 85 | +rows from another table using a LEFT JOIN clause and then query it to retrieve the |
| 86 | +results. |
| 87 | + |
| 88 | +```python |
| 89 | +{!docs/tutorials/python/tutorial_scripts/materializedview.py!lines=133-163} |
| 90 | +``` |
| 91 | + |
| 92 | +<details class="example"> |
| 93 | + <summary>The result of querying your Materialized View with a LEFT JOIN clause should |
| 94 | + look like:</summary> |
| 95 | +``` |
| 96 | +Results from the materialized view with LEFT JOIN: |
| 97 | + sample_id patient_id age diagnosis gene expression_level |
| 98 | +0 S1 P1 70 Alzheimer's APOE 2.5 |
| 99 | +1 S2 P2 65 Healthy APP 1.8 |
| 100 | +2 S3 P3 72 Alzheimer's PSEN1 3.2 |
| 101 | +3 S4 P4 68 Healthy MAPT 2.1 |
| 102 | +4 S5 P5 75 Alzheimer's APP 3.5 |
| 103 | +5 S6 P6 80 Healthy NaN NaN |
| 104 | +``` |
| 105 | +</details> |
| 106 | + |
| 107 | +## 5. Create and query a Materialized View with a RIGHT JOIN clause |
| 108 | + |
| 109 | +Similarly, we can create a Materialized View that includes all rows from one table and |
| 110 | +matches rows from another table using a RIGHT JOIN clause and then query it to retrieve |
| 111 | +the results. |
| 112 | + |
| 113 | +```python |
| 114 | +{!docs/tutorials/python/tutorial_scripts/materializedview.py!lines=166-196} |
| 115 | +``` |
| 116 | + |
| 117 | +<details class="example"> |
| 118 | + <summary>The result of querying your Materialized View with a RIGHT JOIN clause should |
| 119 | + look like:</summary> |
| 120 | +``` |
| 121 | +Results from the materialized view with RIGHT JOIN: |
| 122 | + sample_id patient_id age diagnosis gene expression_level |
| 123 | +0 S1 P1 70.0 Alzheimer's APOE 2.5 |
| 124 | +1 S2 P2 65.0 Healthy APP 1.8 |
| 125 | +2 S3 P3 72.0 Alzheimer's PSEN1 3.2 |
| 126 | +3 S4 P4 68.0 Healthy MAPT 2.1 |
| 127 | +4 S5 P5 75.0 Alzheimer's APP 3.5 |
| 128 | +5 S7 NaN NaN NaN PSEN2 1.9 |
| 129 | +``` |
| 130 | +</details> |
| 131 | + |
| 132 | +## 6. Create and query a Materialized View with a UNION clause |
| 133 | + |
| 134 | +Finally, we can create a Materialized View that combines rows from two tables using a |
| 135 | +UNION clause and then query it to retrieve the results. |
| 136 | + |
| 137 | +```python |
| 138 | +{!docs/tutorials/python/tutorial_scripts/materializedview.py!lines=199-229} |
| 139 | +``` |
| 140 | + |
| 141 | +<details class="example"> |
| 142 | + <summary>The result of querying your Materialized View with a UNION clause should look |
| 143 | + like:</summary> |
| 144 | +``` |
| 145 | +Results from the materialized view with UNION: |
| 146 | + sample_id |
| 147 | +0 S1 |
| 148 | +1 S2 |
| 149 | +2 S3 |
| 150 | +3 S4 |
| 151 | +4 S5 |
| 152 | +5 S6 |
| 153 | +6 S7 |
| 154 | +``` |
| 155 | +</details> |
| 156 | + |
| 157 | +## Source Code for this Tutorial |
| 158 | + |
| 159 | +<details class="quote"> |
| 160 | + <summary>Click to show me</summary> |
| 161 | + |
| 162 | +```python |
| 163 | +{!docs/tutorials/python/tutorial_scripts/materializedview.py!} |
| 164 | +``` |
| 165 | +</details> |
| 166 | + |
| 167 | +## References |
| 168 | +- [MaterializedView][synapseclient.models.MaterializedView] |
| 169 | +- [Column][synapseclient.models.Column] |
| 170 | +- [Project][synapseclient.models.Project] |
| 171 | +- [syn.login][synapseclient.Synapse.login] |
0 commit comments