SQL存储过程根据小类(集合)获取相关数据

limfungsuen 2009-09-07 04:01:28
有张TE_CorpInfoReal表,字段:CI_ID,C_ChildID(类别集合,以逗号隔开)...
-----------------------------
TE_CorpInfoReal存放内容举例:
CI_ID C_ChildID
10024 1045,1048,1056,1058,1063,
10025 1045,1048,1056,1058,1063,
10026 1045,
10027 1048,
10028 1056,1058,
10029 1111,
......
------------------------------------------------
我在生成静态页面,当生成比如CI_ID 为 10024的详细信息同时,调取10条其他数据中分别只要包含(1045,1048,1056,1058,1063, )其中任何一个类别值的数据。比如例子中10025,10026,10027,10028都符合要求。

因为数据量比较多,目前有50多W条,这边1分钟只能生成4个静态页面(因为都进行的是表扫描)。这样一算,要好几个月...寒。。。。

我的做法是:先取出10024对应的C_ChildID=1045,1048,1056,1058,1063,;然后分割C_ChildID,把语句凑成一个where条件;
比如:where charindex(',1045,',','+C_ChildID )>0 or charindex(',1048,',','+C_ChildID )>0 or charindex(',1056,',','+C_ChildID )>0 or charindex(',1058,',','+C_ChildID )>0 or charindex(',1063,',','+C_ChildID )>0
这里也可以改为like。
第一问题:charindex和like在这里那个快?我测试了一下,互有快慢,确定不了。
第二问题:也是关键问题,有什么好的方法既能实现需求,又能加快程序运行效率...
(CI_ID,C_ChildID都做了索引,非聚集,因为一个表值允许一个聚集索引,聚集索引我做在了另外关键字字段上--作为搜索用)
...全文
469 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
minghui0804 2009-09-14
  • 打赏
  • 举报
回复
1. 倒排表策略
2. 缓存策略
kbs 2009-09-11
  • 打赏
  • 举报
回复
索引没用。唯有改表结构,为了效率,空间换时间,冗余存储。
showjim 2009-09-10
  • 打赏
  • 举报
回复
数据量大要改结构,CI_ID和C_ChildID都要建索引,最好是非聚集的.
CI_ID C_ChildID
10024 1045
10024 1048
10024 1056
10024 1058
10024 1063
10025 1045
10025 1048
10025 1056
10025 1058
10025 1063
10026 1045
10027 1048
10028 1056
10028 1058
10029 1111
select CI_ID from TE_CorpInfoReal where C_ChildID in(select C_ChildID from TE_CorpInfoReal where CI_ID=@CI_ID)
lihan6415151528 2009-09-08
  • 打赏
  • 举报
回复
中间表。
Trance 2009-09-08
  • 打赏
  • 举报
回复
表大后速度肯定还是不行的。。把结构换下吧,要不就做个中间表

CI_ID C_ChildID
10024 1045
1048
1056
1058
1063
Trance 2009-09-08
  • 打赏
  • 举报
回复

create table tb(CI_ID int,C_ChildID nvarchar(100))
insert tb
select '10024', '1045,1048,1056,1058,1063,'
union all select '10025', '1045,1048,1056,1058,1063,'
union all select '10026', '1045,'
union all select '10027', '1048,'
union all select '10028', '1056,1058,'
union all select '10029', '1111,'

go

select *
from tb a
left join tb b on a.CI_ID <> b.CI_ID and
a.C_ChildID like '%' + b.C_ChildID + '%'
--where a.CI_ID = '10024'

go

drop table tb
guguda2008 2009-09-08
  • 打赏
  • 举报
回复
这种表结构本来就不能支持大数据量查询,想要快只能换表结构
soft_wsx 2009-09-08
  • 打赏
  • 举报
回复
第一问题:charindex和like在这里那个快?我测试了一下,互有快慢,确定不了。
--因数据而异
第二问题:也是关键问题,有什么好的方法既能实现需求,又能加快程序运行效率...
--不知用这种方法行不
将数据分拆并插入到一个编号表里面!再将编号表与其它表联系起来,(编号表可固定)
对于新插入的数据,可以写一个触发器往编号表里面插入数据!
以下为个人想法!
/*
Microsoft SQL Server 2008 (RTM) - 10.0.1600.22 (Intel X86) Jul 9 2008 14:43:34 Copyright (c)
1988-2008 Microsoft Corporation Enterprise Evaluation Edition on Windows NT 5.1 <X86>
(Build 2600: Service Pack 3)
愿和大家共同进步
如有雷同、实属巧合
●●●●●2009-09-08 14:29:01.607●●●●●
 ★★★★★soft_wsx★★★★★
*/
if OBJECT_ID('tb') is not null drop table tb
go
create table tb(CI_ID int,C_ChildID nvarchar(100))
insert tb
select '10024', '1045,1048,1056,1058,1063,'
union all select '10025', '1045,1048,1056,1058,1063,'
union all select '10026', '1045,'
union all select '10027', '1048,'
union all select '10028', '1056,1058,'
union all select '10029', '1111,'


--用临时表将数据拆分为表
declare @max int
select @max=len(c_childid) from tb order by 1
set ROWCOUNT @max
select IDENTITY(int,1,1) as id,COL=1 into #a from dbo.syscolumns a,dbo.syscolumns b --drop table #a
set ROWCOUNT 0


select a.CI_ID,SUBSTRING(a.C_ChildID,b.id,CHARINDEX(',',a.c_childid+',',b.id)-b.id) as childid
from tb a,#a b
where LEN(a.C_ChildID)>=b.id
and SUBSTRING(','+a.C_ChildID,b.id,1)=','

/*
CI_ID childid
10024 1045
10024 1048
10024 1056
10024 1058
10024 1063
10025 1045
10025 1048
10025 1056
10025 1058
10025 1063
10026 1045
10027 1048
10028 1056
10028 1058
10029 1111
*/

华夏小卒 2009-09-08
  • 打赏
  • 举报
回复
还是觉得应该把C_ChildID 的子ID都分开存进记录
1个ID一条记录
dawugui 2009-09-07
  • 打赏
  • 举报
回复
这样的数据结构估计是没法能提高查询速度了.帮顶.
lihan6415151528 2009-09-07
  • 打赏
  • 举报
回复
like,作为查找条件的字段上面建立索引
--小F-- 2009-09-07
  • 打赏
  • 举报
回复
然后最好不要用or 否则索引失效
建议改成union all
--小F-- 2009-09-07
  • 打赏
  • 举报
回复
用like可以加索引

27,579

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