If you use count() you need to execute fetchAll() first, this means you need to get all data from your database first. So, if your result have 10.000 rows, than you will need to fetch it from your database and load into memory (assign it to a variable).
With rowCount() you don't need to fetch data from database, so it is more efficient.
Wow, that's a pretty big difference. Nice benchmarking. It definitely makes more sense though - with the faster query you're simply querying the number of rows. With the slower query, you're selecting all the data in the rows, then counting it in PHP after it's been retrieved.
Comments
The easiest way would
The easiest way would be:
Log in or register to post comments
Best way to do this...
Log in or register to post comments
thank you both, I think
thank you both,
I think better is rowCount(),
but count() works too
thanks
Tomas
Log in or register to post comments
Diference
The difference here is:
If you use
count()
you need to executefetchAll()
first, this means you need to get all data from your database first. So, if your result have 10.000 rows, than you will need to fetch it from your database and load into memory (assign it to a variable).With
rowCount()
you don't need to fetch data from database, so it is more efficient.Log in or register to post comments
similarly, you can add an
similarly, you can add an expression. See http://drupal.org/node/1266664
Log in or register to post comments
$countnode = db_query("SELECT
or
http://jobs68.com
Build Drupal website please contact me itqn2004@gmail.com
TanTran
Log in or register to post comments
The other way : countQuery()
countQuery() with dynamic queries is also one of the way to acheive this.
Log in or register to post comments
This is 10x more efficient
This is 10x more resource efficient than using ->rowCount() after execute()
A healthy disregard for the impossible.
Log in or register to post comments
This is the proper way. Be
This is the proper way. Be warned though—stupidly it returns the integer count as a string.
_________
http://marmaladesoul.com
Log in or register to post comments
Major performance improvement
While it wasn't a problem until my table got huge, this method is incredibly faster.
Here are some results obtained from devel query log, based on my table and data:
db_select('table', 'alias')->fields(NULL, array('a_field'))->countQuery()->execute()->fetchField();
7ms
db_select('table', 'alias')->fields('alias')->execute()->rowCount();
4365ms
Log in or register to post comments
Wow, that's a pretty big
Wow, that's a pretty big difference. Nice benchmarking. It definitely makes more sense though - with the faster query you're simply querying the number of rows. With the slower query, you're selecting all the data in the rows, then counting it in PHP after it's been retrieved.
Log in or register to post comments
get count with db_select
Hi
Get count with db_select working fine. In the below code: