增加MySql EAV查询性能

时间:2017-08-20 01:21:11

标签: mysql sql database performance entity-attribute-value

当我运行此查询时,平均花费了1.2421秒,我认为这很慢,我已经为这些WHERE子句中的每个可能列添加了索引。那么我可以做些什么来加速这个查询呢?包含大多数数据的表是eav,其具有大约111276行/记录

SELECT SQL_CALC_FOUND_ROWS eav.entid, 
       ent.entname 
FROM   eav, 
       ent, 
       catatt ca 
WHERE  eav.entid = ent.entid 
       AND ent.status = 'active' 
       AND eav.status = 'active' 
       AND eav.attid = ca.attid 
       AND ca.catid = 1 
       AND eav.catid = 1 
       AND ( ca.canviewby <= 6 
             || ( ent.addedby = 87 
                  AND canviewby <= 6 ) ) 
       AND ( ( eav.attid = 13 
               AND ( `char` = '693fafba093bfa35118995860e340dce' ) ) 
              OR ( eav.attid = 3 
                   AND `double` = 6 ) 
              OR ( eav.attid = 45 
                   AND ( `int` = 191 ) ) ) 
GROUP  BY eav.entid 
HAVING Count(*) >= 3 

EXPLAIN输出 EXPLAIN OUTPUT

catatt表索引 enter image description here

eav表索引enter image description here

表格索引 enter image description here

1 个答案:

答案 0 :(得分:3)

我已经简化了您的查询以更好地理解它,从where子句中删除了不必要的案例,进行了查询规划。

因此,请检查此查询并发表评论结果,然后在我的回答中调试它:

SELECT 
  SQL_CALC_FOUND_ROWS 
  eav.entid, 
  ent.entname 
FROM   
  eav
INNER JOIN ent ON (eav.entid = ent.entid AND ent.status = 'active')
INNER JOIN catatt ON (eav.attid = catatt.attid AND catatt.catid = 1)
WHERE 
  eav.catid = 1 AND eav.status = 'active' 
  AND (catatt.canviewby <= 6 OR ent.addedby = 87) 
  AND
  ( 
    (eav.attid = 13 AND eav.`char` = '693fafba093bfa35118995860e340dce') 
    OR 
    (eav.attid = 3 AND eav.`double` = 6) 
    OR 
    (eav.attid = 45 AND eav.`int` = 191) 
  ) 
GROUP BY eav.entid
HAVING COUNT(eav.entid) > 2 



+我也看到你很少UPDATE - 表(数据大多插入到这些表中) - 所以试着让这些表的引擎为MyISAM
+从以下组合创建复合索引:attid, char; attid, double; attid, int
+看看mysql的配置并调整它以获得更好的查询缓存和内存使用