Reputation: 5266
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
Upvotes: 0
Views: 185
Reputation: 5266
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).
Upvotes: 0
Reputation: 3451
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.
Upvotes: 1