IMD SolutionComputing LibraryShop main store

How-to

Downsampling old Ignition historian data to one-minute averages

Edge computing and IIoT Published 7 min read

Short answer

Ignition has no built-in expiry with aggregation, so you downsample stored history yourself. Two workable patterns: a scheduled database job that keeps the first row of each 60,000 ms bucket in the old sqlt_data partitions, or a monthly gateway script that reads averages or min/max with system.tag.queryTagHistory and then deletes the raw rows. Both are irreversible deletes, so run them on a verified backup and tighten per-tag deadbands first.

On this page
  1. What the historian actually stores
  2. Match the aggregation to the tag type before you delete
  3. Back up, then rehearse on the copy
  4. Approach 1: a scheduled SQL job that keeps the first sample of each minute
  5. Approach 2: a gateway script that computes and keeps aggregates
  6. Compressing 30 days of one-second history on a single tag
  7. Choosing the approach
  8. Schedule it and verify
  9. Questions asked

Yes — history already in the database can be compressed to one point per minute, but Ignition will not do it for you. The Tag Historian module has no expiry-with-aggregation function, and its pre-processed partitions run the other way: they add storage so wide-range aggregate queries run faster, and none of the fine-grained data is discarded. You downsample with your own monthly job, in one of two shapes: a scheduled database procedure that keeps the first row of every 60,000 ms bucket in the old sqlt_data_ partitions, or a gateway scheduled script that reads minute averages or min/max with system.tag.queryTagHistory, writes them back, then deletes the raw rows. Take the SQL job for continuous analog tags and the script with MinMax for sporadic tags such as a flow meter that runs 5% of the time. Both delete irreversibly, so the backup comes first.

What the historian actually stores #

The module writes to auto-partitioned tables in the provider's database, not one big table:

  • sqlth_te — one row per historized tag, keyed on tagpath and dtype
  • sqlth_partitions — one row per partition: pname, start_time, end_time
  • sqlt_data_1, sqlt_data_2, and so on — the values for one partition each: tagid, t_stamp, floatvalue, intvalue, stringvalue

t_stamp is epoch milliseconds stored as a BIGINT, so bucketing is integer math on t_stamp. Resolve partition names and their date ranges from sqlth_partitions on every run: never hardcode a name like sqlt_data_7, because partition numbering differs per installation and rolls forward as data accumulates. If you want to read the tables by hand before touching them, how t_stamp and sqlt_data partitions map to readable time ranges is the reference for the conversion.

Built-in pruning drops whole partitions once they pass retention, and it never aggregates anything inside them, which is why setting historian pruning and retention for Ignition tag history solves only the age limit — your 2-year policy — and not the density of the first month.

Match the aggregation to the tag type before you delete #

  • Continuous analog tags — temperature, pressure, level: keeping the first sample of each minute preserves real recorded values, just sparser; a computed Average is equally fine for trending.
  • Sporadic tags — a flow meter unused 95% of the time: an average across a mostly-zero minute buries the batch peak, and the peak is what you review later. Use MinMax, or keep every nonzero sample.
  • Booleans and counters: keep the first (or last) sample plus every state change; an average of 0 and 1 means nothing.

Tighten deadbands first. A deadband defined per equipment type stores far more rows than a tag needs: a level meter on a 30 ft tank and one on a 3 ft tub move very different distances, and a tag scaled in inches crosses any fixed deadband twelve times more often than one in feet. A row that is never stored needs no deleting, so this is the cheapest compression you will get.

Back up, then rehearse on the copy #

One rule covers both approaches: the deletes are irreversible, so take a full backup of the historian tables, restore it as a working copy, and run one month's job against that copy before production. Keep the backup until the verification at the end passes. Seeding a large Ignition historian database for replication covers the same restore-and-seed path you need to stand that copy up.

Approach 1: a scheduled SQL job that keeps the first sample of each minute #

