Using this answer I tried added caching to a query that is particularly slow.
This query has fixed selected fields and limits but three components can differ between different users: the where
statement on the table itself, on
filters on a joined table and on
filters on a second joined table.
So I filter data on the main table and two joined ones.
For caching to work I need to incorporate the filtered conditions into the cache-key. How can I extract all the find filters, including on joins, for this purpose?
If I use $q->clause('where')
, like in the linked suggestion, I only get the filter on the main table, not the joins. And $q->clause('join')
is empty.
$query = $this->Regions->find('veryComplicatedFinder')
->cache(function (\Cake\ORM\Query $q)
{
debug($q->clause('where'));
return 'test123';
}, 'queries');
I figured I might just use the entire SQL statement. However, $q->sql()
results in something like:
'SELECT ... FROM regions Regions INNER JOIN chains Chains ON (Chains.id in (:c0,:c1,:c2) AND Chains.id = (Regions.chain_id)) LEFT JOIN stores Stores ON (Stores.type in (:c388) AND Stores.type in (:c389) AND Regions.id = (Stores.region_id)) LEFT JOIN sales Sales ON Stores.id = (Sales.store_id) WHERE (Regions.shape && ST_MakeEnvelope(51.5,5,52,5.5,4326)) GROUP BY Regions.id LIMIT 2000'
It seems the binding parameters are not included.