Vision DocumentationDataset
DocumentationDataset
Dataset

Types of SQL updates

Last changed in 03/04/2024

Vision allows two main options to update or refresh the data of a dataset whose data source is a relational database (SQL): full or incremental update. Furthermore, the incremental update is subdivided into several options depending on the type of incremental that is desired or can be applied.

Types of updates from SQL DB

Complete Update

This type of update is very simple: the previous data is discarded from a dataset and loaded again from the result of the query execution. For security reasons, this update is carried out as follows:

  • A process is launched that loads the data into an auxiliary dataset hidden from users.
  • If the process is successful, i.e. the data returned by the query is loaded without problems, the dataset is modified to point to the newly updated data and the old data is deleted. This prevents an interrupted load from leaving the dataset in a state of data corruption, the result of the update being OK.
  • If there is an error in the auxiliary process it is aborted and/or undone, and the dataset keeps its original data intact. The status of the last update is therefore ERROR.

Incremental Update

An incremental update leaves a part of the dataset data intact, and loads new data. Therefore, it is recommended when the volume of data to be moved between DB and Vision is high.

In order to perform an incremental update, 3 elements are required:

  1. An "incremental" field of type date or numeric.
  2. A partition criterion, for example "greater than".
  3. A reference value equal to the maximum value of the incremental field in the previous data load, and which is therefore modified after each refresh.

This criterion has to be included in the SQL query that refreshes the data, so that all the records of the SQL query that fulfill that the incremental field is greater than the reference value are inserted in the dataset without touching the data already present, while when the condition is not fulfilled the query itself excludes such data.

The incremental update, however, can be modified to differentiate more delicate particular cases, since in reality the incremental criterion is not always met in 100% of the cases. Therefore, depending on the nature of the data recording and updating process itself, there are different options:

  • Incremental Normal: leaves present data intact and loads new content.
  • Incremental Window: leaves a part of the present data intact, deletes another part according to a temporary window, and loads new content from the beginning of the window.
  • Incremental Last Changes: leaves a part of the present data intact, updates another part of it, and also loads new content.

In addition, and as a complement to the incremental update, it is also possible to truncate the data history. The idea behind an incremental update is that new data is added to update the current data and old data that remains in the distant past is deleted.

Normal Incremental

The Normal incremental update type leaves existing data intact and loads new content, records from a certain value of the "incremental" field.

Configuration of a Normal incremental

  1. Incremental field: is the column of a table or the alias of a field present in the SQL query (and therefore the "DB alias" or Fieldname of the field in the dataset) to be used in the incremental condition, usually an auto-incremental numeric column or a date.
  2. Variable: is the variable used internally by Vision that takes the maximum value of the incremental field in the dataset before the update ($last_value$). This value is not modified, unless a more complex incremental condition needs to be implemented, in which case the Vision administrator must be contacted to evaluate the case.
  3. The incremental condition is added to the SQL statement by inserting in the where part of the query a special text: $condition$. Vision understands this text as a variable, when executing the query, it first resolves the value of $last_value$ and replaces $condition$ by campo_incremental > last_value . With this in mind, the user has to write a valid SQL query, inserting the $condition$ variable correctly in the where clause of the query.

Valid SQL syntax using an alias in the incremental condition

Incremental Window

The incremental update type Window is an extension of the normal incremental, as it inserts data starting from a value prior to the maximum value of the incremental field in the dataset. This update is only allowed using an incremental field of type date: the "window" is the interval that the user indicates by configuration. All dataset data within the window are deleted, and the SQL query is launched with a condition that results in data starting from the initial date of the window, which includes records with previously loaded dates and with new dates not loaded (like the normal incremental). Records in the dataset prior to the start of the window are left untouched.

This type of update provides an answer to the use case in which, without wanting to make a complete update, it is necessary to load new records and also modify some recent records, without going into the detail of which ones they are. Situations like this occur frequently in practice: accounting data in an open fiscal year that are corrected, sales amounts that are modified due to discounts applied, etc.

