diff options
| author | Oleg Pudeyev <oleg@bsdpower.com> | 2012-03-08 08:44:24 -0500 | 
|---|---|---|
| committer | Oleg Pudeyev <oleg@bsdpower.com> | 2012-03-08 08:44:24 -0500 | 
| commit | d380a1a36ffd5b2de6e403eb63f8754d8a277386 (patch) | |
| tree | 167e903aad4f7408a131426112a065c19ade0b56 | |
| parent | d67fae0f09b339dc0f399062dd228fba94e5aaa0 (diff) | |
| parent | c11607bdd1ad4209a7697f0dc8d46ea81795b14c (diff) | |
| download | forums-d380a1a36ffd5b2de6e403eb63f8754d8a277386.tar forums-d380a1a36ffd5b2de6e403eb63f8754d8a277386.tar.gz forums-d380a1a36ffd5b2de6e403eb63f8754d8a277386.tar.bz2 forums-d380a1a36ffd5b2de6e403eb63f8754d8a277386.tar.xz forums-d380a1a36ffd5b2de6e403eb63f8754d8a277386.zip  | |
Merge remote-tracking branch 'bantu/ticket/9813' into develop-olympus
* bantu/ticket/9813:
  [ticket/10653] Call get_row_count of base class in mysql get_estimated_row_count
  [ticket/9813] Only get posts table row count if we detected a fulltext index.
  [ticket/9813] Also use estimated row count of posts table for fulltext mysql.
  [ticket/10653] Fix parameter to substr() in unit tests. Should be 1, not -1.
  [ticket/10653] Unit tests for get_row_count() and get_estimated_row_count().
  [ticket/10653] Add ability to count table rows to database abstraction layer.
  [ticket/9813] Use table status row count only if greater than 100000 or exact.
  [ticket/9813] Use SHOW TABLE STATUS to get search stats for native on MySQL.
| -rw-r--r-- | phpBB/includes/db/dbal.php | 35 | ||||
| -rw-r--r-- | phpBB/includes/db/mysql.php | 70 | ||||
| -rw-r--r-- | phpBB/includes/db/mysqli.php | 70 | ||||
| -rw-r--r-- | phpBB/includes/search/fulltext_mysql.php | 6 | ||||
| -rw-r--r-- | phpBB/includes/search/fulltext_native.php | 13 | ||||
| -rw-r--r-- | tests/dbal/select_test.php | 25 | 
6 files changed, 203 insertions, 16 deletions
diff --git a/phpBB/includes/db/dbal.php b/phpBB/includes/db/dbal.php index 230c9c8ed7..5d456c2ff0 100644 --- a/phpBB/includes/db/dbal.php +++ b/phpBB/includes/db/dbal.php @@ -900,6 +900,41 @@ class dbal  		return true;  	} + +	/** +	* Gets the estimated number of rows in a specified table. +	* +	* @param string $table_name		Table name +	* +	* @return string				Number of rows in $table_name. +	*								Prefixed with ~ if estimated (otherwise exact). +	* +	* @access public +	*/ +	function get_estimated_row_count($table_name) +	{ +		return $this->get_row_count($table_name); +	} + +	/** +	* Gets the exact number of rows in a specified table. +	* +	* @param string $table_name		Table name +	* +	* @return string				Exact number of rows in $table_name. +	* +	* @access public +	*/ +	function get_row_count($table_name) +	{ +		$sql = 'SELECT COUNT(*) AS rows_total +			FROM ' . $this->sql_escape($table_name); +		$result = $this->sql_query($sql); +		$rows_total = $this->sql_fetchfield('rows_total'); +		$this->sql_freeresult($result); + +		return $rows_total; +	}  }  /** diff --git a/phpBB/includes/db/mysql.php b/phpBB/includes/db/mysql.php index 1e24c79577..1ccb785150 100644 --- a/phpBB/includes/db/mysql.php +++ b/phpBB/includes/db/mysql.php @@ -319,6 +319,76 @@ class dbal_mysql extends dbal  	}  	/** +	* Gets the estimated number of rows in a specified table. +	* +	* @param string $table_name		Table name +	* +	* @return string				Number of rows in $table_name. +	*								Prefixed with ~ if estimated (otherwise exact). +	* +	* @access public +	*/ +	function get_estimated_row_count($table_name) +	{ +		$table_status = $this->get_table_status($table_name); + +		if (isset($table_status['Engine'])) +		{ +			if ($table_status['Engine'] === 'MyISAM') +			{ +				return $table_status['Rows']; +			} +			else if ($table_status['Engine'] === 'InnoDB' && $table_status['Rows'] > 100000) +			{ +				return '~' . $table_status['Rows']; +			} +		} + +		return parent::get_row_count($table_name); +	} + +	/** +	* Gets the exact number of rows in a specified table. +	* +	* @param string $table_name		Table name +	* +	* @return string				Exact number of rows in $table_name. +	* +	* @access public +	*/ +	function get_row_count($table_name) +	{ +		$table_status = $this->get_table_status($table_name); + +		if (isset($table_status['Engine']) && $table_status['Engine'] === 'MyISAM') +		{ +			return $table_status['Rows']; +		} + +		return parent::get_row_count($table_name); +	} + +	/** +	* Gets some information about the specified table. +	* +	* @param string $table_name		Table name +	* +	* @return array +	* +	* @access protected +	*/ +	function get_table_status($table_name) +	{ +		$sql = "SHOW TABLE STATUS +			LIKE '" . $this->sql_escape($table_name) . "'"; +		$result = $this->sql_query($sql); +		$table_status = $this->sql_fetchrow($result); +		$this->sql_freeresult($result); + +		return $table_status; +	} + +	/**  	* Build LIKE expression  	* @access private  	*/ diff --git a/phpBB/includes/db/mysqli.php b/phpBB/includes/db/mysqli.php index 456ce906d0..a311b8cda6 100644 --- a/phpBB/includes/db/mysqli.php +++ b/phpBB/includes/db/mysqli.php @@ -316,6 +316,76 @@ class dbal_mysqli extends dbal  	}  	/** +	* Gets the estimated number of rows in a specified table. +	* +	* @param string $table_name		Table name +	* +	* @return string				Number of rows in $table_name. +	*								Prefixed with ~ if estimated (otherwise exact). +	* +	* @access public +	*/ +	function get_estimated_row_count($table_name) +	{ +		$table_status = $this->get_table_status($table_name); + +		if (isset($table_status['Engine'])) +		{ +			if ($table_status['Engine'] === 'MyISAM') +			{ +				return $table_status['Rows']; +			} +			else if ($table_status['Engine'] === 'InnoDB' && $table_status['Rows'] > 100000) +			{ +				return '~' . $table_status['Rows']; +			} +		} + +		return parent::get_row_count($table_name); +	} + +	/** +	* Gets the exact number of rows in a specified table. +	* +	* @param string $table_name		Table name +	* +	* @return string				Exact number of rows in $table_name. +	* +	* @access public +	*/ +	function get_row_count($table_name) +	{ +		$table_status = $this->get_table_status($table_name); + +		if (isset($table_status['Engine']) && $table_status['Engine'] === 'MyISAM') +		{ +			return $table_status['Rows']; +		} + +		return parent::get_row_count($table_name); +	} + +	/** +	* Gets some information about the specified table. +	* +	* @param string $table_name		Table name +	* +	* @return array +	* +	* @access protected +	*/ +	function get_table_status($table_name) +	{ +		$sql = "SHOW TABLE STATUS +			LIKE '" . $this->sql_escape($table_name) . "'"; +		$result = $this->sql_query($sql); +		$table_status = $this->sql_fetchrow($result); +		$this->sql_freeresult($result); + +		return $table_status; +	} + +	/**  	* Build LIKE expression  	* @access private  	*/ diff --git a/phpBB/includes/search/fulltext_mysql.php b/phpBB/includes/search/fulltext_mysql.php index 52372a14d8..779ec1d216 100644 --- a/phpBB/includes/search/fulltext_mysql.php +++ b/phpBB/includes/search/fulltext_mysql.php @@ -896,11 +896,7 @@ class fulltext_mysql extends search_backend  		}  		$db->sql_freeresult($result); -		$sql = 'SELECT COUNT(post_id) as total_posts -			FROM ' . POSTS_TABLE; -		$result = $db->sql_query($sql); -		$this->stats['total_posts'] = (int) $db->sql_fetchfield('total_posts'); -		$db->sql_freeresult($result); +		$this->stats['total_posts'] = empty($this->stats) ? 0 : $db->get_estimated_row_count(POSTS_TABLE);  	}  	/** diff --git a/phpBB/includes/search/fulltext_native.php b/phpBB/includes/search/fulltext_native.php index b63205fd76..dc961f3c8a 100644 --- a/phpBB/includes/search/fulltext_native.php +++ b/phpBB/includes/search/fulltext_native.php @@ -1461,17 +1461,8 @@ class fulltext_native extends search_backend  	{  		global $db; -		$sql = 'SELECT COUNT(*) as total_words -			FROM ' . SEARCH_WORDLIST_TABLE; -		$result = $db->sql_query($sql); -		$this->stats['total_words'] = (int) $db->sql_fetchfield('total_words'); -		$db->sql_freeresult($result); - -		$sql = 'SELECT COUNT(*) as total_matches -			FROM ' . SEARCH_WORDMATCH_TABLE; -		$result = $db->sql_query($sql); -		$this->stats['total_matches'] = (int) $db->sql_fetchfield('total_matches'); -		$db->sql_freeresult($result); +		$this->stats['total_words']		= $db->get_estimated_row_count(SEARCH_WORDLIST_TABLE); +		$this->stats['total_matches']	= $db->get_estimated_row_count(SEARCH_WORDMATCH_TABLE);  	}  	/** diff --git a/tests/dbal/select_test.php b/tests/dbal/select_test.php index 05b0e68e29..21b12777dc 100644 --- a/tests/dbal/select_test.php +++ b/tests/dbal/select_test.php @@ -357,4 +357,29 @@ class phpbb_dbal_select_test extends phpbb_database_test_case  		$this->assertSame(false, $row);  	} + +	public function test_get_row_count() +	{ +		$this->assertSame( +			3, +			(int) $this->new_dbal()->get_row_count('phpbb_users'), +			"Failed asserting that user table has exactly 3 rows." +		); +	} + +	public function test_get_estimated_row_count() +	{ +		$actual = $this->new_dbal()->get_estimated_row_count('phpbb_users'); + +		if (is_string($actual) && isset($actual[0]) && $actual[0] === '~') +		{ +			$actual = substr($actual, 1); +		} + +		$this->assertGreaterThan( +			1, +			$actual, +			"Failed asserting that estimated row count of user table is greater than 1." +		); +	}  }  | 
