@cryptotaxi247 / netdata-1 / commits / e3de1518c

Add index to ACLK table to improve update statements (#15890)

* Add index to improve update statements * Add index to improve select statements * Improve update statement

Stelios Fragkakis committed Aug 30, 2023 at 15:29 UTC e3de1518c69b6e8fe40aa6b023c6932a385d08ed
3 files changed +14 -1
database/sqlite/sqlite_aclk.c
+10
@@ -499,6 +499,16 @@ void sql_create_aclk_table(RRDHOST *host __maybe_unused, uuid_t *host_uuid __may
499 rc = db_execute(db_meta, sql);
500 if (unlikely(rc))
501 error_report("Failed to create ACLK alert table index for host %s", host ? string2str(host->hostname) : host_guid);
502 +
503 + snprintfz(sql, ACLK_SYNC_QUERY_SIZE -1, INDEX_ACLK_ALERT1, uuid_str, uuid_str);
504 + rc = db_execute(db_meta, sql);
505 + if (unlikely(rc))
506 + error_report("Failed to create ACLK alert table index 1 for host %s", host ? string2str(host->hostname) : host_guid);
507 +
508 + snprintfz(sql, ACLK_SYNC_QUERY_SIZE -1, INDEX_ACLK_ALERT2, uuid_str, uuid_str);
509 + rc = db_execute(db_meta, sql);
510 + if (unlikely(rc))
511 + error_report("Failed to create ACLK alert table index 2 for host %s", host ? string2str(host->hostname) : host_guid);
512 }
513 if (likely(host) && unlikely(host->aclk_sync_host_config))
514 return;
database/sqlite/sqlite_aclk.h
+3
@@ -46,6 +46,9 @@ static inline int claimed()
46 "unique(alert_unique_id));"
47
48 #define INDEX_ACLK_ALERT "CREATE INDEX IF NOT EXISTS aclk_alert_index_%s ON aclk_alert_%s (alert_unique_id);"
49 +#define INDEX_ACLK_ALERT1 "CREATE INDEX IF NOT EXISTS aclk_alert_index1_%s ON aclk_alert_%s (filtered_alert_unique_id);"
50 +#define INDEX_ACLK_ALERT2 "CREATE INDEX IF NOT EXISTS aclk_alert_index2_%s ON aclk_alert_%s (date_submitted);"
51 +
52 enum aclk_database_opcode {
53 ACLK_DATABASE_NOOP = 0,
54
database/sqlite/sqlite_aclk_alert.c
+1 -1
@@ -407,7 +407,7 @@ void aclk_push_alert_event(struct aclk_sync_host_config *wc)
407 if (first_sequence_id) {
408 buffer_flush(sql);
409 buffer_sprintf(sql, "UPDATE aclk_alert_%s SET date_submitted=unixepoch() "
410 - "WHERE date_submitted IS NULL AND sequence_id BETWEEN %" PRIu64 " AND %" PRIu64 ";",
410 + "WHERE +date_submitted IS NULL AND sequence_id BETWEEN %" PRIu64 " AND %" PRIu64 ";",
411 wc->uuid_str, first_sequence_id, last_sequence_id);
412
413 if (unlikely(db_execute(db_meta, buffer_tostring(sql))))