---
title: "Update your analytical data selectively"
excerpt: "Updating specific records in your analytical database couldn't be easier with Tinybird's new 'replace with condition' functionality"
authors: "Jorge Sancha"
categories: "Product updates"
createdOn: "2023-09-25 00:00:00"
publishedOn: "2020-01-16 00:00:00"
updatedOn: "2022-09-08 00:00:00"
status: "published"
---

<p>The ability to “update” data is often not the top priority when designing analytical databases.</p><p>First, the source of truth for the data is often somewhere else (like a Transactional Database); that's where the brunt of the UPDATEs take place. And secondly, what you are looking for when analysing is for the ability to query large quantities of data in multiple ways.</p><!--kg-card-begin: html--><blockquote>That is why these databases most of the time optimise for fast recovery (SELECTs) and fast ingestion (INSERTs).</blockquote><!--kg-card-end: html--><p>But there are always scenarios where you need to update or replace your analytical data: you might have reconciliation processes over your transactions that affect your original data. Or maybe simply your ingestion process was faulty and ingested inaccurate data for a period of time.</p><p>Updating that data can very often be cumbersome, slow and resource intensive, often due to how the data is partitioned or replicated for performance reasons. This is especially true when you are dealing with <strong>replicated tables over multiple servers</strong>, and even harder if you have <strong>materialized views over those tables</strong>, since these will be partitioned or replicated too; that means that not only the original data needs to be updated, but that those views will also have to be regenerated.</p><p>So when the inevitable time to update the data comes, it can easily turn into a nightmare: replacing entire partitions is possible, but you require intimate knowledge of how the data is organised, partitioned or replicated. Also, you might want to replace only part of those partitions or just the data that satisfies certain conditions.</p><figure class="kg-card kg-image-card"><img src="https://tinybird-blog.ghost.io/content/images/2023/09/62662478776abb47c1764267_updatedata-10.png" class="kg-image" alt="Updatedata 10" loading="lazy"></figure><p>At Tinybird we want to make these kinds of updates seamless, so that <strong>you can replace data with full flexibility and without having to worry about replication, how the data is partitioned or anything of the sort</strong>.</p><p>At the same time, we want to ensure that any materialized view that feeds off the data you are replacing gets replaced seamlessly as well, without ever exposing inconsistent or partially replaced data.</p><p>That’s where our “Replace by condition” functionality comes in: we talked about the ability to “replace” entire data sources already in a previous post (you can also check <code>mode=replace</code> in our DataSources API documentation).</p><!--kg-card-begin: html--><blockquote>It is now possible to specify a condition under which only a part of the data is replaced during the ingestion process.</blockquote><!--kg-card-end: html--><p>For instance, let’s say you have a Tinybird data source called <code>events</code> with an <code>event_datetime</code> column and that you want to re-ingest a CSV with the data for January 1st, 2020. In order to update the data you only need to pass the parameter “replace_condition” with the condition <code>toDate(event_datetime) = ‘2020-01-01’</code></p><p>Your API call would look like this:</p><figure class="kg-card kg-embed-card"><iframe loading="lazy" width="100%" src="https://snippets.tinybird.co/XQAAAAJeAQAAAAAAAABBKUqGk9nLKuqyL7oM70y_4pzqfIZc3tJ4WcBUtNHSry8TGJw8A0iKe9j938YEMwK1-8-eMhjsQMXCy0-_SoKrUivwDs5K_0f-86_tFhztAX5WC_BhucB_SjyqAo-EiHvap4LlQqSxa62RRk1p2YWLtsjr289IY0DljVMstpanISu5EIUPFMqmRodl3DR90qTA0mLZduK4gR7wok0JLkOCJIhbmZRq18F_6tpkpnrK0NSeGLQ-Cp5J0JvtXZYoKOV5upHchUbJChmBPYej9ekuIPAjVUc7ifgNuxmcpzqZOmdr0Jj2aP1a5G5QkstW0qbWAHy_ME5auulKsgbodVZbY8qZ7BWns_RCcqKDPANeGxfrNB2ZVhW-N-3_Z5gcAA/embed"></iframe></figure><p>And what if you want to replace events of a certain <code>event_type</code> over the last 30 days?</p><p>In that case, your replace_condition could look like something like:</p><figure class="kg-card kg-embed-card"><iframe loading="lazy" width="100%" src="https://snippets.tinybird.co/XQAAAALGAAAAAAAAAABBKUqGk9nLKzhSjO8WpMF-zKaKjoe0yFbTA5PfSMM-A18GjP7BceYiP9TndtLMix0bFSjAr6KcK2zYelGMfyA5BVO9aV_Ugq9Hrz77tEIowaDI1NohQ1stxmRAtZWqJdJTaHJRfxHhF6i3oMCYc5EuSdn6zvnCZCr9G-6vln0oGgHl_gb_pNxXOksZnLgwqsRhrUerzyEJkCovy7B_kXItE92_suXhT4_FQFsP_TG0gA/embed"></iframe></figure><p>Isn’t that cool? Two great things to note about this:</p><ul><li>With <code>replace_condition</code>, <em>you don’t have to worry about how many partitions your data touches</em>: we take care of that stuff while ensuring all downstream materialized views get regenerated seamlessly.</li><li>The replace condition ALSO <strong>guarantees that any data not matching the condition doesn't get ingested</strong>. That means that you can use a CSV file that represents a superset of what you want to replace to just replace some parts. E.g., use a csv that contains all the transactions for a certain period even if you just want to replace a few of them.</li></ul><p>Does this sound familiar? Want to give it a try? <a href="mailto:hi@tinybird.co">Get in touch</a> or <a href="https://www.tinybird.co/survey">join our waiting list</a></p>
