diff --git a/src/libs/zbxcachehistory/cachehistory.c b/src/libs/zbxcachehistory/cachehistory.c
index ca0d71394c3..2a4b4cb48a4 100644
--- a/src/libs/zbxcachehistory/cachehistory.c
+++ b/src/libs/zbxcachehistory/cachehistory.c
@@ -401,18 +401,21 @@ static void	dc_insert_trends_in_db(ZBX_DC_TREND *trends, int trends_num, int ups
 	zbx_db_insert_prepare(&db_insert, table_name, "itemid", "clock", "num", "value_min", "value_avg",
 			"value_max", (char *)NULL);
 
-#ifdef HAVE_POSTGRESQL
 	if (0 != upserts_num)
 	{
+#ifdef HAVE_POSTGRESQL
 		zbx_db_insert_clause(&db_insert, " on conflict (itemid,clock) do update set num=EXCLUDED.num,"
 				"value_min=EXCLUDED.value_min,"
 				"value_avg=EXCLUDED.value_avg,"
 				"value_max=EXCLUDED.value_max");
-	}
 #else
-	ZBX_UNUSED(upserts_num);
+		zbx_db_insert_clause(&db_insert, " on duplicate key update"
+				" num=VALUES(num),"
+				"value_min=VALUES(value_min),"
+				"value_avg=VALUES(value_avg),"
+				"value_max=VALUES(value_max)");
 #endif
-
+	}
 	for (i = 0; i < trends_num; i++)
 	{
 		trend = &trends[i];
@@ -541,16 +544,6 @@ static void	dc_trends_update_float(ZBX_DC_TREND *trend, zbx_db_row_t row, int nu
 	trend->num += num;
 }
 
-static void	db_trends_update_float(ZBX_DC_TREND *trend, size_t *sql_offset)
-{
-	zbx_snprintf_alloc(&sql, &sql_alloc, sql_offset, "update trends set"
-			" num=%d,value_min=" ZBX_FS_DBL64_SQL ",value_avg=" ZBX_FS_DBL64_SQL
-			",value_max=" ZBX_FS_DBL64_SQL
-			" where itemid=" ZBX_FS_UI64 " and clock=%d;\n",
-			trend->num, trend->value_min.dbl, trend->value_avg.dbl, trend->value_max.dbl,
-			trend->itemid, trend->clock);
-}
-
 /******************************************************************************
  *                                                                            *
  * Purpose: helper function for DCflush trends                                *
@@ -577,32 +570,13 @@ static void	dc_trends_update_uint(ZBX_DC_TREND *trend, zbx_db_row_t row, int num
 	zbx_uinc128_128(&trend->value_avg.ui64, &avg);
 }
 
-static void	db_trends_update_uint(ZBX_DC_TREND *trend, size_t *sql_offset)
-{
-	zbx_uint128_t	avg;
-
-	zbx_udiv128_64(&avg, &trend->value_avg.ui64, trend->num);
-
-	zbx_snprintf_alloc(&sql, &sql_alloc, sql_offset,
-			"update trends_uint set num=%d,value_min=" ZBX_FS_UI64 ",value_avg="
-			ZBX_FS_UI64 ",value_max=" ZBX_FS_UI64 " where itemid=" ZBX_FS_UI64
-			" and clock=%d;\n",
-			trend->num,
-			trend->value_min.ui64,
-			avg.lo,
-			trend->value_max.ui64,
-			trend->itemid,
-			trend->clock);
-}
-
 /******************************************************************************
  *                                                                            *
  * Purpose: helper function for DCflush trends                                *
  *                                                                            *
  ******************************************************************************/
 static void	dc_trends_fetch_and_update(ZBX_DC_TREND *trends, int trends_num, zbx_uint64_t *itemids,
-		int itemids_num, int *inserts_num, int *upserts_num, unsigned char value_type, const char *table_name,
-		int clock)
+		int itemids_num, int *upserts_num, unsigned char value_type, const char *table_name, int clock)
 {
 	int		i, num;
 	zbx_db_result_t	result;
@@ -610,12 +584,7 @@ static void	dc_trends_fetch_and_update(ZBX_DC_TREND *trends, int trends_num, zbx
 	zbx_uint64_t	itemid;
 	ZBX_DC_TREND	*trend;
 	size_t		sql_offset;
-#ifdef HAVE_POSTGRESQL
-	int		upsert = 0;
 
-	if (0 != zbx_tsdb_get_version())
-		upsert = 1;
-#endif
 	sql_offset = 0;
 	zbx_snprintf_alloc(&sql, &sql_alloc, &sql_offset,
 			"select itemid,num,value_min,value_avg,value_max"
@@ -625,14 +594,7 @@ static void	dc_trends_fetch_and_update(ZBX_DC_TREND *trends, int trends_num, zbx
 
 	zbx_db_add_condition_alloc(&sql, &sql_alloc, &sql_offset, "itemid", itemids, itemids_num);
 
-#ifdef HAVE_POSTGRESQL
-	if (1 == upsert)
-		result = zbx_db_select("%s", sql);
-	else
-		result = zbx_db_select("%s order by itemid,clock", sql);
-#else
-	result = zbx_db_select("%s order by itemid,clock", sql);
-#endif
+	result = zbx_db_select("%s", sql);
 
 	sql_offset = 0;
 
@@ -666,30 +628,10 @@ static void	dc_trends_fetch_and_update(ZBX_DC_TREND *trends, int trends_num, zbx
 		else
 			dc_trends_update_uint(trend, row, num);
 
-#ifdef HAVE_POSTGRESQL
-		if (1 == upsert)
-		{
-			(*upserts_num)++;
-			continue;
-		}
-#else
-		ZBX_UNUSED(upserts_num);
-#endif
-		if (value_type == ITEM_VALUE_TYPE_FLOAT)
-			db_trends_update_float(trend, &sql_offset);
-		else
-			db_trends_update_uint(trend, &sql_offset);
-
-		trend->itemid = 0;
-
-		--*inserts_num;
-
-		zbx_db_execute_overflowed_sql(&sql, &sql_alloc, &sql_offset);
+		(*upserts_num)++;
 	}
 
 	zbx_db_free_result(result);
-
-	(void)zbx_db_flush_overflowed_sql(sql, sql_offset);
 }
 
 /******************************************************************************
@@ -771,8 +713,8 @@ void	zbx_db_flush_trends(ZBX_DC_TREND *trends, int *trends_num, zbx_vector_uint6
 
 	if (0 != itemids_num)
 	{
-		dc_trends_fetch_and_update(trends, trends_to, itemids, itemids_num, &inserts_num, &upserts_num,
-				value_type, table_name, clock);
+		dc_trends_fetch_and_update(trends, trends_to, itemids, itemids_num, &upserts_num, value_type,
+				table_name, clock);
 	}
 
 	zbx_free(itemids);