This is the tool for continuous analog tags. It deletes rows; it does not compute anything.

  1. Resolve the partitions whose end_time falls before your precision window (30 days ago, expressed in epoch milliseconds) from sqlth_partitions.
  2. Count the rows you are about to touch — sqlt_data_7 below is an example name resolved in step 1:
SELECT COUNT(*) FROM dbo.sqlt_data_7;  -- before
  1. Delete every row that is not the first of its minute, per tag:
WITH ranked AS (
    SELECT tagid, t_stamp,
           ROW_NUMBER() OVER (
               PARTITION BY tagid, t_stamp / 60000  -- 60000 ms = one minute
               ORDER BY t_stamp ASC) AS rn
    FROM dbo.sqlt_data_7
)
DELETE FROM ranked WHERE rn > 1;

t_stamp / 60000 integer-divides epoch milliseconds into 60,000 ms buckets, and row 1 of each bucket survives. On MySQL or MariaDB use t_stamp DIV 60000 and drive the delete through a joined derived table. 4. Count again. For true one-second data the after-count is the before-count divided by 60. 5. Schedule it monthly, off-hours: a SQL Server Agent job, a MySQL event, or pg_cron.

Approach 2: a gateway script that computes and keeps aggregates #

This is the tool for sporadic and mixed tag sets, because each tag gets its own aggregation mode.

  1. Enumerate the tags with history enabled and assign each a mode by type: Average for continuous, MinMax for sporadic.
  2. Read one month of data older than 30 days:
# Illustrative scheduled gateway script (Jython, Ignition 8.1)
from datetime import datetime, timedelta

DB_NAME = 'HistorianDB'          # illustrative datasource name
BUCKET_SEC = 60                  # one point per minute
AGE_DAYS = 30                    # compress only data older than this
AGG = 'MinMax'                   # 'Average' for continuous tags

end = datetime.now() - timedelta(days=AGE_DAYS)
start = end - timedelta(days=31)  # one month per run

ds = system.tag.queryTagHistory(
    paths=['[default]Plant/FT_101'],   # illustrative tag path
    startDate=start, endDate=end,
    returnSize=-1,
    aggregationMode=AGG,
    intervalSeconds=BUCKET_SEC)

The aggregation mode applies exactly when stored data is denser than the request — which is the whole point of this job. MinMax returns both the minimum and the maximum of each minute. 3. Resolve the tag's tagid from sqlth_te by tagpath, and the covering partitions from sqlth_partitions by start_time and end_time. 4. Delete the raw rows in that window with system.db.runPrepUpdate — after step 2, so the aggregates are already computed. 5. Insert one row per returned bucket into the same covering partition: the computed value in floatvalue for floating dtypes or intvalue otherwise, with the returned timestamp in t_stamp. 6. Rehearse first: inserting rows into sqlt_data_ tables is outside the module's own write path, so prove one tag end to end on the restored copy before scheduling.

Compressing 30 days of one-second history on a single tag #

Inputs, each with its source:

  • Recording rate: 1 stored row per second per tag — the recording rate described in the field question, and the ceiling, because the deadband only stores changes.
  • Precision window: 30 days of full resolution, then one point per minute for the rest of a 2-year retention.
  • Bucket: 60 s = 60,000 ms.
  • Time unit of the tables: t_stamp in epoch milliseconds.

The rule in words: kept rows equal the window length divided by the bucket length; deleted rows equal stored rows minus kept rows; the reduction factor equals the stored rows per minute.

The arithmetic:

  • 86,400 s/day × 30 days = 2,592,000 s, so at one row per second the month holds 2,592,000 stored rows.
  • 2,592,000 ÷ 60 = 43,200 kept rows, one per minute.
  • 2,592,000 − 43,200 = 2,548,800 rows deleted.
  • 2,548,800 ÷ 2,592,000 = 0.983, so 98.3% of the month goes.
  • Beyond the precision window, the 2-year policy keeps 43,200 rows each month × 23 months = 993,600 rows per tag.

Decision: count before you delete. If a continuous tag's 30-day partition holds more than 43,200 rows, run the SQL job — at one-second density you reclaim 60 to 1. If it holds 43,200 or fewer, that tag already stores a point per minute or less, and the fix is its deadband, not its data. For N continuous tags at that density, the monthly job deletes N × 2,548,800 rows.

