Stop CakePHP 3 from listing all fields in the SELECT query it generates with find()

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-11 18:16:44

问题


I'm trying to optimise a large MySQL query. I accidentally found out that a query with all fields listed (SELECT Orders.id AS Orders__id, <...>; the default CakePHP behaviour) takes 4 times longer compared to a query with just SELECT * FROM - 0.324 seconds vs. 0.084 seconds; checked several times.

I'm wondering if I can disable this behaviour. I've tried:

  • adding 'fields' => '*' to the find() options or calling ->select('*'), but it results in SELECT Orders.* AS Orders__* which throws an SQLSTATE[42000] error.

  • getting rid of the aliased title with ->select(['*' => '*']) as per query-builder.html#selecting-data, but that results in SELECT * AS * which also throws an error

  • using ->enableAutoFields(false)

I also tried to Google but I don't even know how to call this


回答1:


It seems you want to override the benefits of the ORM so I'm going to suggest a method that should not be used in normal operations.

$datasource = ConnectionManager::get('default');
$datasource->execute('SELECT * FROM some_table;');

If you want to hydrate entities you must allow the select statement to alias the fields so the above will not give you entities.

My opinion is that you should use the normal select and optimize your caching strategies for this data.




回答2:


Apparently, I can't not have the fields listed because of how CakePHP ORM was designed.

So instead, solved by manually whitelisting only the fields I actually need. The query is still relatively fast after all (around 100 ms. according to my measurements).



来源:https://stackoverflow.com/questions/50528670/stop-cakephp-3-from-listing-all-fields-in-the-select-query-it-generates-with-fin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!