提高 MySql EAV 查询性能

Increase MySql EAV query performance

当我 运行 这个查询时,平均需要 1.2421 秒,我认为这很慢,我已经为那些 WHERE 子句中的每个可能的列添加了索引。那么我可以做些什么来加速这个查询呢?包含最多数据的 table 是 eav,它有大约 111276 rows/records

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 

解释输出

catatt table 指数

eav table 指数

ent table 索引

我已经简化了您的查询以便更好地理解它,从 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-ing tables(数据主要插入到这些 tables) - 所以尽量让这些 table 的引擎成为MyISAM
+ 从以下组合创建复合索引:attid, charattid, double; attid, int
+ 查看 mysql 的配置并调整它以获得更好的查询缓存和内存使用