来自 数据库 2019-11-23 07:21 的文章
当前位置: 网上澳门金莎娱乐 > 数据库 > 正文

.net+mssql制作抽奖程序思路及源码_MsSql_脚本之家

有产品表,包含id,name,city,addtime四个字段,因报表需要按城市分组,统计每个城市的最新10个产品,便向该表中插入了100万数据,做了如下系列测试: 复制代码 代码如下: CREATE TABLE [dbo].[products]( [id] [int] IDENTITY NOT NULL, [name] [nvarchar] NULL, [addtime] [datetime] NULL, [city] [nvarchar] NULL, CONSTRAINT [PK_products] PRIMARY KEY CLUSTERED WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] 1、采用row_number方法,执行5次,平均下来8秒左右,速度最快。 复制代码 代码如下: select no, id,name,city from (select no =row_number() over (partition by city order by addtime desc), * from products)t where no< 11 order by city asc,addtime desc 2、采用cross apply方法,执行了3次,基本都在3分5秒以上,已经很慢了。 复制代码 代码如下: select distinct b.id,b.name,b.city from products a cross apply (select top 10 * from products where city = a.city order by addtime desc) b 3、采用Count查询,只执行了两次,第一次执行到5分钟时,取消任务执行了;第二次执行到13分钟时,没有hold住又直接停止了,实在无法忍受。 复制代码 代码如下: select id,name,city from products a where from products where a.city = city and addtime>a.addtime) < 10 order by city asc,addtime desc 4、采用游标方法,这个最后测试的,执行了5次,每次都是10秒完成,感觉还不错。 复制代码 代码如下: declare @city nvarchar create table #Top(id int,name nvarchar,addtime datetime) declare mycursor cursor for select distinct city from products order by city asc open mycursor fetch next from mycursor into @city while @@fetch_status =0 begin insert into #Top select top 10 id,name,city,addtime from products where city = @city fetch next from mycursor into @city end close mycursor deallocate mycursor Select * from #Top order by city asc,addtime desc drop table #Top 通过上述对比不难发现,在面临Group获取Top N场景时,可以首选row_number,游标cursor其次,另外两个就基本不考虑了,数据量大的时候根本没法使用。

for (int i = 0; i < count; i++) { SqlCommand sqlcmd = new SqlCommand("getranddata", sqlcon); SqlParameter pcount = new SqlParameter("@count", Convert.ToInt32); SqlParameter pawards = new SqlParameter("@awards", Convert.ToInt32; sqlcmd.Parameters.Add; sqlcmd.Parameters.Add; sqlcmd.CommandType = CommandType.StoredProcedure; sqlcmd.ExecuteNonQuery();

其中三张表,一个视图,一个存储过程。

思路整理,无非就是点一个按钮,然后一个图片旋转一会就出来个结果就行了,可这个程序的要求不是这样的,是需要从数据库中随机抽取用户,根据数据库中指定的等级和人数,一键全部抽出来结果就行了。同时需要存储到数据库。还需要一个导出的功能。

抽奖结果:

复制代码 代码如下:DECLARE @i intSET @i=1 while @i<8 BEGIN IF @i<5 print space+REPLICATE ELSE print space+REPLICATESET @i=@i + 1 END

CREATE TABLE [dbo].[awards]([id] [int] IDENTITY NOT NULL,[Name] [nvarchar] NOT NULL,[Number] [int] NOT NULL,CONSTRAINT [PK_awards] PRIMARY KEY CLUSTERED WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]) ON [PRIMARY]

奖项设置:

后台代码:

SET @minid = (SELECT top 1 id FROM users ORDER BY id ASC) --查询最小idSET @maxid = (SELECT top 1 id FROM users ORDER BY id DESC) --查询最大id--set @count = 100--set @awards = 1--嵌套语句begin开始,end结束while @count>0 BEGINSELECT @randnum = ROUND * RANDSET @exist = FROM users WHERE id=@randnum) IF @exist = 1 BEGININSERT INTO resultVALUESSET @count = @count - 1 END END END

知识点:

复制代码 代码如下:DECLARE @Result INT DECLARE @Upper INT DECLARE @Lower INTSET @Lower = 1SET @Upper = 10SELECT @Result = ROUND * RANDSELECT @Result

ROUND()函数:返回按指定位数进行四舍五入的数值。

=================================================================

本文由网上澳门金莎娱乐发布于数据库,转载请注明出处:.net+mssql制作抽奖程序思路及源码_MsSql_脚本之家

关键词: