加入表需要很长时间symfony 2.7

时间:2016-05-20 11:25:09

标签: mysql query-optimization

我有以下查询需要超过20秒(20138毫秒)才能返回结果。

$locale = 'en'; // test

$query = $this->getEntityManager()->createQuery('
        SELECT
         product.id, product.productnr, ProductGrp.productgrp' . $locale . ', Criteria.criteria'.$locale.'
        FROM
            Productbundle:product product
        JOIN
            Productbundle:Criteria Criteria WITH Criteria.criteriaid =  product.criteriaid
        JOIN
            Productbundle:ProductGrp ProductGrp WITH ProductGrp.partgrpid =  product.partgrpid
        WHERE
            product.productnr =:productnr
        ')
        ->setMaxResults(1)
        ->setParameter('productnr', $productnr)
        ->getResult();

当我从“runnable query”运行查询时,在phpmyadmin中花了大约20秒(20.7809)。

runnable query:

SELECT o0_.id AS id0, o0_.productnr AS productnr1, o1_.productgrpen AS productgrpen2, o2_.criteriaen AS criteriaen3 
FROM product o0_ 
INNER JOIN Criteria o2_ ON (o2_.criteriaid = o0_.criteriaid) 
INNER JOIN ProductGrp o1_ ON (o1_.partgrpid = o0_.partgrpid) 
WHERE o0_.productnr = 'ABC1234' 
LIMIT 1;

然而,当我在phpmyadmin中运行以下代码时,返回结果所需的时间不到2秒

SELECT product.id, product.productnr,ProductGrp.productgrpen ,Criteria.criteriaen
FROM `product` 
INNER JOIN ProductGrp ON ProductGrp.partgrpid = product.partgrpid 
INNER JOIN Criteria ON Criteria.criteriaid = product.criteriaid 
Where productnr = 'ABC1234'
LIMIT 1 

表格大小

-------------------------------
|Product    | over 5mill rows |
-------------------------------
|ProductGrp | over 200 rows   |
-------------------------------
|Criteria   | over 600 rows   |
-------------------------------

Symfony版本:2.7

1 个答案:

答案 0 :(得分:0)

索引虽然没有列出,但我建议如下

table      indexed on 
Product    (productnr, id, criteriaid, partgrpid )
Criteria   (criteriaid )  -- would expect as primary key
ProductGrp (partgrpid )   -- also would expect

此外,您拥有/支持多少“区域设置”字符串版本列。