diff --git a/create/src/schema.tmpl b/create/src/schema.tmpl
index 8637c48aab..7ccfd1c898 100644
--- a/create/src/schema.tmpl
+++ b/create/src/schema.tmpl
@@ -115,7 +115,8 @@ FIELD		|groupid	|t_id		|	|NULL		|0			|2|hstgrp	|		|RESTRICT
 FIELD		|templateid	|t_id		|	|NULL		|0			|3|group_prototype|group_prototypeid
 INDEX		|1		|hostid
 
-TABLE|group_discovery|groupid|ZBX_TEMPLATE
+TABLE|group_discovery|group_discoveryid|ZBX_TEMPLATE
+FIELD		|group_discoveryid|t_id		|	|NOT NULL	|0
 FIELD		|groupid	|t_id		|	|NOT NULL	|0			|1|hstgrp
 FIELD		|parent_group_prototypeid|t_id	|	|NOT NULL	|0			|2|group_prototype|group_prototypeid|RESTRICT
 FIELD		|name		|t_varchar(64)	|''	|NOT NULL	|ZBX_NODATA
diff --git a/src/zabbix_server/lld/lld_host.c b/src/zabbix_server/lld/lld_host.c
index 39871cdb70..6416df64e6 100644
--- a/src/zabbix_server/lld/lld_host.c
+++ b/src/zabbix_server/lld/lld_host.c
@@ -168,6 +168,7 @@ typedef struct
 {
 	zbx_uint64_t		groupid;
 	zbx_uint64_t		group_prototypeid;
+	zbx_uint64_t		group_discoveryid;
 	zbx_vector_ptr_t	hosts;
 	char			*name_proto;
 	char			*name;
@@ -873,13 +874,25 @@ static void	lld_groups_get(zbx_uint64_t parent_hostid, zbx_vector_ptr_t *groups)
 	zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __func__);
 
 	result = DBselect(
-			"select gd.groupid,gp.group_prototypeid,gd.name,gd.lastcheck,gd.ts_delete,g.name"
+			"select gd.groupid,gp.group_prototypeid,gd.name,gd.lastcheck,gd.ts_delete,g.name,gd.group_discoveryid"
 			" from group_prototype gp,group_discovery gd"
 				" join hstgrp g"
 					" on gd.groupid=g.groupid"
 			" where gp.group_prototypeid=gd.parent_group_prototypeid"
-				" and gp.hostid=" ZBX_FS_UI64,
-			parent_hostid);
+				" and gp.hostid in ("
+					"select hdd2.hostid from host_discovery hdd2"
+					" join items drule2 on drule2.itemid=hdd2.parent_itemid"
+					" where drule2.hostid in ("
+						"select hreal.hostid"
+						" from hosts hproto"
+						" join host_discovery hdd on hproto.hostid=hdd.hostid"
+						" join items drule on drule.itemid=hdd.parent_itemid"
+						" join hosts hreal on drule.hostid=hreal.hostid"
+						" where"
+							" hreal.status in (1,0) and"
+							" drule.flags=1 and"
+							" hproto.flags=2 and"
+							" hproto.hostid=" ZBX_FS_UI64 "))", parent_hostid);
 
 	while (NULL != (row = DBfetch(result)))
 	{
@@ -892,6 +905,7 @@ static void	lld_groups_get(zbx_uint64_t parent_hostid, zbx_vector_ptr_t *groups)
 		group->lastcheck = atoi(row[3]);
 		group->ts_delete = atoi(row[4]);
 		group->name = zbx_strdup(NULL, row[5]);
+		ZBX_STR2UINT64(group->group_discoveryid, row[6]);
 		group->name_orig = NULL;
 		group->flags = 0x00;
 
@@ -938,6 +952,7 @@ static zbx_lld_group_t	*lld_group_make(zbx_vector_ptr_t *groups, zbx_uint64_t gr
 
 	if (i == groups->values_num)	/* no group found */
 	{
+		zbx_uint64_t	groupid_master = 0;
 		/* trying to find an already existing group */
 
 		buffer = zbx_strdup(buffer, name_proto);
@@ -948,21 +963,18 @@ static zbx_lld_group_t	*lld_group_make(zbx_vector_ptr_t *groups, zbx_uint64_t gr
 		{
 			group = (zbx_lld_group_t *)groups->values[i];
 
-			if (group->group_prototypeid != group_prototypeid)
-				continue;
-
-			if (0 == (group->flags & ZBX_FLAG_LLD_GROUP_DISCOVERED))
-				continue;
-
 			if (0 == strcmp(group->name, buffer))
-				goto out;
+				break;
 		}
 
+		if (i != groups->values_num)
+			groupid_master = group->groupid;
+
 		/* otherwise create a new group */
 
 		group = (zbx_lld_group_t *)zbx_malloc(NULL, sizeof(zbx_lld_group_t));
 
-		group->groupid = 0;
+		group->groupid = groupid_master;
 		group->group_prototypeid = group_prototypeid;
 		zbx_vector_ptr_create(&group->hosts);
 		group->name_proto = NULL;
@@ -995,7 +1007,7 @@ static zbx_lld_group_t	*lld_group_make(zbx_vector_ptr_t *groups, zbx_uint64_t gr
 
 		group->flags |= ZBX_FLAG_LLD_GROUP_DISCOVERED;
 	}
-out:
+
 	zbx_free(buffer);
 
 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%p", __func__, (void *)group);
@@ -1434,7 +1446,7 @@ static void	lld_groups_save(zbx_vector_ptr_t *groups, const zbx_vector_ptr_t *gr
 	zbx_lld_group_t			*group;
 	const zbx_lld_group_prototype_t	*group_prototype;
 	zbx_lld_host_t			*host;
-	zbx_uint64_t			groupid = 0;
+	zbx_uint64_t			groupid = 0, group_discoveryid = 0;
 	char				*sql = NULL, *name_esc, *name_proto_esc;
 	size_t				sql_alloc = 0, sql_offset = 0;
 	zbx_db_insert_t			db_insert, db_insert_gdiscovery;
@@ -1473,11 +1485,12 @@ static void	lld_groups_save(zbx_vector_ptr_t *groups, const zbx_vector_ptr_t *gr
 	if (0 != new_group_prototype_ids.values_num)
 	{
 		groupid = DBget_maxid_num("hstgrp", new_group_prototype_ids.values_num);
+		group_discoveryid = DBget_maxid("group_discovery");
 
 		zbx_db_insert_prepare(&db_insert, "hstgrp", "groupid", "name", "flags", NULL);
 
-		zbx_db_insert_prepare(&db_insert_gdiscovery, "group_discovery", "groupid", "parent_group_prototypeid",
-				"name", NULL);
+		zbx_db_insert_prepare(&db_insert_gdiscovery, "group_discovery", "group_discoveryid", "groupid",
+				"parent_group_prototypeid", "name", NULL);
 
 		zbx_vector_ptr_create(&new_groups);
 	}
@@ -1506,7 +1519,7 @@ static void	lld_groups_save(zbx_vector_ptr_t *groups, const zbx_vector_ptr_t *gr
 			{
 				group_prototype = (zbx_lld_group_prototype_t *)group_prototypes->values[j];
 
-				zbx_db_insert_add_values(&db_insert_gdiscovery, group->groupid,
+				zbx_db_insert_add_values(&db_insert_gdiscovery, group_discoveryid++, group->groupid,
 						group->group_prototypeid, group_prototype->name);
 			}
 			else
@@ -1551,8 +1564,8 @@ static void	lld_groups_save(zbx_vector_ptr_t *groups, const zbx_vector_ptr_t *gr
 					zbx_snprintf_alloc(&sql, &sql_alloc, &sql_offset,
 							"update group_discovery"
 							" set name='%s'"
-							" where groupid=" ZBX_FS_UI64 ";\n",
-							name_proto_esc, group->groupid);
+							" where group_discoveryid=" ZBX_FS_UI64 ";\n",
+							name_proto_esc, group->group_discoveryid);
 
 					zbx_free(name_proto_esc);
 				}
@@ -2591,22 +2604,22 @@ static void	lld_groups_remove(const zbx_vector_ptr_t *groups, int lifetime, int
 
 			if (lastcheck > ts_delete)
 			{
-				zbx_vector_uint64_append(&del_groupids, group->groupid);
+				zbx_vector_uint64_append(&del_groupids, group->group_discoveryid);
 			}
 			else if (group->ts_delete != ts_delete)
 			{
 				zbx_snprintf_alloc(&sql, &sql_alloc, &sql_offset,
 						"update group_discovery"
 						" set ts_delete=%d"
-						" where groupid=" ZBX_FS_UI64 ";\n",
-						ts_delete, group->groupid);
+						" where group_discoveryid=" ZBX_FS_UI64 ";\n",
+						ts_delete, group->group_discoveryid);
 			}
 		}
 		else
 		{
-			zbx_vector_uint64_append(&lc_groupids, group->groupid);
+			zbx_vector_uint64_append(&lc_groupids, group->group_discoveryid);
 			if (0 != group->ts_delete)
-				zbx_vector_uint64_append(&ts_groupids, group->groupid);
+				zbx_vector_uint64_append(&ts_groupids, group->group_discoveryid);
 		}
 	}
 
@@ -2614,7 +2627,7 @@ static void	lld_groups_remove(const zbx_vector_ptr_t *groups, int lifetime, int
 	{
 		zbx_snprintf_alloc(&sql, &sql_alloc, &sql_offset, "update group_discovery set lastcheck=%d where",
 				lastcheck);
-		DBadd_condition_alloc(&sql, &sql_alloc, &sql_offset, "groupid",
+		DBadd_condition_alloc(&sql, &sql_alloc, &sql_offset, "group_discoveryid",
 				lc_groupids.values, lc_groupids.values_num);
 		zbx_strcpy_alloc(&sql, &sql_alloc, &sql_offset, ";\n");
 	}
@@ -2622,7 +2635,7 @@ static void	lld_groups_remove(const zbx_vector_ptr_t *groups, int lifetime, int
 	if (0 != ts_groupids.values_num)
 	{
 		zbx_strcpy_alloc(&sql, &sql_alloc, &sql_offset, "update group_discovery set ts_delete=0 where");
-		DBadd_condition_alloc(&sql, &sql_alloc, &sql_offset, "groupid",
+		DBadd_condition_alloc(&sql, &sql_alloc, &sql_offset, "group_discoveryid",
 				ts_groupids.values, ts_groupids.values_num);
 		zbx_strcpy_alloc(&sql, &sql_alloc, &sql_offset, ";\n");
 	}
@@ -3203,11 +3216,13 @@ void	lld_update_hosts(zbx_uint64_t lld_ruleid, const zbx_vector_ptr_t *lld_rows,
 		lld_interfaces_validate(&hosts, error);
 
 		lld_hostgroups_make(&groupids, &hosts, &groups, &del_hostgroupids);
+
 		lld_templates_make(parent_hostid, &hosts);
 
 		lld_hostmacros_make(&hostmacros, &hosts, lld_macro_paths);
 
 		lld_groups_save(&groups, &group_prototypes);
+
 		lld_hosts_save(parent_hostid, &hosts, host_proto, proxy_hostid, ipmi_authtype, ipmi_privilege,
 				ipmi_username, ipmi_password, status, inventory_mode, tls_connect, tls_accept,
 				tls_issuer, tls_subject, tls_psk_identity, tls_psk, &del_hostgroupids);
