而我做个库存管理的程序,需要对各种材料进行分类汇总,年汇总,总量总金额汇总,
还要总体汇总,有月结算,年结算,结算后还要可以对结算数据改动。估计详细记录有10万条记录左右,对于各种汇总数据和结算的数据,我应该需要时实时查询还是保存到固定的表中或固定的字段中?如果实时查询会不会速度太慢?有什么好的设计思想请指教。谢谢。
7 个解决方案
#1
如果你愿意可以看看“数据仓库”它是专门解决这类问题的!
#2
如果你的需求不需要非常实时的统计,那你可以用固定表,做个job每天晚上统计插入,如果你的需求必须完全实时准确的统计,你只有在设计表和建索引上下工夫了!
#3
对于各种汇总数据和结算的数据,应该保存到固定的表中或固定的字段中,不要时实时查询,添加数据时重新更新汇总数据的表。
我觉得应该把数据纵向分割一下,每个月有每个月的,要汇总一年的数据时把12个月的Union起来,这样查询和插入感觉都要快一点。
我觉得应该把数据纵向分割一下,每个月有每个月的,要汇总一年的数据时把12个月的Union起来,这样查询和插入感觉都要快一点。
#4
调用时处理也可以,表设计索引处理好了,可以弥补一些速度的影响
#5
赫赫!
#6
适当的索引会有意想不到的效果
我测试过
100万条记录不到5分钟
我测试过
100万条记录不到5分钟
#7
测试过程:
环境:
PIII900 128M
win2000+sql server2000企业版
table name :t_data
CLUSTERED PRIMARY KEY :id
Rows:441000
语句1:
select * from t_data where
id in
(select top 10 id from t_data where id in
(select top 60000 id from t_data order by id asc)
order by id desc)
order by id asc
时间:390ms
语句2:
select * from t_data where
id in
(select top 10 id from t_data where id in
(select top 100000 id from t_data order by id asc)
order by id desc)
order by id asc
时间:4s
语句3:
select * from t_data where
id in
(select top 10 id from t_data where id in
(select top 150000 id from t_data order by id asc)
order by id desc)
order by id asc
时间:5s
语句4:
select * from t_data where
id in
(select top 10 id from t_data where id in
(select top 200000 id from t_data order by id asc)
order by id desc)
order by id asc
时间:6s
语句5:
select * from t_data where
id in
(select top 10 id from t_data where id in
(select top 250000 id from t_data order by id asc)
order by id desc)
order by id asc
时间:过了15m没有出来,中断
语句6:
select * from t_data where
id in
(select top 10 id from t_data where id in
(select top 300000 id from t_data order by id asc)
order by id desc)
order by id asc
时间:过了20m没有出来,中断
语句7:
select top 60000 id into #a from t_data order by id asc
create index ind_a_base_id on #a(id)
select * from t_data where
id in (select top 10 id from #a order by id desc)
order by id asc
时间:20s
语句8:
select top 100000 id into #a from t_data order by id asc
create index ind_a_base_id on #a(id)
select * from t_data where
id in (select top 10 id from #a order by id desc)
order by id asc
时间:21s
语句9:
select top 150000 id into #a from t_data order by id asc
create index ind_a_base_id on #a(id)
select * from t_data where
id in (select top 10 id from #a order by id desc)
order by id asc
时间:23s
语句10:
select top 200000 id into #a from t_data order by id asc
create index ind_a_base_id on #a(id)
select * from t_data where
id in (select top 10 id from #a order by id desc)
order by id asc
时间:24s
语句11:
select top 250000 id into #a from t_data order by id asc
create index ind_a_base_id on #a(id)
select * from t_data where
id in (select top 10 id from #a order by id desc)
order by id asc
时间:26s
语句12:
select top 300000 id into #a from t_data order by id asc
create index ind_a_base_id on #a(id)
select * from t_data where
id in (select top 10 id from #a order by id desc)
order by id asc
时间:28s
语句13:
select top 400000 id into #a from t_data order by id asc
create index ind_a_base_id on #a(id)
select * from t_data where
id in (select top 10 id from #a order by id desc)
order by id asc
时间:29s
语句14:
select * from t_data where
id in
(select top 10 id from t_data where id in
(select top 250000 id from t_data order by id asc)
order by id desc)
order by id asc
时间:过了25m没有出来,还没有中断
环境:
PIII900 128M
win2000+sql server2000企业版
table name :t_data
CLUSTERED PRIMARY KEY :id
Rows:441000
语句1:
select * from t_data where
id in
(select top 10 id from t_data where id in
(select top 60000 id from t_data order by id asc)
order by id desc)
order by id asc
时间:390ms
语句2:
select * from t_data where
id in
(select top 10 id from t_data where id in
(select top 100000 id from t_data order by id asc)
order by id desc)
order by id asc
时间:4s
语句3:
select * from t_data where
id in
(select top 10 id from t_data where id in
(select top 150000 id from t_data order by id asc)
order by id desc)
order by id asc
时间:5s
语句4:
select * from t_data where
id in
(select top 10 id from t_data where id in
(select top 200000 id from t_data order by id asc)
order by id desc)
order by id asc
时间:6s
语句5:
select * from t_data where
id in
(select top 10 id from t_data where id in
(select top 250000 id from t_data order by id asc)
order by id desc)
order by id asc
时间:过了15m没有出来,中断
语句6:
select * from t_data where
id in
(select top 10 id from t_data where id in
(select top 300000 id from t_data order by id asc)
order by id desc)
order by id asc
时间:过了20m没有出来,中断
语句7:
select top 60000 id into #a from t_data order by id asc
create index ind_a_base_id on #a(id)
select * from t_data where
id in (select top 10 id from #a order by id desc)
order by id asc
时间:20s
语句8:
select top 100000 id into #a from t_data order by id asc
create index ind_a_base_id on #a(id)
select * from t_data where
id in (select top 10 id from #a order by id desc)
order by id asc
时间:21s
语句9:
select top 150000 id into #a from t_data order by id asc
create index ind_a_base_id on #a(id)
select * from t_data where
id in (select top 10 id from #a order by id desc)
order by id asc
时间:23s
语句10:
select top 200000 id into #a from t_data order by id asc
create index ind_a_base_id on #a(id)
select * from t_data where
id in (select top 10 id from #a order by id desc)
order by id asc
时间:24s
语句11:
select top 250000 id into #a from t_data order by id asc
create index ind_a_base_id on #a(id)
select * from t_data where
id in (select top 10 id from #a order by id desc)
order by id asc
时间:26s
语句12:
select top 300000 id into #a from t_data order by id asc
create index ind_a_base_id on #a(id)
select * from t_data where
id in (select top 10 id from #a order by id desc)
order by id asc
时间:28s
语句13:
select top 400000 id into #a from t_data order by id asc
create index ind_a_base_id on #a(id)
select * from t_data where
id in (select top 10 id from #a order by id desc)
order by id asc
时间:29s
语句14:
select * from t_data where
id in
(select top 10 id from t_data where id in
(select top 250000 id from t_data order by id asc)
order by id desc)
order by id asc
时间:过了25m没有出来,还没有中断
#1
如果你愿意可以看看“数据仓库”它是专门解决这类问题的!
#2
如果你的需求不需要非常实时的统计,那你可以用固定表,做个job每天晚上统计插入,如果你的需求必须完全实时准确的统计,你只有在设计表和建索引上下工夫了!
#3
对于各种汇总数据和结算的数据,应该保存到固定的表中或固定的字段中,不要时实时查询,添加数据时重新更新汇总数据的表。
我觉得应该把数据纵向分割一下,每个月有每个月的,要汇总一年的数据时把12个月的Union起来,这样查询和插入感觉都要快一点。
我觉得应该把数据纵向分割一下,每个月有每个月的,要汇总一年的数据时把12个月的Union起来,这样查询和插入感觉都要快一点。
#4
调用时处理也可以,表设计索引处理好了,可以弥补一些速度的影响
#5
赫赫!
#6
适当的索引会有意想不到的效果
我测试过
100万条记录不到5分钟
我测试过
100万条记录不到5分钟
#7
测试过程:
环境:
PIII900 128M
win2000+sql server2000企业版
table name :t_data
CLUSTERED PRIMARY KEY :id
Rows:441000
语句1:
select * from t_data where
id in
(select top 10 id from t_data where id in
(select top 60000 id from t_data order by id asc)
order by id desc)
order by id asc
时间:390ms
语句2:
select * from t_data where
id in
(select top 10 id from t_data where id in
(select top 100000 id from t_data order by id asc)
order by id desc)
order by id asc
时间:4s
语句3:
select * from t_data where
id in
(select top 10 id from t_data where id in
(select top 150000 id from t_data order by id asc)
order by id desc)
order by id asc
时间:5s
语句4:
select * from t_data where
id in
(select top 10 id from t_data where id in
(select top 200000 id from t_data order by id asc)
order by id desc)
order by id asc
时间:6s
语句5:
select * from t_data where
id in
(select top 10 id from t_data where id in
(select top 250000 id from t_data order by id asc)
order by id desc)
order by id asc
时间:过了15m没有出来,中断
语句6:
select * from t_data where
id in
(select top 10 id from t_data where id in
(select top 300000 id from t_data order by id asc)
order by id desc)
order by id asc
时间:过了20m没有出来,中断
语句7:
select top 60000 id into #a from t_data order by id asc
create index ind_a_base_id on #a(id)
select * from t_data where
id in (select top 10 id from #a order by id desc)
order by id asc
时间:20s
语句8:
select top 100000 id into #a from t_data order by id asc
create index ind_a_base_id on #a(id)
select * from t_data where
id in (select top 10 id from #a order by id desc)
order by id asc
时间:21s
语句9:
select top 150000 id into #a from t_data order by id asc
create index ind_a_base_id on #a(id)
select * from t_data where
id in (select top 10 id from #a order by id desc)
order by id asc
时间:23s
语句10:
select top 200000 id into #a from t_data order by id asc
create index ind_a_base_id on #a(id)
select * from t_data where
id in (select top 10 id from #a order by id desc)
order by id asc
时间:24s
语句11:
select top 250000 id into #a from t_data order by id asc
create index ind_a_base_id on #a(id)
select * from t_data where
id in (select top 10 id from #a order by id desc)
order by id asc
时间:26s
语句12:
select top 300000 id into #a from t_data order by id asc
create index ind_a_base_id on #a(id)
select * from t_data where
id in (select top 10 id from #a order by id desc)
order by id asc
时间:28s
语句13:
select top 400000 id into #a from t_data order by id asc
create index ind_a_base_id on #a(id)
select * from t_data where
id in (select top 10 id from #a order by id desc)
order by id asc
时间:29s
语句14:
select * from t_data where
id in
(select top 10 id from t_data where id in
(select top 250000 id from t_data order by id asc)
order by id desc)
order by id asc
时间:过了25m没有出来,还没有中断
环境:
PIII900 128M
win2000+sql server2000企业版
table name :t_data
CLUSTERED PRIMARY KEY :id
Rows:441000
语句1:
select * from t_data where
id in
(select top 10 id from t_data where id in
(select top 60000 id from t_data order by id asc)
order by id desc)
order by id asc
时间:390ms
语句2:
select * from t_data where
id in
(select top 10 id from t_data where id in
(select top 100000 id from t_data order by id asc)
order by id desc)
order by id asc
时间:4s
语句3:
select * from t_data where
id in
(select top 10 id from t_data where id in
(select top 150000 id from t_data order by id asc)
order by id desc)
order by id asc
时间:5s
语句4:
select * from t_data where
id in
(select top 10 id from t_data where id in
(select top 200000 id from t_data order by id asc)
order by id desc)
order by id asc
时间:6s
语句5:
select * from t_data where
id in
(select top 10 id from t_data where id in
(select top 250000 id from t_data order by id asc)
order by id desc)
order by id asc
时间:过了15m没有出来,中断
语句6:
select * from t_data where
id in
(select top 10 id from t_data where id in
(select top 300000 id from t_data order by id asc)
order by id desc)
order by id asc
时间:过了20m没有出来,中断
语句7:
select top 60000 id into #a from t_data order by id asc
create index ind_a_base_id on #a(id)
select * from t_data where
id in (select top 10 id from #a order by id desc)
order by id asc
时间:20s
语句8:
select top 100000 id into #a from t_data order by id asc
create index ind_a_base_id on #a(id)
select * from t_data where
id in (select top 10 id from #a order by id desc)
order by id asc
时间:21s
语句9:
select top 150000 id into #a from t_data order by id asc
create index ind_a_base_id on #a(id)
select * from t_data where
id in (select top 10 id from #a order by id desc)
order by id asc
时间:23s
语句10:
select top 200000 id into #a from t_data order by id asc
create index ind_a_base_id on #a(id)
select * from t_data where
id in (select top 10 id from #a order by id desc)
order by id asc
时间:24s
语句11:
select top 250000 id into #a from t_data order by id asc
create index ind_a_base_id on #a(id)
select * from t_data where
id in (select top 10 id from #a order by id desc)
order by id asc
时间:26s
语句12:
select top 300000 id into #a from t_data order by id asc
create index ind_a_base_id on #a(id)
select * from t_data where
id in (select top 10 id from #a order by id desc)
order by id asc
时间:28s
语句13:
select top 400000 id into #a from t_data order by id asc
create index ind_a_base_id on #a(id)
select * from t_data where
id in (select top 10 id from #a order by id desc)
order by id asc
时间:29s
语句14:
select * from t_data where
id in
(select top 10 id from t_data where id in
(select top 250000 id from t_data order by id asc)
order by id desc)
order by id asc
时间:过了25m没有出来,还没有中断