function Count()
    {
		if (!$this->CountIsKnown())
		{
			// check the cache
			$cachekey = $this->_sql . " COUNT";
			$this->_totalcount = $this->GetDelayedCache($cachekey);

			// if no cache, go to the db
			if ($this->_totalcount != null)
			{
				$this->_phreezer->Observe("DataSet.Count: skipping count query because cache exists",OBSERVE_DEBUG);
			}
			else
			{
				$this->LockCache($cachekey);

				
				$sql = "";

				// if a custom counter sql query was provided, use that because it should be more efficient
				if ($this->CountSQL)
				{
					$this->_phreezer->Observe("DataSet.Count: using CountSQL to obtain total number of records",OBSERVE_DEBUG);
					$sql = $this->CountSQL;
				}
				else
				{
					$this->_phreezer->Observe("(DataSet.Count: CountSQL was not provided so a counter query will be generated.  Implement GetCustomCountQuery in the reporter class to improve performance.)",OBSERVE_WARN);
					$sql = "select count(1) as counter from (" . $this->_sql . ") tmptable" . rand(1000,9999);
				}

				$rs = $this->_phreezer->DataAdapter->Select($sql);
				$row = $this->_phreezer->DataAdapter->Fetch($rs);
				$this->_phreezer->DataAdapter->Release($rs);
				$this->_totalcount = $row["counter"];

				$this->_phreezer->SetValueCache($cachekey, $this->_totalcount, $this->_cache_timeout);

				$this->UnlockCache($cachekey);
			}
		}

		return $this->_totalcount;
    }