大家好。我用MySql和ASP.Net做了个小东西。
在我的程序中,有如下的SQL:String strSqlText =
                    "SELECT AVG(A1) AS avgA1 " +
                    "FROM tableA " +
                    "WHERE (date BETWEEN @startDate and @endDate) AND " +
                    "(time BETWEEN @startTime AND @endTime) AND " +
                    "(countvalueA BETWEEN @startCountA AND @endCountA) AND " +
                    "(countvalueB BETWEEN @startCountB AND @endCountB) AND " +
                    "(countvalueC BETWEEN @startCountC AND @endCountC)";客户端画面每次更新时,假设有300个不同的参数值
(
startCountA1, endCountA1, startCountB1,  endCountB1, startCountC1 ,endCountC1;
startCountA2, endCountA2, startCountB2,  endCountB2, startCountC2 ,endCountC2;
startCountA3, endCountA3, startCountB3,  endCountB3, startCountC3 ,endCountC3;
......
)
被传入上面的语句,通过查询返回300个avgA1.现在的结果是,查询及计算速度非常慢。
如何提高,请经验者指导。谢谢。  

解决方案 »

  1.   

    不知道MySQL这个小东西支持索引否?
      

  2.   

    我未建立任何索引,单表查询。
    数据库的记录数是1395832。表的结构:
    InfoID int Unchecked  (primary key)
    date nvarchar(8) Unchecked
    time varchar(8) Unchecked
    countvalueA   decimal(10, 0) Unchecked
    countvalueB   decimal(10, 0) Unchecked
    countvalueC   decimal(10, 0) Unchecked
    如果要建立索引,是否要这样做:
    creat index idx_date on tableA(date);
    creat index idx_time on tableA(time);
    creat index idx_countvalueA  on tableA(countvalueA);
    creat index idx_countvalueB  on tableA(countvalueB);
    creat index idx_countvalueC  on tableA(countvalueC);
    另外,是否用PL/SQL做这样的处理会快一些?
      

  3.   

    建议贴出以下如何结果,以便于分析:show table status like '表名' \Gshow index from 表名;
    explain 你的SQL语句
      

  4.   

    因为在一个SQL语句中,一个表引用一次,只能用一个索引另外,你还要看你的数据分布情况,如date为某一天的数据多吗?你查询时,这个data的周期一般以多少天为准?
      

  5.   


    date可以为一天,可以为一个星期的,也可以是一个月的。
      

  6.   

    索引的建法要看你表中记录的分布情况。 比如你表中共100,000 记录,但其中符合 (date BETWEEN @startDate and @endDate) AND 条件的不过 100 条,则仅建索引(`date`) 就可以了(不建议用date这种保留字为字段名 )如果date BETWEEN @startDate and @endDate记录仍很多,比如 100,000 记录中半数符合。则需要考虑是否建索引(countvalueA,countvalueB,countvalueC,date)
      

  7.   

    date可以为一天,可以为一个星期的,也可以是一个月的。一天的数据大概在2600000。 
    一个月的数据2600000 x 30--------------------------------
    像这样的数据,开始设计时,就应该考虑分区或分表了
    否则,你现在优化了,随着日期的增长,也会越来越慢
      

  8.   


    相对来说,符合date 和 time的记录数最多,
    接着是符合(countvalueC BETWEEN @startCountC AND @endCountC)的记录数多,其次是
    符合 (countvalueB BETWEEN @startCountB AND @endCountB)的记录数,次之是
    符合 (countvalueA BETWEEN @startCountA AND @endCountA)的记录数。该如何建立索引?
      

  9.   


    create index on   ....  (countvalueA,countvalueB,countvalueC,date) 最少的放在最前面
    http://dev.mysql.com/doc/refman/5.1/zh/partitioning.html
    18.1. MySQL中的分区概述
    18.2. 分区类型
    18.2.1. RANGE分区
    18.2.2. LIST分区
    18.2.3. HASH分区
    18.2.4. KEY分区
    18.2.5. 子分区
    18.2.6. MySQL分区处理NULL值的方式
    18.3. 分区管理
    18.3.1. RANGE和LIST分区的管理
    18.3.2. HASH和KEY分区的管理
    18.3.3. 分区维护
    18.3.4. 获取关于分区的信息
      

  10.   

    设想下面的结构for (int i = 0; i < 150; i++) {    // operation for DB
        select average(XXX) from table where (column = 'parameter')
    }
    上面的查询,总计循环执行150次,而且每次传进的parameter的数值各不相同。
    怎样写才能提高查询速度?
      

  11.   

    rom table where (column = 'parameter')创建基于 column 的索引。
    当您的问题得到解答后请及时结贴.
    http://topic.csdn.net/u/20090501/15/7548d251-aec2-4975-a9bf-ca09a5551ba5.html