各位大哥,我的问题是这样的,我表中数据又将近两百万条,所以查询时候很慢
例如:
select count(*),sum(price),outmimal from aa where departdate between 时间1 and 时间2
union select count(*),sum(price),outmimal from AA where departdate between 时间1 and 时间2表aa 是 AA 的备份表现在我已经在表中建立索引departdate 请问如何让它起作用呢?
谢谢大家le该问题是从我上一贴总结过来的,若有不明,可看我上一贴
再次谢谢大家了

解决方案 »

  1.   

    union两个一样?
    优化会自己选择用不用索引,不过你可以强制
      

  2.   

    where departdate between 时间1 and 时间2 ==>試下改成where departdate >= 时间1 
    and departdate <= 时间2
      

  3.   

    建立索引在departdate 字段 应该会起作用的 
      

  4.   


    SELECT * FROM PersonMember (INDEX = IX_Title) WHERE ....
      

  5.   

    SELECT c.num, c.a1, c.a2
      FROM ( SELECT COUNT(*) num, SUM(a.a1) a1, SUM(a.a2) a2 WHERE a.a3 >=时间1 AND a.a3<=时间2
              UNION ALL
             SELECT COUNT(*) num, SUM(b.b1) a1, SUM(b.b2) a2 WHERE b.b3 >=时间1 AND b.a3<=时间2
           ) c
      

  6.   

    where departdate >= 时间1 
    and departdate <= 时间2
    没有效果
      

  7.   

    SELECT c.num, c.a1, c.a2
      FROM ( SELECT COUNT(*) num, SUM(a.a1) a1, SUM(a.a2) a2 WHERE a.a3 >=时间1 AND a.a3<=时间2
              UNION ALL
             SELECT COUNT(*) num, SUM(b.b1) a1, SUM(b.b2) a2 WHERE b.b3 >=时间1 AND b.a3<=时间2
           ) c也没效果
    已经做过测试了
      

  8.   

    如果是只读表的话,建立索引的时候FILLFACTOR设置为100
      

  9.   

    尽量让索引字段条件靠近where.
      

  10.   

    select count(索引列),sum(price),outmimal from aa where departdate between 时间1 and 时间2 
    union select count(索引列),sum(price),outmimal from AA where departdate between 时间1 and 时间2 
    试试
      

  11.   

    SELECT c.num, c.a1, c.a2 
      FROM ( SELECT COUNT(*) num, SUM(a.a1) a1, SUM(a.a2) a2 WHERE a.a3 between 时间1 AND 时间2 
              UNION ALL 
            SELECT COUNT(*) num, SUM(b.b1) a1, SUM(b.b2) a2 WHERE b.b3 between  时间1 AND 时间2 
          ) c 在A3和B3上建立索引
      

  12.   


    义务帮你折腾一下:SELECT c.num, c.a1, c.a2
      FROM ( SELECT COUNT(*) num, SUM(a.a1) a1, SUM(a.a2) a2 from AA a WHERE a.a3 between 时间1 AND 时间2
              UNION ALL
            SELECT COUNT(*) num, SUM(b.b1) a1, SUM(b.b2) a2 from BB b WHERE b.b3 between  时间1 AND 时间2
          ) c
      

  13.   

    如果你加了索引,sql server 会自动判断是否使用索引.
    如果你能确定使用索引效率高,那么可以强制索引试试:select * from tb with(index=indexname) where
      

  14.   


    这个查询高效的索引加法:
    --sql server 2000:
    create index ix_01 on aa(departdate,price,outmimal)--sql server 2005/2008
    create index ix_01 on aa(departdate) include(price,outmimal)
      

  15.   

    CREATE INDEX ix_1 ON record (selldate,name,price)
    GO
    CREATE INDEX ix_1 ON backup(selldate,name,Price)
    GO
    试下
      

  16.   

    对departdate建立索引.
    然后
    where departdate >= cast(时间1 as datetime)
    and departdate <= cast(时间2 as datetime)
      

  17.   

    在表名称后面加一个表提示就可以了
    from table_name with(index = idxname)
      

  18.   

    以上经测试都没效果!!请问下大家平时遇到上百万数据都如何优化啊?
    我的数据才有两百多万,应该不算很大啊  高人求教啊!!谢谢了其实 我就简单差:
    select count(*),sum(price),outmimal from aa where departdate >= 时间1 
    and departdate <= 时间2 
    也很慢的!!
    我aa表上已经有 组建 : a,b,c字段