Choosing the approach #

SQL job, first-in-bucket Gateway script, queryTagHistory
What survives real recorded samples computed Average, MinMax or Last
Continuous analog tags the right tool works, with Average
Sporadic tags the wrong tool the right tool, with MinMax
Where it runs database engine, off-hours gateway thread, watch memory
Extra machinery none tag enumeration and write-back inserts

Run the SQL job plant-wide on the monthly schedule, and reserve the script for the sporadic set where the aggregation mode has to vary per tag.

Schedule it and verify #

Run monthly, with each pass covering the window 30 to 60 days old, so the live month is never touched. Verify three things per run: the before-count and after-count per partition, one downsampled day plotted at a one-minute interval, and for the script path that a test tag's minimum and maximum over an old day survived. Exporting a historian trend CSV at one-minute intervals gives you the artifact to file with the change record. Minutes with no stored sample surface as None in exports; resolving None values in queryTagHistory CSV exports covers that before you go chasing the historian for it.

Questions asked

Do Ignition's pre-processed history partitions do this compression for me?

No. Pre-processed partitions add aggregate tables so wide-range queries run faster, and they keep every fine-grained sample, so total storage goes up slightly. They are the opposite of expiry with aggregation. Compressing data that is already stored stays your own monthly job, by script or by SQL.

How many rows does one month of one-second data shed?

At a true one row per second, a 30-day month holds 2,592,000 rows per tag and keeps 43,200 of them, deleting 2,548,800 — a 60-to-1 reduction. Deadbands usually store fewer rows than the scan rate, so count the partition first; your real reduction equals the average stored rows per minute.

Which aggregation should a flow meter that only runs during batches use?

MinMax, or keep every nonzero sample. A 60-second average across a meter that sits at zero 95% of the time flattens the batch peak, and that peak is exactly what you review after an upset. First-in-bucket deletion has the same problem, which is why sporadic tags belong to the script, not the SQL job.

Do I need to resolve partition names on every run?

Yes. Partition table names come from sqlth_partitions (pname with start_time and end_time in epoch milliseconds) and they differ per installation and partition length. A job with a hardcoded sqlt_data_7-style name breaks as soon as the historian rolls a new partition, so resolve pname by time window at the start of each run.

Can the gateway script write averages into the same tables the historian writes?

You can insert tagid, t_stamp and value rows into the covering sqlt_data_ partition, but that insert is outside the module's own write path. Prove one tag end to end on the restored backup copy and check its trend before you schedule the job, and put the computed value in floatvalue or intvalue to match the tag's dtype.

Keep reading

  • How-to

    Identifying the source gateway in a shared historian database

    The history does not land in one table. Each gateway connected to the shared database registers at least two rows in sqlth_drv, and the number in the sqlt_data_ table prefix is one of those row IDs, so the source is already recorded. Join sqlth_te to sqlth_drv for the tag-to-gateway map and filter the visualization on a site-code tag-path prefix, not on scan class names.

    Edge computing and IIoT

  • Comparison

    MQTT Recorder or Tag Historian for plant data

    Tag Historian stores timestamped values for individual tags in a SQL database, built for trends and time-based queries. MQTT Recorder receives a block of data from an MQTT server and inserts it as one row in a database table, built for completed field-generated records such as tickets and batch results. Choose by the unit of information, then put buffering where the link fails.

    Edge computing and IIoT

  • How-to

    Setting historian pruning and retention for Ignition tag history

    Prune tag history on the history provider, not with the transaction group's delete setting: in the Gateway's tag history configuration, enable data pruning and set a retention period in days. At 250 MB per day of growth, 30 days of retention holds about 7.5 GB and a full year about 91 GB. Deadband and Max Time Between Samples decide most of the daily rate, and QC records from transaction groups keep growing on a separate budget.

    Edge computing and IIoT

Local Views service or remote Views for multi-historian Axiom