今天下午遇到了一个问题,就是对两个动态查询出来的记录集(注意是动态查询),判断二者其中一个字段是否包含相同的数据,当然,数据类型是一样的。着实为这个问题头疼了一番
我的数据库中的字段是较大的,包含了几十万行的数据,如果遍历两个记录集,天哪,这可是乘法原理呀,这样数量级的排列所产生的运算次数为:P(100000,100000),一个10万的全排列。效率有多高就知道了。
怎样才能找到一个性能较高的办法呢,把检索后的数据放入内存表,这时我想到了一个办法,就是对象的hashcode的计算方式,呵呵,如果能够为这个记录集找到一个唯一值,就像对象的HashCode,这样是不是就可以了呢,就像两个对象的比较一样。果然,试过之后,这样的问题就解决了,当然产生这个唯一值的算法是有待于研究的。
这样只要判断这个@hashvalue就可以了
我的数据库中的字段是较大的,包含了几十万行的数据,如果遍历两个记录集,天哪,这可是乘法原理呀,这样数量级的排列所产生的运算次数为:P(100000,100000),一个10万的全排列。效率有多高就知道了。
怎样才能找到一个性能较高的办法呢,把检索后的数据放入内存表,这时我想到了一个办法,就是对象的hashcode的计算方式,呵呵,如果能够为这个记录集找到一个唯一值,就像对象的HashCode,这样是不是就可以了呢,就像两个对象的比较一样。果然,试过之后,这样的问题就解决了,当然产生这个唯一值的算法是有待于研究的。
CREATE PROCEDURE [dbo].[ct_AddNewSection]
@lose_section_id int ,
@cell_id_list varchar(2000),
@errstr varchar(500) output
AS
set nocount on
declare @indexValue int,@pos int
declare @sectionid int
set @pos = 1
set @indexValue = 1
set @indexValue = charindex(',',@cell_id_list,@indexValue)
--两个内存表用来存储动态查询出的记录集
declare @t_cellid table (cellid int) --定义临时表
declare @t_sectionid table (sectionid int, tempval bigint ) --定义临时表,第一个字段是为了将多个类似的记录集放在一个表里与@t_cellid做比较
while @indexValue > 0
begin
set @cellID = substring(@cell_id_list,@pos,(@indexValue-@pos))
if @cellID <>''
begin
insert into @t_cellid(cellid)values(@cellID)
end
set @pos = @indexValue+1
set @indexValue = charindex(',',@cell_id_list,@indexValue+1) -----更改@indexValue
end
---下面就是主要的处理了,取要判断数据相同字段的hashcode
declare @hashvalue bigint
select @hashvalue = sum(CellID^3080|10880) from @t_cellid
insert into @t_sectionid select LogicSectionID,sum(CellID^3080|10880) from tb_CellOfLogicSection group by LogicSectionID
if exists (select * from @t_sectionid where tempval = @hashvalue)
begin
select @sectionid=sectionid from @t_sectionid where tempval = @hashvalue
set @errstr = '已经有包含相同组合存在,编号'+cast(@sectionid as varchar(10))
return
end
@lose_section_id int ,
@cell_id_list varchar(2000),
@errstr varchar(500) output
AS
set nocount on
declare @indexValue int,@pos int
declare @sectionid int
set @pos = 1
set @indexValue = 1
set @indexValue = charindex(',',@cell_id_list,@indexValue)
--两个内存表用来存储动态查询出的记录集
declare @t_cellid table (cellid int) --定义临时表
declare @t_sectionid table (sectionid int, tempval bigint ) --定义临时表,第一个字段是为了将多个类似的记录集放在一个表里与@t_cellid做比较
while @indexValue > 0
begin
set @cellID = substring(@cell_id_list,@pos,(@indexValue-@pos))
if @cellID <>''
begin
insert into @t_cellid(cellid)values(@cellID)
end
set @pos = @indexValue+1
set @indexValue = charindex(',',@cell_id_list,@indexValue+1) -----更改@indexValue
end
---下面就是主要的处理了,取要判断数据相同字段的hashcode
declare @hashvalue bigint
select @hashvalue = sum(CellID^3080|10880) from @t_cellid
insert into @t_sectionid select LogicSectionID,sum(CellID^3080|10880) from tb_CellOfLogicSection group by LogicSectionID
if exists (select * from @t_sectionid where tempval = @hashvalue)
begin
select @sectionid=sectionid from @t_sectionid where tempval = @hashvalue
set @errstr = '已经有包含相同组合存在,编号'+cast(@sectionid as varchar(10))
return
end