个页面 (商业数据不能显示)
有大概7个左右分组查询语句 。(打开页面的时候,会出现6组数据 ,
每组数据都是select coun(*) ,sum(*) from a, b, c    where  .    .  group by a.id order by )
mysql 不是非常支持 group by 。  这样导致缓慢 。
7个sql例 : SELECT U.Id, A.curre,sum(A.blnce) Total FROM Account A,User U where A.userId=U.id group by U.pa我的初步想法是这些分组 sql  分别建立视图,
之后页面直接调用view。 不知可行否?

解决方案 »

  1.   

    可以,但未必会快。建立VIEW从速度上来说,只是省掉一个分析的过程。SELECT U.Id, A.curre,U.pa,sum(A.blnce) Total 
    FROM Account A,User U 
    where A.userId=U.id 
    group by U.Id, A.curre,U.pa如果只是这两个表的联接查询,假设索引设置没有问题的话,应该速度不会慢的。
      

  2.   

    观察2个表 Account A 已经建立userid索引
    user的id是主键 ,他还需要建立索引吗 ?(索引里没有看到id)
      

  3.   

    explain 
    SELECT U.Id, A.curre,U.pa,sum(A.blnce) Total 
    FROM Account A,User U 
    where A.userId=U.id 
    group by U.Id, A.curre,U.pa;用EXPLAIN看一下执行计划,另外看一下两个表的索引。
    show index from table1;
      

  4.   

    +----+-------------+-------+------+--------------------------+--------------------------+---------+-------------------+-------+---------------------------------+
    | id | select_type | table | type | possible_keys            | key                      | key_len | ref               | rows  | Extra                           |
    +----+-------------+-------+------+--------------------------+--------------------------+---------+-------------------+-------+---------------------------------+
    |  1 | SIMPLE      | U     | ALL  | PRIMARY                  | NULL                     | NULL    | NULL              | 39941 | Using temporary; Using filesort |
    |  1 | SIMPLE      | A     | ref  | idx_accountEntity_userid | idx_accountEntity_userid | 4       | U.id |     1 | Using where                     |
    +----+-------------+-------+------+--------------------------+--------------------------+---------+-------------------+-------+---------------------------------+
      

  5.   

    因为group by 所以 Using temporary; Using filesort
    (主键好像没有必要再建立索引了吧???)
      

  6.   

    执行计划上看,没有问题啊。因为你是查所有记录,第一步做全表扫描,然后利用索引从A表中找对应的记录。
    userId 在A表中也是唯一的?主键本身就是索引。