ᴍᴇʜᴏᴠ
ᴍᴇʜᴏᴠ

Reputation: 5266

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

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:

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

Upvotes: 0

Views: 185

Answers (2)

ᴍᴇʜᴏᴠ
ᴍᴇʜᴏᴠ

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

styks
styks

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

Related Questions