Click here to Skip to main content
15,910,886 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Is there any other method to do this query optimized.


SQL
select ProductId,ImageName,ImageType, ROW_NUMBER() over (order by ProductId desc) RowId from
			(
				select p.id ProductId ,p.pic_image ImageName,'pic_image' ImageType
				from product p
				left outer join iimages_edited pe on p.id = pe.[id] 
				where isnull(p.pic_image,'') <> '' and isnull(pe.pic_image,0)=0 
				union
				select p.id ProductId,p.pic_bimage ImageName,'pic_bimage' ImageType
				from product p 
				left outer join iimages_edited pe on p.id = pe.[id] 
				where isnull(p.pic_bimage,'') <> '' and isnull(pe.pic_bimage,0)=0
				union
				select p.id ProductId,p.pic_limage ImageName,'pic_limage' ImageType
				from product p 
				left outer join iimages_edited pe on p.id = pe.[id] 
				where isnull(p.pic_limage,'') <> '' and isnull(pe.pic_limage,0)=0 
				union
				select p.id ProductId,p.pic_blimage ImageName,'pic_blimage' ImageType
				from product p 
				left outer join iimages_edited pe on p.id = pe.[id] 
				where isnull(p.pic_blimage,'') <> '' and isnull(pe.pic_blimage,0)=0 
				union
				select p.id ProductId,p.pic_cimage ImageName,'pic_cimage' ImageType
				from product p 
				left outer join iimages_edited pe on p.id = pe.[id] 
				where isnull(p.pic_cimage,'') <> '' and isnull(pe.pic_cimage,0)=0
				union
				select p.id ProductId,p.pic_climage ImageName,'pic_climage' ImageType
				from product p 
				left outer join iimages_edited pe on p.id = pe.[id] 
				where isnull(p.pic_climage,'') <> '' and isnull(pe.pic_climage,0)=0
				union
				select p.id ProductId,p.pic_cimage_2 ImageName,'pic_cimage_2' ImageType
				from product p 
				left outer join iimages_edited pe on p.id = pe.[id] 
				where isnull(p.pic_cimage_2,'') <> '' and isnull(pe.pic_cimage_2,0)=0
						
				union
				select p.id ProductId,p.pic_cimage_3 ImageName,'pic_cimage_3' ImageType
				from product p 
				left outer join iimages_edited pe on p.id = pe.[id] 
				where isnull(p.pic_cimage_3,'') <> '' and isnull(pe.pic_cimage_3,0)=0
				union
				select p.id ProductId,p.pic_cimage_4 ImageName,'pic_cimage_4' ImageType
				from product p 
				left outer join iimages_edited pe on p.id = pe.[id] 
				where isnull(p.pic_cimage_4,'') <> '' and isnull(pe.pic_cimage_4,0)=0
				union
				select p.id ProductId,p.pic_cimage_5 ImageName,'pic_cimage_5' ImageType
				from product p 
				left outer join iimages_edited pe on p.id = pe.[id] 
				where isnull(p.pic_cimage_5,'') <> '' and isnull(pe.pic_cimage_5,0)=0
				union
				select p.id ProductId,p.pic_cimage_6 ImageName,'pic_cimage_6' ImageType
				from product p 
				left outer join iimages_edited pe on p.id = pe.[id] 
				where isnull(p.pic_cimage_6,'') <> '' and isnull(pe.pic_cimage_6,0)=0
				union
				select p.id ProductId,p.pic_cimage_7 ImageName,'pic_cimage_7' ImageType
				from product p 
				left outer join iimages_edited pe on p.id = pe.[id] 
				where isnull(p.pic_cimage_7,'') <> '' and isnull(pe.pic_cimage_7,0)=0
				union
				select p.id ProductId,p.pic_cimage_8 ImageName,'pic_cimage_8' ImageType
				from product p 
				left outer join iimages_edited pe on p.id = pe.[id] 
				where isnull(p.pic_cimage_8,'') <> '' and isnull(pe.pic_cimage_8,0)=0
				union
				select p.id ProductId,p.pic_cimage_9 ImageName,'pic_cimage_9' ImageType
				from product p 
				left outer join iimages_edited pe on p.id = pe.[id]
				where isnull(p.pic_cimage_9,'') <> '' and isnull(pe.pic_cimage_9,0)=0
				union
				select p.id ProductId,p.pic_cimage_10 ImageName,'pic_cimage_10' ImageType
				from product p 
				left outer join iimages_edited pe on p.id = pe.[id] 
				where isnull(p.pic_cimage_10,'') <> '' and isnull(pe.pic_cimage_10,0)=0 
		)t
Posted
Comments
Tomas Takac 19-Nov-15 5:04am    
Seems to me your schema is not quite correct. Anyway, use UNION ALL instead of UNION - there is no need to merge the sets as they are not overlapping - each has different ImageType.

1 solution

If you are storing Images in dbase, then there is a chance of query executing slowly.
Even if you fire a simple query it would take time.

It may not be the problem of Union or Unionall because the query used is not complicated.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900