Index: httpdetails.php
===================================================================
--- httpdetails.php	(revision 20279)
+++ httpdetails.php	(working copy)
@@ -155,9 +155,9 @@
 		}
 
 		$itemids = array();
-		$sql = 'SELECT i.lastvalue, i.lastclock, i.value_type, i.valuemapid, i.units, i.itemid, hi.type as httpitem_type '.
-				' FROM items i, httpstepitem hi '.
-				' WHERE hi.itemid=i.itemid '.
+		$sql = 'SELECT its.lastvalue, its.lastclock, i.value_type, i.valuemapid, i.units, i.itemid, hi.type as httpitem_type '.
+				' FROM items i, items_state its, httpstepitem hi '.
+				' WHERE hi.itemid=i.itemid AND its.itemid = i.itemid'.
 					' AND hi.httpstepid='.$httpstep_data['httpstepid'];
 		$db_items = DBselect($sql);
 		while($item_data = DBfetch($db_items)){
Index: include/triggers.inc.php
===================================================================
--- include/triggers.inc.php	(revision 20279)
+++ include/triggers.inc.php	(working copy)
@@ -1470,9 +1492,9 @@
 					$functionid = trigger_get_N_functionid($row['expression'], $i ? $i : 1);
 
 					if(isset($functionid)){
-						$sql = 'SELECT i.lastvalue, i.lastclock, i.value_type, i.itemid, i.valuemapid, i.units '.
-								' FROM items i, functions f '.
-								' WHERE i.itemid=f.itemid '.
+						$sql = 'SELECT its.lastvalue as lastvalue, its.lastclock, i.value_type, i.itemid, i.valuemapid, i.units '.
+								' FROM items i, items_state its, functions f '.
+								' WHERE i.itemid=f.itemid AND its.itemid = i.itemid'.
 									' AND f.functionid='.$functionid;
 						$row2=DBfetch(DBselect($sql));
 						$description = str_replace($macro, format_lastvalue($row2), $description);
Index: include/schema.inc.php
===================================================================
--- include/schema.inc.php	(revision 20279)
+++ include/schema.inc.php	(working copy)
@@ -3546,21 +3546,6 @@
 				'length' => 10,
 				'default' => '365',
 			),
-			'lastvalue' => array(
-				'null' => true,
-				'type' => DB::FIELD_TYPE_CHAR,
-				'length' => 255,
-			),
-			'lastclock' => array(
-				'null' => true,
-				'type' => DB::FIELD_TYPE_INT,
-				'length' => 10,
-			),
-			'prevvalue' => array(
-				'null' => true,
-				'type' => DB::FIELD_TYPE_CHAR,
-				'length' => 255,
-			),
 			'status' => array(
 				'null' => false,
 				'type' => DB::FIELD_TYPE_INT,
@@ -3597,11 +3582,6 @@
 				'length' => 10,
 				'default' => '0',
 			),
-			'prevorgvalue' => array(
-				'null' => true,
-				'type' => DB::FIELD_TYPE_CHAR,
-				'length' => 255,
-			),
 			'snmpv3_securityname' => array(
 				'null' => false,
 				'type' => DB::FIELD_TYPE_CHAR,
@@ -3632,18 +3612,6 @@
 				'length' => 255,
 				'default' => '1',
 			),
-			'error' => array(
-				'null' => false,
-				'type' => DB::FIELD_TYPE_CHAR,
-				'length' => 128,
-				'default' => '',
-			),
-			'lastlogsize' => array(
-				'null' => false,
-				'type' => DB::FIELD_TYPE_INT,
-				'length' => 10,
-				'default' => '0',
-			),
 			'logtimefmt' => array(
 				'null' => false,
 				'type' => DB::FIELD_TYPE_CHAR,
@@ -3717,17 +3685,6 @@
 				'length' => 64,
 				'default' => '',
 			),
-			'mtime' => array(
-				'null' => false,
-				'type' => DB::FIELD_TYPE_INT,
-				'length' => 10,
-				'default' => '0',
-			),
-			'lastns' => array(
-				'null' => true,
-				'type' => DB::FIELD_TYPE_INT,
-				'length' => 10,
-			),
 			'flags' => array(
 				'null' => false,
 				'type' => DB::FIELD_TYPE_INT,
@@ -3766,6 +3723,60 @@
 			),
 		),
 	),
