各位大哥,我的问题是这样的,我表中数据又将近两百万条,所以查询时候很慢
例如:
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该问题是从我上一贴总结过来的,若有不明,可看我上一贴
再次谢谢大家了
例如:
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该问题是从我上一贴总结过来的,若有不明,可看我上一贴
再次谢谢大家了
解决方案 »
- 偷偷问一下,sql server2005安装详解,
- MySQL一个小问题 路过的高手帮忙下捏!谢谢
- 逻辑表达式和标量表达式是什么意思?
- 简单基础问题
- 在一个服务器上可以最多建立多少个数据库阿,每个数据库中有多少个表阿?
- 求一个sql语句(关于分组求和)
- 来公司也快两年了,今天才加一次工资!高兴,但还是有磁盘镜像的问题想问问?
- sql Server能不能做到几毫秒刷新一次
- 在SQL Server 2000中,导出的数据能在Excel文件中进行追加吗?
- sqlserver 中有没有全局表变量的概念
- EXECUTE 后的事务计数指出缺少了 COMMIT 或 ROLLBACK TRANSACTION 语句。原计数 = 0,当前计数 = 1。
- sql 查询
优化会自己选择用不用索引,不过你可以强制
and departdate <= 时间2
SELECT * FROM PersonMember (INDEX = IX_Title) WHERE ....
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
and departdate <= 时间2
没有效果
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也没效果
已经做过测试了
union select count(索引列),sum(price),outmimal from AA where departdate between 时间1 and 时间2
试试
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上建立索引
义务帮你折腾一下: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
如果你能确定使用索引效率高,那么可以强制索引试试:select * from tb with(index=indexname) where
这个查询高效的索引加法:
--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)
GO
CREATE INDEX ix_1 ON backup(selldate,name,Price)
GO
试下
然后
where departdate >= cast(时间1 as datetime)
and departdate <= cast(时间2 as datetime)
from table_name with(index = idxname)
我的数据才有两百多万,应该不算很大啊 高人求教啊!!谢谢了其实 我就简单差:
select count(*),sum(price),outmimal from aa where departdate >= 时间1
and departdate <= 时间2
也很慢的!!
我aa表上已经有 组建 : a,b,c字段