Database access · Question 10 of 12
For an overview, only the number of entries on page 12 should be counted. Which implementation is the most efficient?
1 answer is correct. Medium
Show answer
Answer
- D $qb->count('uid')->from('tx_myext_item')->where($qb->expr()->eq('pid', $qb->createNamedParameter(12, Connection::PARAM_INT)))->executeQuery()->fetchOne()
Explanation
count() replaces the SELECT part with COUNT(), the database only returns a number, which fetchOne() reads. Loading all rows and counting them in PHP costs memory and time. rowCount() is not reliable for SELECT on all database systems, and select('COUNT(*)') would quote the expression as a column name.