+	'items_state' => array(
+		'key' => 'itemid',
+		'fields' => array(
+			'itemid' => array(
+				'null' => false,
+				'type' => DB::FIELD_TYPE_ID,
+				'length' => 20,
+			),
+			'lastvalue' => array(
+				'null' => true,
+				'type' => DB::FIELD_TYPE_CHAR,
+				'length' => 255,
+			),
+			'lastclock' => array(
+				'null' => true,
+				'type' => DB::FIELD_TYPE_INT,
+				'length' => 10,
+			),
+			'prevvalue' => array(
+				'null' => true,
+				'type' => DB::FIELD_TYPE_CHAR,
+				'length' => 255,
+			),
+			'prevorgvalue' => array(
+				'null' => true,
+				'type' => DB::FIELD_TYPE_CHAR,
+				'length' => 255,
+			),
+			'lastlogsize' => array(
+				'null' => false,
+				'type' => DB::FIELD_TYPE_INT,
+				'length' => 10,
+				'default' => '0',
+			),
+			'error' => array(
+				'null' => false,
+				'type' => DB::FIELD_TYPE_CHAR,
+				'length' => 128,
+				'default' => '',
+			),
+			'mtime' => array(
+				'null' => false,
+				'type' => DB::FIELD_TYPE_INT,
+				'length' => 10,
+				'default' => '0',
+			),
+			'lastns' => array(
+				'null' => true,
+				'type' => DB::FIELD_TYPE_INT,
+				'length' => 10,
+			),
+
+		),
+	),
 	'item_discovery' => array(
 		'key' => 'itemdiscoveryid',
 		'fields' => array(
@@ -3794,6 +3805,18 @@
 				'length' => 255,
 				'default' => '',
 			),
+			'error' => array(
+				'null' => false,
+				'type' => DB::FIELD_TYPE_CHAR,
+				'length' => 128,
+				'default' => '',
+			),
+			'lastlogsize' => array(
+				'null' => false,
+				'type' => DB::FIELD_TYPE_INT,
+				'length' => 10,
+				'default' => '0',
+			),
 		),
 	),
 	'items_applications' => array(
Index: include/items.inc.php
===================================================================
--- include/items.inc.php	(revision 20279)
+++ include/items.inc.php	(working copy)
@@ -652,9 +652,10 @@
 		$table = new CTableInfo(S_NO_ITEMS_DEFINED);
 
 // COpt::profiling_start('prepare_data');
-		$result = DBselect('SELECT DISTINCT h.hostid, h.name as hostname,i.itemid, i.key_, i.value_type, i.lastvalue, i.units, i.lastclock, '.
+		$result = DBselect('SELECT DISTINCT h.hostid, h.name as hostname,i.itemid, i.key_, i.value_type, its.lastvalue, i.units, its.lastclock, '.
 				' i.name, t.priority, i.valuemapid, t.value as tr_value, t.triggerid '.
-			' FROM hosts h, items i '.
+			' FROM hosts h, items i'.
+				' JOIN items_state its ON its.itemid = i.itemid'.
 				' LEFT JOIN functions f on f.itemid=i.itemid '.
 				' LEFT JOIN triggers t on t.triggerid=f.triggerid and t.status='.TRIGGER_STATUS_ENABLED.
 			' WHERE '.DBcondition('h.hostid',$hostids).
Index: include/classes/class.cpie.php
===================================================================
--- include/classes/class.cpie.php	(revision 20279)
+++ include/classes/class.cpie.php	(working copy)
@@ -173,9 +173,10 @@
 				array_push($sql_arr,
 					'SELECT h.itemid, '.
 						' avg(h.value) AS avg,min(h.value) AS min, '.
-						' max(h.value) AS max,max(h.clock) AS clock, max(i.lastvalue) as lst '.
+						' max(h.value) AS max,max(h.clock) AS clock, max(its.lastvalue) as lst '.
 					' FROM history h '.
 						' LEFT JOIN items i ON h.itemid = i.itemid'.
+						' LEFT JOIN items_state its ON its.itemid = i.itemid'.
 					' WHERE h.itemid='.$this->items[$i]['itemid'].
 						' AND h.clock>='.$from_time.
 						' AND h.clock<='.$to_time.
@@ -184,9 +185,10 @@
 
 					'SELECT hu.itemid, '.
 						' avg(hu.value) AS avg,min(hu.value) AS min,'.
-						' max(hu.value) AS max,max(hu.clock) AS clock, max(i.lastvalue) as lst'.
+						' max(hu.value) AS max,max(hu.clock) AS clock, max(its.lastvalue) as lst'.
 					' FROM history_uint hu '.
-						' LEFT JOIN items i ON hu.itemid = i.itemid'.
+						' LEFT JOIN items i ON hu.itemid = i.itemid'.	
+						' LEFT JOIN items_state its ON its.itemid = i.itemid'.
 					' WHERE hu.itemid='.$this->items[$i]['itemid'].
 						' AND hu.clock>='.$from_time.
 						' AND hu.clock<='.$to_time.
@@ -198,9 +200,10 @@
 				array_push($sql_arr,
 					'SELECT t.itemid, '.
 						' avg(t.value_avg) AS avg,min(t.value_min) AS min,'.
-						' max(t.value_max) AS max,max(t.clock) AS clock, max(i.lastvalue) as lst'.
+						' max(t.value_max) AS max,max(t.clock) AS clock, max(its.lastvalue) as lst'.
 					' FROM trends t '.
 						' LEFT JOIN items i ON t.itemid = i.itemid'.
+						' LEFT JOIN items_state its ON its.itemid = i.itemid'.
 					' WHERE t.itemid='.$this->items[$i]['itemid'].
 						' AND t.clock>='.$from_time.
 						' AND t.clock<='.$to_time.
@@ -209,9 +212,10 @@
 
 					'SELECT t.itemid, '.
 						' avg(t.value_avg) AS avg,min(t.value_min) AS min,'.
-						' max(t.value_max) AS max,max(t.clock) AS clock, max(i.lastvalue) as lst'.
+						' max(t.value_max) AS max,max(t.clock) AS clock, max(its.lastvalue) as lst'.
 					' FROM trends_uint t '.
 						' LEFT JOIN items i ON t.itemid = i.itemid'.
+						' LEFT JOIN items_state its ON its.itemid = i.itemid'.
 					' WHERE t.itemid='.$this->items[$i]['itemid'].
 						' AND t.clock>='.$from_time.
 						' AND t.clock<='.$to_time.
Index: include/hosts.inc.php
===================================================================
--- include/hosts.inc.php	(revision 20279)
+++ include/hosts.inc.php	(working copy)
@@ -750,7 +750,7 @@
 		$def_sql['from'][] = 'hosts_groups hg';
 
 		$def_sql['where'][] = 'hg.groupid=g.groupid';
-		$def_sql['where'][] = 'EXISTS (SELECT i.hostid FROM items i WHERE hg.hostid=i.hostid AND (i.status='.ITEM_STATUS_ACTIVE.' OR i.status='.ITEM_STATUS_NOTSUPPORTED.') AND i.lastvalue IS NOT NULL)';
+		$def_sql['where'][] = 'EXISTS (SELECT i.hostid FROM items i,items_state its WHERE hg.hostid=i.hostid AND its.itemid = i.itemid AND (i.status='.ITEM_STATUS_ACTIVE.' OR i.status='.ITEM_STATUS_NOTSUPPORTED.') AND its.lastvalue IS NOT NULL)';
 	}
 
 // triggers
@@ -1019,7 +1019,7 @@
 		$def_sql['where'][] = 'EXISTS (SELECT i.hostid FROM items i WHERE h.hostid=i.hostid AND i.status='.ITEM_STATUS_ACTIVE.')';
 	}
 	else if($def_options['with_historical_items']){
-		$def_sql['where'][] = 'EXISTS (SELECT i.hostid FROM items i WHERE h.hostid=i.hostid AND (i.status='.ITEM_STATUS_ACTIVE.' OR i.status='.ITEM_STATUS_NOTSUPPORTED.') AND i.lastvalue IS NOT NULL)';
+		$def_sql['where'][] = 'EXISTS (SELECT i.hostid FROM items i,items_state WHERE h.hostid=i.hostid AND its.itemid = i.itemid AND (i.status='.ITEM_STATUS_ACTIVE.' OR i.status='.ITEM_STATUS_NOTSUPPORTED.') AND its.lastvalue IS NOT NULL)';
 	}
 
 
Index: items.php
===================================================================
--- items.php	(revision 20279)
+++ items.php	(working copy)
@@ -487,7 +487,7 @@
 			$result = delete_history_by_itemid($_REQUEST['itemid']);
 		}
 		if($result){
-			DBexecute('UPDATE items SET lastvalue=null,lastclock=null,prevvalue=null'.
+			DBexecute('UPDATE items_state SET lastvalue=null,lastclock=null,prevvalue=null'.
 				' WHERE itemid='.$_REQUEST['itemid']);
 			$host = get_host_by_hostid($item['hostid']);
 			add_audit(AUDIT_ACTION_UPDATE, AUDIT_RESOURCE_ITEM,
@@ -706,7 +706,7 @@
 		// clean history for selected items
 		DBstart();
 		$go_result = delete_history_by_itemid($_REQUEST['group_itemid']);
-		DBexecute('UPDATE items SET lastvalue=null,lastclock=null,prevvalue=null'.
+		DBexecute('UPDATE items_state SET lastvalue=null,lastclock=null,prevvalue=null'.
 					' WHERE '.DBcondition('itemid', $_REQUEST['group_itemid']));
 		foreach($_REQUEST['group_itemid'] as $id){
 			if(!$item = get_item_by_itemid($id)){
Index: latest.php
===================================================================
--- latest.php	(revision 20279)
+++ latest.php	(working copy)
@@ -245,13 +245,13 @@
 	}
 
 	$tab_rows = array();
-	$sql = 'SELECT DISTINCT i.*, ia.applicationid '.
-			' FROM items i,items_applications ia'.
+	$sql = 'SELECT DISTINCT i.itemid, i.name, i.units, its.lastclock, its.lastvalue, its.prevvalue, i.value_type, i.valuemapid, i.key_, i.status, ia.applicationid '.
+			' FROM items i,items_state its,items_applications ia'.
 			' WHERE '.DBcondition('ia.applicationid',$db_appids).
-				' AND i.itemid=ia.itemid AND i.lastvalue IS NOT NULL'.
+				' AND i.itemid=ia.itemid AND its.itemid = i.itemid AND its.lastvalue IS NOT NULL'.
 				' AND (i.status='.ITEM_STATUS_ACTIVE. ' OR i.status='.ITEM_STATUS_NOTSUPPORTED.')'.
 				' AND '.DBcondition('i.flags', array(ZBX_FLAG_DISCOVERY_NORMAL, ZBX_FLAG_DISCOVERY_CREATED)).
-			order_by('i.name,i.itemid,i.lastclock');
+			order_by('i.name,i.itemid,its.lastclock');
 //SDI($sql);
 	$db_items = DBselect($sql);
 	while($db_item = DBfetch($db_items)){
@@ -394,9 +394,10 @@
 
 	$tab_rows = array();
 
-	$sql = 'SELECT DISTINCT h.host as hostname,h.hostid,i.* '.
+	$sql = 'SELECT DISTINCT h.host as hostname,h.hostid,i.*,its.* '.
 			' FROM hosts h'.$sql_from.', items i '.
 				' LEFT JOIN items_applications ia ON ia.itemid=i.itemid'.
+				' LEFT JOIN items_state its ON its.itemid = i.itemid'.
 			' WHERE ia.itemid is NULL '.
 				$sql_where.
 				' AND h.hostid=i.hostid '.
Index: api/classes/class.chostgroup.php
===================================================================
--- api/classes/class.chostgroup.php	(revision 20279)
+++ api/classes/class.chostgroup.php	(working copy)
@@ -263,7 +263,7 @@
 		else if(!is_null($options['with_historical_items'])){
 			$sql_parts['from']['hosts_groups'] = 'hosts_groups hg';
 			$sql_parts['where']['hgg'] = 'hg.groupid=g.groupid';
-			$sql_parts['where'][] = 'EXISTS (SELECT i.hostid FROM items i WHERE hg.hostid=i.hostid AND (i.status='.ITEM_STATUS_ACTIVE.' OR i.status='.ITEM_STATUS_NOTSUPPORTED.') AND i.lastvalue IS NOT NULL)';
+			$sql_parts['where'][] = 'EXISTS (SELECT i.hostid FROM items i,items_state its WHERE hg.hostid=i.hostid AND its.itemid = i.itemid AND (i.status='.ITEM_STATUS_ACTIVE.' OR i.status='.ITEM_STATUS_NOTSUPPORTED.') AND its.lastvalue IS NOT NULL)';
 		}
 
 // with_triggers, with_monitored_triggers
Index: api/classes/class.ctrigger.php
===================================================================
--- api/classes/class.ctrigger.php	(revision 20279)
+++ api/classes/class.ctrigger.php	(working copy)
@@ -1057,9 +1057,10 @@
 			}
 
 			if(!empty($functionids)){
-				$sql = 'SELECT DISTINCT f.triggerid, f.functionid, h.host, h.name, i.lastvalue'.
-						' FROM functions f,items i,hosts h'.
+				$sql = 'SELECT DISTINCT f.triggerid, f.functionid, h.host, h.name, its.lastvalue'.
+						' FROM functions f,items i,items_state its,hosts h'.
 						' WHERE f.itemid=i.itemid'.
+							' AND its.itemid = i.itemid'.
 							' AND i.hostid=h.hostid'.
 							' AND h.status<>'.HOST_STATUS_TEMPLATE.
 							' AND '.DBcondition('f.functionid', $functionids);
Index: api/classes/class.chost.php
===================================================================
--- api/classes/class.chost.php	(revision 20279)
+++ api/classes/class.chost.php	(working copy)
@@ -437,7 +437,7 @@
 			$sql_parts['where'][] = 'EXISTS (SELECT i.hostid FROM items i WHERE h.hostid=i.hostid AND i.status='.ITEM_STATUS_ACTIVE.')';
 		}
 		else if(!is_null($options['with_historical_items'])){
-			$sql_parts['where'][] = 'EXISTS (SELECT i.hostid FROM items i WHERE h.hostid=i.hostid AND (i.status='.ITEM_STATUS_ACTIVE.' OR i.status='.ITEM_STATUS_NOTSUPPORTED.') AND i.lastvalue IS NOT NULL)';
+			$sql_parts['where'][] = 'EXISTS (SELECT i.hostid FROM items i,items_state its WHERE h.hostid=i.hostid AND its.itemid = i.itemid AND (i.status='.ITEM_STATUS_ACTIVE.' OR i.status='.ITEM_STATUS_NOTSUPPORTED.') AND its.lastvalue IS NOT NULL)';
 		}
 
 // with_triggers, with_monitored_triggers
Index: api/classes/class.ctriggerprototype.php
===================================================================
--- api/classes/class.ctriggerprototype.php	(revision 20279)
+++ api/classes/class.ctriggerprototype.php	(working copy)
@@ -977,9 +977,9 @@
 			}
 
 			if(!empty($functionids)){
-				$sql = 'SELECT DISTINCT f.triggerid, f.functionid, h.host, h.name, i.lastvalue'.
-						' FROM functions f,items i,hosts h'.
-						' WHERE f.itemid=i.itemid'.
+				$sql = 'SELECT DISTINCT f.triggerid, f.functionid, h.host, h.name, its.lastvalue'.
+						' FROM functions f,items i,items_state its,hosts h'.
+						' WHERE f.itemid=i.itemid AND its.itemid = i.itemid'.
 							' AND i.hostid=h.hostid'.
 							' AND h.status<>'.HOST_STATUS_TEMPLATE.
 							' AND '.DBcondition('f.functionid', $functionids);
Index: api/classes/class.citem.php
===================================================================
--- api/classes/class.citem.php	(revision 20279)
+++ api/classes/class.citem.php	(working copy)
@@ -135,6 +135,7 @@
 
 // editable + PERMISSION CHECK
 
+
 		if((USER_TYPE_SUPER_ADMIN == $user_type) || $options['nopermissions']){
 		}
 		else{
@@ -410,7 +411,10 @@
 
 // output
 		if($options['output'] == API_OUTPUT_EXTEND){
-			$sql_parts['select']['items'] = 'i.*';
+			$sql_parts['select']['items'] = 'i.*,its.error';
+
+			$sql_parts['from']['items_stat'] = 'items_state its';
+			$sql_parts['where']['aia'] = 'its.itemid = i.itemid';
 		}
 
 // countOutput
@@ -896,6 +900,14 @@
 
 		$itemids = DB::insert('items', $items);
 
+		$tmp_itemids = $itemids;
+
+		foreach ($tmp_itemids as $key => $itemid) {
+		    $tmp_itemids[$key] = array ('itemid' => $itemid);
+		}
+
+		DB::insert('items_state', $tmp_itemids);
+
 		$itemApplications = array();
 		foreach($items as $key => $item){
 			$items[$key]['itemid'] = $itemids[$key];
