Hi all. I have a content type with 15+ fields. In some situations I need to retrieve only 3 fields.
So, is better (perfomance, security, portability, etc etc) a call to node_load() function or a direct SQL query on the right table ?
M.
node_load() should be better in most cases because it uses an internal cache to make sure a node is never loaded twice.
This ensures no SQL query repetition, that are often the worst nightmare for performance.
Pierre.
Le jeudi 02 juillet 2009 à 12:20 +0200, Michel Morelli a écrit :
Hi all. I have a content type with 15+ fields. In some situations I need to retrieve only 3 fields.
So, is better (perfomance, security, portability, etc etc) a call to node_load() function or a direct SQL query on the right table ?
M.
Note: you must care about the context your function is executed. If it's an AJAX callback, you might want to do your own query, because there is no way your node might be loaded twice.
But, if you are executing this function in normal Drupal execution flow, you have to use node_load() because there is a lot of cases in which your node will be loaded more than once (views, some custom blocks, access checks on url, etc...).
Pierre.
Le jeudi 02 juillet 2009 à 12:28 +0200, Pierre Rineau a écrit :
node_load() should be better in most cases because it uses an internal cache to make sure a node is never loaded twice.
This ensures no SQL query repetition, that are often the worst nightmare for performance.
Pierre.
Le jeudi 02 juillet 2009 à 12:20 +0200, Michel Morelli a écrit :
Hi all. I have a content type with 15+ fields. In some situations I need to retrieve only 3 fields.
So, is better (perfomance, security, portability, etc etc) a call to node_load() function or a direct SQL query on the right table ?
M.
-- [ Drupal support list | http://lists.drupal.org/ ]
Or you can use a view that return only the required fields. It is a good compromise between custom query and full node load.
In term of performance, nothing will beat custom SQL query. But this is worst in term of maintainance and portability. If you do direct SQL query be sure to use the CCK API to get the name of the tables and fields. Otherwise you will be in trouble if the CCK tables structure change (which can happen very easely if you decide to share a field among several content types or set it to multiple *after* you have written your own queries).
Damien Cirotteau Rue89 http://www.rue89.com +33 1 55 25 74 96 dcirotteau@rue89.com ==================================== Pour soutenir Rue89 : http://mur.rue89.com/ ====================================
On Thu, Jul 2, 2009 at 12:20 PM, Michel Morellimichel@ziobuddalabs.it wrote:
Hi all. I have a content type with 15+ fields. In some situations I need to retrieve only 3 fields. So, is better (perfomance, security, portability, etc etc) a call to node_load() function or a direct SQL query on the right table ?
M.
-- Michel 'ZioBudda' Morelli michel@ziobuddalabs.net Consulenza sistemistica in ambito OpenSource. Sviluppo applicazioni web dinamiche (LAMP+Ajax) Telefono: 0200619074 Telefono Cell: +39-3939890025 -- Fax: +39-0291390660
http://www.ziobudda.net ICQ: 58351764 http://www.ziobuddalabs.it Skype: zio_budda http://www.ziodrupal.net MSN: michel@ziobuddalabs.it JABBER: michel@ziobuddalabs.it
-- [ Drupal support list | http://lists.drupal.org/ ]