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.
Contact me to contract me for D7 -> D10/11 migrations.
Comments
The easiest way would
The easiest way would be:
Best way to do this...
thank you both, I think
thank you both,
I think better is rowCount(),
but count() works too
thanks
Tomas
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.similarly, you can add an
similarly, you can add an expression. See http://drupal.org/node/1266664
$countnode = db_query("SELECT
or
http://jobs68.com
Build Drupal website please contact me itqn2004@gmail.com
TanTran
The other way : countQuery()
countQuery() with dynamic queries is also one of the way to acheive this.
This is 10x more efficient
This is 10x more resource efficient than using ->rowCount() after execute()
A healthy disregard for the impossible.
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
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
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.
Contact me to contract me for D7 -> D10/11 migrations.
Thanks, Anthony for
Thanks, Anthony for mentioning and sharing this result. (thumbsup)
get count with db_select
Hi
Get count with db_select working fine. In the below code:
Thx
Thank a lot! it worked for me
counting rows returned from SELECT using db_query()
The only elegant way to resolve this