[TYPO3-dev] Tip: use SQL function COUNT(*)

Martin Kutschker masi-no at spam-typo3.org
Fri Jul 18 12:11:19 CEST 2008


Hi!

I see stuff like this in many extenions:

$res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('*', 'tx_table);
$count = $GLOBALS['TYPO3_DB']->sql_num_rows($res);

But this wastes resources as the SQL server sends you rows you don't
need. Furthermore here all fields will be request although a single
field would be sufficient (eg uid).

Anyway it's better to use this:

$res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('COUNT(*)', 'tx_table');
$row = $GLOBALS['TYPO3_DB']->sql_fetch_row($res);
$count = $row[0];

Mysql will send you now only one row which is more efficient.

Masi




More information about the TYPO3-dev mailing list