哎呀!扫描了40亿行。这是正确的吗?

时间:2011-10-20 21:46:47

标签: mysql sql performance

我对这个很困惑。根据{{​​1}}它看起来没问题,但是MYSQL慢速日志报告的结果却截然不同:

EXPLAIN

现在,如果我对此查询执行了Count : 21 (8.50%) Time : 174 s total, 8.285714 s avg, 6 s to 16 s max (7.87%) 95% of Time : 142 s total, 7.473684 s avg, 6 s to 10 s max Lock Time (s) : 0 total, 0 avg, 0 to 0 max (0.00%) 95% of Lock : 0 total, 0 avg, 0 to 0 max Rows sent : 34 avg, 1 to 42 max (0.05%) Rows examined : 2.63G avg, 8.13M to 4.22G max (99.97%) Database : Users : hub@localhost 127.0.0.1 : 100.00% (21) of query, 98.38% (243) of all users

EXPLAIN

就我所见,我看起来没问题,但是我错过了一些东西而我却看不到它。有什么想法吗?

更新 好的,这是使用g.d.d.c sql的EXPLAIN(看起来好多了):

mysql> explain SELECT t.* FROM teamproject t WHERE    t.company_id= 3494 AND t.template=0 AND t.id IN (SELECT DISTINCT a.targetId FROM acl a WHERE a.targetId=t.id and a.targetType='hub.app.model.TeamProject' AND a.company_id = 3494 AND a.role_id=4 AND a.permission_id=4  UNION  SELECT DISTINCT a.targetId FROM acl a WHERE a.targetId=t.id AND a.targetType='hub.app.model.TeamProject' AND a.company_id = 3494 AND a.role_id=-1 and a.user_id= 20929 ) ORDER BY t.name ASC limit 500 \G;
*************************** 1. row ***************************
           id: 1
  select_type: PRIMARY
        table: t
         type: ref
possible_keys: FK2EF9161C3D5FD8EF
          key: FK2EF9161C3D5FD8EF
      key_len: 8
          ref: const
         rows: 93
        Extra: Using where; Using filesort
*************************** 2. row ***************************
           id: 2
  select_type: DEPENDENT SUBQUERY
        table: a
         type: ref
possible_keys: company_id,FK1788A3D5FD8EF,FK1788AA16C7AE5,IDX1788AA27C8AE1,IDX1788AA27C8AE2,IDX1788AA27C8AE4,IDX1788AA27C8AE5,IDX1788AA27C8999
          key: company_id
      key_len: 18
          ref: const,const
         rows: 1
        Extra: Using where; Using index; Using temporary
*************************** 3. row ***************************
           id: 3
  select_type: DEPENDENT UNION
        table: a
         type: ref
possible_keys: company_id,FK1788A3D5FD8EF,IDX1788AA27C8AE1,IDX1788AA27C8AE2,IDX1788AA27C8AE4,IDX1788AA27C8AE5,IDX1788AA27C8AE6,IDX1788AA27C8999
          key: IDX1788AA27C8AE1
      key_len: 9
          ref: func
         rows: 5
        Extra: Using where; Using temporary
*************************** 4. row ***************************
           id: NULL
  select_type: UNION RESULT
        table: <union2,3>
         type: ALL
possible_keys: NULL
          key: NULL
      key_len: NULL
          ref: NULL
         rows: NULL
        Extra: 
4 rows in set (0.01 sec)

acl上的索引是:

*************************** 1. row ***************************
           id: 1
  select_type: PRIMARY
        table: t
         type: ref
possible_keys: FK2EF9161C3D5FD8EF
          key: FK2EF9161C3D5FD8EF
      key_len: 8
          ref: const
         rows: 93
        Extra: Using where; Using filesort
*************************** 2. row ***************************
           id: 2
  select_type: DEPENDENT SUBQUERY
        table: a
         type: index_subquery
possible_keys: company_id,FK1788A3D5FD8EF,FK1788AA16C7AE5,IDX1788AA27C8AE1,IDX1788AA27C8AE2,IDX1788AA27C8AE4,IDX1788AA27C8AE5,IDX1788AA27C8AE6,IDX1788AA27C8999
          key: IDX1788AA27C8AE1
      key_len: 9
          ref: func
         rows: 3
        Extra: Using where

1 个答案:

答案 0 :(得分:2)

我认为,首先,您可以重新格式化where子句以避免使用UNION。这可能会删除大量已检查的行。接下来,在acl?/ / p>中索引哪些列

SELECT 
  t.* 
FROM 
  teamproject t 
WHERE    
  t.company_id= 3494 
AND 
  t.template=0 
AND 
  t.id IN (
    SELECT DISTINCT 
      a.targetId 
    FROM 
      acl a 
    WHERE 
      a.targetId=t.id 
    and 
      a.targetType='hub.app.model.TeamProject' 
    AND 
      a.company_id = 3494
    AND 
      ((a.role_id=4 
        AND 
       a.permission_id=4)
        OR
       (a.role_id=-1 
        and 
       a.user_id= 20929))
  ) 
ORDER BY 
  t.name ASC limit 500