Hello,
I am using the Phalcon Model for my Application in combination with SQLite and the number of unneccesary count queries have some a great negative impact on the performance.
This Query for example took 1324 ms
SELECT si.* FROM customer si JOIN tag_to_customer tts ON si.id = tts.customer_id JOIN tag t ON t.id = tts.tag_id WHERE t.tag = ?1 EXCEPT SELECT si.* FROM tag_to_customer tts LEFT JOIN customer si ON si.id = tts.customer_id LEFT JOIN tag t ON t.id = tts.tag_id WHERE t.tag IN (?2) LIMIT 1
Afterwads a count query gets executed, hence I'm using the model's findByRawSql
method, which took 1297 ms and makes no sense in this context:
SELECT COUNT(*) "numrows" FROM (SELECT si.* FROM customer si JOIN tag_to_customer tts ON si.id = tts.customer_id JOIN tag t ON t.id = tts.tag_id WHERE t.tag = ?1 EXCEPT SELECT si.* FROM tag_to_customer tts LEFT JOIN customer si ON si.id = tts.customer_id LEFT JOIN tag t ON t.id = tts.tag_id WHERE t.tag IN (?2) LIMIT 1)
This doubles the Page loading time in this case by two.
Another example is this query which gets executed for loading a related model:
SELECT "note"."id", "note"."inserted", "note"."updated", "note"."customer_id", "note"."create_user_id", "note"."user_id", "note"."message", "note"."due_date" FROM "note" WHERE "note"."id" = :APR0 LIMIT :APL0
Even here a count gets executed.
SELECT COUNT(*) "numrows" FROM (SELECT "note"."id", "note"."inserted", "note"."updated", "note"."customer_id", "note"."create_user_id", "note"."user_id", "note"."message", "note"."due_date" FROM "note" WHERE "note"."id" = :APR0 LIMIT :APL0)
The relation is defined as a has one
and an additional count does not makes sense here at all.
$this->hasOne('note_id', '\ACME\Model\Note', 'id', ['alias' => 'Note', 'reusable' => true]);
Therefore the number of querys needed for showing a single page is nearly doubled, only because of many needless counts.
Is there a way to tell Phalcon not to perform a count on every query executed by a model?