I have a problem with phalcon query builder. When I use inWhere/notInWhere conditions, performaces go down and execution of queries is very very slow.
Following two examples of the same query, the first with notInWhere, the second with array implosion:
$arrayIDs; // array contains about 16k integer IDs. Result of this query: SELECT DISTINCT(Table2.idTable2) AS idTable2 FROM NAMESPACE\idTable2 AS idTable2 WHERE idTable2.Attr1 IS NOT NULL OR idTable2.Attr2 = 1;
// VERSION 1
$builder = $modelsManager->createBuilder()
->columns(array('COUNT(*) as CountTable1'))
->from(array('Table1'=>'NAMESPACE\Table1'))
->where('Table1.Attr1 = :Attr1:', array('Attr1' => 1))
->notInWhere('Table1.idTable1', $arrayIDs);
$builder->getQuery()->execute()->setHydrateMode(Resultset::HYDRATE_ARRAYS); //execution completed in more than 30 seconds.
// VERSION 2
$query = 'SELECT COUNT(*) AS CountTable1 FROM Table1 WHERE Table1.Attr1 = 1 AND Table1.idTable1 NOT IN (' . implode(',', $arrayIDs) . ')';
$modelsManager->executeQuery($query); //execution completed in more than 30 seconds.
Instead if I write the query selecting IDs and Count in single statement, the execution is faster.
Following the example code:
// VERSION 3
$query = 'SELECT COUNT(*) AS CountTable1 FROM NAMESPACE\Table1 AS Table1 WHERE Table1.Attr1 = 1 AND Table1.idTable1 NOT IN (SELECT DISTINCT(Table2.idTable2) AS idTable2 FROM NAMESPACE\Table2 AS Table2 WHERE Table2.Attr1 IS NOT NULL OR Table2.Attr2 = 1)';
$$modelsManager->executeQuery($query);
Where is the problem? What am I doing wrong? It is possible to obtain the same performaces with query builder?
Finally, is normal that query with inWhere and notInWhere conditions is so slow with query builder?
System configuration:
- Phalcon: 2.0.10
- PHP: 5.6.26
- MySql: 5.7.12
- OS: CentOS 7.4