The configuration of this type of update is divided into 2 parts:

  1. The incremental part proceeds in a similar way to the normal incremental: you need to indicate the incremental field and insert in the where part of the SQL query the text $condition$, which will be understood as a variable and Vision will replace it with the expression campo_incremental >= window_start_value.
  2. It is indicated how to determine the starting date of the window, which will be a date prior to the current date window_start_value = today - X "time units". There are 3 different options to determine X and time units:
    • Current period: the window is a dynamic period covering the "current" time interval corresponding to the selected unit, which can take the values years, months, weeks, days, hours or minutes.For example, if the unit "years" is chosen the start of the interval begins on January 1 of the current year, if "month" is chosen on the 1st of the current month, etc.
    • Exact interval: the window is a fixed time interval as indicated by the number of periods and the time unit, e.g. if "2 weeks" is specified and the update is executed on a "Thursday" the window starts on Thursday 2 weeks ago.
    • Full interval: this option is a combination of the current period and the exact interval, as it "rounds" the start of the window to the full interval of the selected unit, e.g. if "1 year" is specified the window starts on January 1st of the year before the current one (it goes one year into the past and rounds according to the time unit), if "0 months" is specified the interval starts on the 1st of the current month, if "2 weeks" is specified the interval starts on Sunday of 2 weeks ago (the week in Vision starts on Sunday and ends on Saturday).

Configuration of an incremental Window

Incremental Last Changes

The Incremental Last Changes incremental update type leaves a part of the present data intact, updates (or deletes) another part of it, and also loads new content. The configuration of this type of update is divided into 3 parts:

  1. Incremental field: it is the column of a table or the alias of a field present in the SQL query (and therefore the "DB alias or Fieldname" of the field in the dataset) to be used in the incremental condition, usually the creation/update date of each record.
  2. A primary key (PK) of the record needs to be defined. In the corresponding text box, the names of the fields that form this PK are written (press Enter to enable each written field). In the update process, for each record that arrives as a result of the query, the equivalent record in the dataset is searched by comparing by the PK: if it is found it is updated by the new one, and if it is not found it is inserted as it is since it is a "new" record.
  3. The incremental condition is added to the SQL statement by inserting in the where part of the query a special text: $condition$. Vision understands this text as a variable, when executing the query, it first resolves the value of $last_value$ and replaces $condition$ by campo_incremental > last_value .With this in mind, the user has to write a valid SQL query, inserting the $condition$ variable correctly in the where clause of the query.
  4. The process allows not only to update data but also to delete records. To do this, a field must be included in the SQL query that must be called "_delete": when the value of the field is "true" and the PK of the record matches the PK of a row in the dataset, it is deleted. However, this part is not mandatory: if it is not included, no record is deleted.

Configuration of an incremental Last changes

It is important to note that this type of update requires a certain logic in the management and structure of the information in the database:

  • The SQL query should preferably include only "new" records or records to be modified. In that sense it is an "almost" incremental process, that is to say, it is recommended not to include information of already existing records that have not changed, since it would be transferring unnecessary information between the source database and Vision, and in Vision would be making comparisons by PK useless, since the content remains the same as it was. In order to be able to use this type of update, it is usual to have the date when the information was recorded (creation or update), and therefore the incremental field will be that "control" date, and not the date associated with another event in the dataset (a sale, an invoice, etc.).
  • The deletion process is performed in Vision from the information that arrives in the SQL query. In this sense, a logical deletion of the records must be performed in the source database, i.e. marking the records as deleted instead of a physical deletion, otherwise these records would not be part of the SQL query result! If it is not possible to avoid the physical deletion, a possible solution is, prior to the deletion, to copy the records to be deleted to an auxiliary table. In this way, the SQL query can already include those records, for example by means of a join to the auxiliary table.

Historical Data

Datasets with incremental update allow you to set up historical data deletion. In many cases, data from past periods do not add much value to the dashboards, but can have a negative impact on the performance of the visualizations, since they can considerably increase the volume of the datasets that the widgets use as data source.

To perform this deletion, the time window of the records to be kept is defined. Once this option is configured, Vision takes care of automatically deleting the data prior to that window: at each data update, a second process is added to the insertion that proceeds to delete those "old" records.

The configuration of the deletion of historical data consists of 2 parts:

  1. Field for data deletion: this is the name of the field in the dataset, and it must be of date type to be able to generate the temporary window.
  2. It indicates how to determine the starting date of the window, which will be a date prior to the current date window_start_value = today - X "time units". The time unit can be years, months, weeks, days, hours or minutes, while the number of periods X must be an integer greater than or equal to 0. In addition, the date is rounded according to the time unit, e.g. "0 years" retains data from the current year (deletes records dated prior to January 1 of the current year), "2 months" retains data from the current and previous 2 months (deletes data dated prior to the 1st of 2 months ago), etc.

Configuring the deletion of historical data

Bookmark or share this article
Esta página foi útil?
Obrigado pelo seu voto.

login para deixar a sua opinião.

Obrigado pelo seu feedback. Iremos analisá-lo para continuarmos a melhorar!
Artigos Relacionados
Tipos de atualização SQL Tipos de atualização SQL