Thursday, February 18, 2021

Revised Script: Indexing and looking for a cursor in an update statement

I ran a QC script over the results of yesterday's cursor counts table, and realized I had made a logical error.


Here's the QC scripts:

select count(*) as counted, 'photos' as [Source] from dbo.photos
union all
select count(*) as counted, 'photos-null votes' as [Source] from dbo.photos where tally is null
union all
select count(*) as counted, 'votes' as [Source] from dbo.votes;

select top 10 * from dbo.photos;
select top 10 * from dbo.votes;
select sum(tally) as TotalVotesCounted from dbo.photos;
select top 10 * from dbo.votes as v left join dbo.photos as p on p.id = v.photoID where p.tally is null





And here's the revised creation script:

set nocount on;
use enron;
go

if object_id('dbo.photos') is null
create table photos (id INT identity(1,1) primary key, photo varbinary(max), tally INT);
else
truncate table dbo.photos;
go

if object_id('dbo.votes') is null
create table votes(id INT identity(1,1) primary key, userID INT, photoID INT);
else
truncate table dbo.votes;
go

DECLARE @counter INT;  
SET @counter = 1;  
WHILE @counter < 500
   BEGIN  
      insert into dbo.photos(photo) values(rand() * 1000)
      SET @counter = @counter + 1  
   END;  
GO  

DECLARE @counter INT, @userid int, @photoid int, @msg varchar(256);  
SET @counter = 1;  
WHILE @counter < 50000
	BEGIN  
		SELECT @userid = rand() * 100;
		select @photoid = rand() * 1000;
		if (@photoid between 1 and 499)
		insert into votes(userid, photoid) values (@userid, @photoid)
		SET @counter = @counter + 1;
	END;  
GO  


 



No comments:

Post a Comment