query advice

I have an internet db designed as follows (scripts follow and the end of the post)

table entities - different articles to be published to the internet

table medias - media files upload to our site (may be of different types - audio, video, pictures,...)

table entity_medias - N:N relationship between entities and entity_medias

many queries are like : choose the desired entities and then give me the top 1 media files of each type of media (i.e. 1 picture, 1 video, 1 audio, etc.) for each entity chosen. there are several pieces of info needed for each media: url, type, tool tip, descr,...) each entity can ahve between 0 and N medias of each type

I created a function using pivot to return all the media info to be select after the entity list has been created and it seems faster than any other method I tried, but still it is used a lot and is heavy. any suggestions for improvement?

typical query:

select bunch of fields from entities, all fields from func

  from entities

 outer apply fn_add_top1_media_info (entities.entity_Code) func

where conditions on entities

                          

ALTER function [fn_add_top1_media_info] 
(@entity int )
RETURNS @t TABLE ( entity_code int,
pic_media_code int, 
video_media_code int, 
audio_media_code int,
passport_media_code int,
file_media_code int,
hd_media_code int,

pic_file_ext char(4), 
video_file_ext char(4), 
audio_file_ext char(4), 
passport_file_ext char(4), 
file_file_ext char(4), 
hd_file_ext char(4), 

pic_tool_tip nvarchar(450),
video_tool_tip nvarchar(450),
audio_tool_tip nvarchar(450),
passport_tool_tip nvarchar(450),
file_tool_tip nvarchar(450),
hd_tool_tip nvarchar(450),

pic_bottom_desc nvarchar(275),
video_bottom_desc nvarchar(275),
audio_bottom_desc nvarchar(275),
passport_bottom_desc nvarchar(275),
file_bottom_desc nvarchar(275),
hd_bottom_desc nvarchar(275),

video_url varchar(300),
audio_url varchar(300),
hd_url varchar(300),

video_statDesc nvarchar(275),
audio_statDesc nvarchar(275),
hd_statDesc nvarchar(275),

can_enlarge bit)
AS

begin
insert into @t (entity_code ,
pic_media_code , 
video_media_code , 
audio_media_code ,
passport_media_code ,
file_media_code ,
hd_media_code ,

pic_file_ext , 
video_file_ext , 
audio_file_ext , 
passport_file_ext , 
file_file_ext , 
hd_file_ext , 

pic_tool_tip ,
video_tool_tip ,
audio_tool_tip ,
passport_tool_tip ,
file_tool_tip ,
hd_tool_tip ,

pic_bottom_desc ,
video_bottom_desc ,
audio_bottom_desc ,
passport_bottom_desc ,
file_bottom_desc ,
hd_bottom_desc ,

video_url ,
audio_url ,
hd_url,

video_statDesc,
audio_statDesc,
hd_statDesc,

can_enlarge)

select entity_code,
min([1]) as pic_media_code, 
min([2]) as video_media_code, 
min([3]) as audio_media_code,
min([4]) as passport_media_code,
min([5]) as file_media_code,
min([7]) as hd_media_code,

min([101]) as pic_file_ext, 
min([102]) as video_file_ext,
min([103]) as audio_file_ext,
min([104]) as passport_file_ext,
min([105]) as file_file_ext,
min([107]) as hd_file_ext,

min([201]) as pic_tool_tip,
min([202]) as video_tool_tip,
min([203]) as audio_tool_tip,
min([204]) as passport_tool_tip,
min([205]) as file_tool_tip,
min([207]) as hd_tool_tip,

min([301]) as pic_bottom_desc,
min([302]) as video_bottom_desc,
min([303]) as audio_bottom_desc,
min([304]) as passport_bottom_desc,
min([305]) as file_bottom_desc,
min([307]) as hd_bottom_desc,

min([402]) as video_url,
min([403]) as audio_url,
min([407]) as hd_url,

min([502]) as video_statDesc,
min([503]) as audio_statDesc,
min([507]) as hd_statDesc,

min(can_enlarge) as can_enlarge
from
(
select em.entity_code, em.media_code, m.file_extension, url,
  case when m.media_type_code=1 then convert(tinyint,m.can_enlarge) else null end as can_enlarge,
  tool_tip = dbo.add_copyrights( em.tool_tip, m.media_company_code,0),
      bottom_desc = dbo.add_copyrights(em.bottom_desc, m.media_company_code, 1 ),
  statDesc = isNull(dbo.Trim(em.bottom_desc),m.media_desc),
  --ROW_NUMBER() OVER(PARTITION BY em.entity_code,m.media_type_code ORDER BY em.entity_code DESC) as rownum,
  media_type = m.media_type_code,
  ext_type = m.media_type_code+100,
  tool_type = m.media_type_code+200,
      bottom_type = m.media_type_code+300,
  url_type = m.media_type_code+400,
  stat_type = m.media_type_code+500
 from entity_medias em 
inner join medias m 
 on em.media_code=m.media_code 
where em.sort_order = 1 and entity_code = @entity) as tmpPic 

pivot
(min(media_code)
for media_type in ([1],[2],[3],[4],[5],[7])
) as mvt
pivot
(min(file_extension)
for ext_type in ([101],[102],[103],[104],[105],[107])
) as fvt
pivot
(min(tool_tip)
for tool_type in ([201],[202],[203],[204],[205],[207])
) as tvt
pivot
(min(bottom_desc)
for bottom_type in ([301],[302],[303],[304],[305],[307])
) as bvt
pivot
(min(url)
for url_type in ([402],[403],[407])
) as uvt
pivot
(min(statDesc)
for stat_type in ([502],[503],[507])
) as svt
--where rownum=1 
group by entity_code

return

end

 table definitions

CREATE TABLE [entities](
[site_code] [smallint] NOT NULL,
[entity_type_code] [smallint] NOT NULL,
[entity_code] [int] IDENTITY(1,1) NOT FOR REPLICATION NOT NULL,
[extra] [bit] NOT NULL,
[start_date] [smalldatetime] NOT NULL,
[end_date] [smalldatetime] NOT NULL,
[create_date] [smalldatetime] NOT NULL,
[show_time] [tinyint] NOT NULL,
[update_date] [smalldatetime] NOT NULL,
[comment] [nvarchar](350) NOT NULL,
[worker_code] [smallint] NULL,
[user_code] [smallint] NOT NULL,
[worker_name] [nvarchar](280) SPARSE  NULL,
[email] [varchar](40) SPARSE  NULL,
[update_content_date] [smalldatetime] NULL,
[locked_user_code] [smallint] SPARSE  NULL,
[locked_time] [smalldatetime] SPARSE  NULL,
[locked_ip] [char](15) SPARSE  NULL,
[locked_station] [char](15) SPARSE  NULL,
[locked_user] [varchar](25) SPARSE  NULL,
[update_computer] [char](15) SPARSE  NULL,
[rowguid] [uniqueidentifier] ROWGUIDCOL  NOT NULL,
[is_exclusive] [bit] NOT NULL,
 CONSTRAINT [PK_entities] PRIMARY KEY NONCLUSTERED 
(
[entity_code] ASC)

CREATE TABLE [medias](
[media_code] [int] IDENTITY(1,1) NOT FOR REPLICATION NOT NULL,
[media_date] [smalldatetime] NOT NULL,
[media_desc] [nvarchar](50) NOT NULL,
[media_type_code] [smallint] NOT NULL,
[media_subtype_code] [tinyint] NOT NULL,
[url] [varchar](300) NOT NULL,
[file_extension] [char](4) NOT NULL,
[media_rights] [nvarchar](50) SPARSE  NULL,
[can_enlarge] [bit] NOT NULL,
[can_delete] [bit] NOT NULL,
[is_deleted] [bit] NOT NULL,
[media_company_code] [tinyint] NOT NULL,

 CONSTRAINT [PK_medias] PRIMARY KEY CLUSTERED 
(
[media_code] ASC)

CREATE TABLE [entity_medias](
[entity_code] [int] NOT NULL,
[media_code] [int] NOT NULL,
[sort_order] [tinyint] NOT NULL,
[tool_tip] [nvarchar](350) NOT NULL,
[bottom_desc] [nvarchar](175) NOT NULL,
[updated_by] [smallint] NULL,
[update_time] [smalldatetime] NULL,
[update_computer] [char](15) SPARSE  NULL,
[rowguid] [uniqueidentifier] ROWGUIDCOL  NOT NULL,
 CONSTRAINT [PK_entity_medias] PRIMARY KEY CLUSTERED 
(
[entity_code] ASC,
[media_code] ASC)


CREATE NONCLUSTERED INDEX [IX_entity_medias] ON [entity_medias] 
(
[entity_code] ASC,
[sort_order] ASC
)
INCLUDE ( [media_code]) WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [index]
GO



CREATE NONCLUSTERED INDEX [IX_entity_medias_1] ON [entity_medias] 
(
[sort_order] ASC
)
INCLUDE ( [entity_code],
[media_code]) WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [index]
GO

August 18th, 2014 10:36am

Why using a pivot at all? Why don't you lookup the values in a client-side cursor?

Free Windows Admin Tool Kit Click here and download it now
August 19th, 2014 4:24am

this is an internet application and clients do not have direct access to the db. a cursor would have to look up values for every entity and sometimes there are dozens or hundreds of entities chosen. a cursor would then make dozens or hundreds of separate queries ( one per entity and quite possibly several per entity per media type) instead of one using the pivot .
August 19th, 2014 4:29am

No, a client-side cursor is also only one query. And client-side cursor doesn't mean it runs at the users side. It runs where you consume the data (e.g. like your pivot).

The problem is quite simple: the design is an EAV model. There is imho no need to flatten that by a pivot.

btw, what's the sense behind this:

 media_type = m.media_type_code,
 ext_type = m.media_type_code+100,
 tool_type = m.media_type_code+200,
 bottom_type = m.media_type_code+300,
 url_type = m.media_type_code+400,
 stat_type = m.media_type_code+500

Use an appropriate model.

Free Windows Admin Tool Kit Click here and download it now
August 19th, 2014 7:43am

I guess I don't understand what you mean by a "client side cursor" can you explain or give an example?

for each entity, there are 6 types of medias. (audio, video, picture, thumbnail, file, ...)

for each type of media there are between 0-N medias each of which has several pieces(up to 7) of important info

I need the top 1  set of info for each media type (assuming that there at least 1 value) . so for each media, i need x columns and not just one. the +100 allowed pivoting of multiple columns rather than just one


August 19th, 2014 9:58am

If you provide sample data and a required output it might help to clarify your needs.
Free Windows Admin Tool Kit Click here and download it now
August 19th, 2014 10:16am

a simplified schema and data for 1 entity. queries select a set of entities and fill in the function's data for each select entity. I am supplying create table, insert data, create function, and finally a select using the function

CREATE TABLE [entity_medias](
[entity_code] [int] NOT NULL,
[media_code] [int] NOT NULL,
[sort_order] [tinyint] NOT NULL,
[tool_tip] [nvarchar](350) NOT NULL,
[bottom_desc] [nvarchar](175) NOT NULL,
 CONSTRAINT [PK_entity_medias] PRIMARY KEY CLUSTERED 
( [entity_code] ASC, [media_code] ASC))

CREATE TABLE [medias](
[media_code] [int] IDENTITY(1,1) NOT FOR REPLICATION NOT NULL,
[media_date] [smalldatetime] NOT NULL,
[media_desc] [nvarchar](50) NOT NULL,
[media_type_code] [smallint] NOT NULL,
[url] [varchar](300) NOT NULL,
[file_extension] [char](4) NOT NULL,
[can_enlarge] [bit] NOT NULL,
 CONSTRAINT [PK_medias] PRIMARY KEY CLUSTERED 
( [media_code] ASC))

INSERT INTO [dbo].[entity_medias] ([entity_code], [media_code], [sort_order], [tool_tip], [bottom_desc]) VALUES (671471, 501043, 1, N'06.09.09', N' ')
INSERT INTO [dbo].[entity_medias] ([entity_code], [media_code], [sort_order], [tool_tip], [bottom_desc]) VALUES (671471, 501845, 2, N'09.09.09', N' ')
INSERT INTO [dbo].[entity_medias] ([entity_code], [media_code], [sort_order], [tool_tip], [bottom_desc]) VALUES (671471, 502467, 3, N'11.09.09', N' ')
INSERT INTO [dbo].[entity_medias] ([entity_code], [media_code], [sort_order], [tool_tip], [bottom_desc]) VALUES (671471, 504610, 4, N'21.09.09', N' "')
INSERT INTO [dbo].[entity_medias] ([entity_code], [media_code], [sort_order], [tool_tip], [bottom_desc]) VALUES (671471, 507269, 5, N'04.10.09', N' "')
INSERT INTO [dbo].[entity_medias] ([entity_code], [media_code], [sort_order], [tool_tip], [bottom_desc]) VALUES (671471, 510555, 6, N'18.10.09', N' ')
INSERT INTO [dbo].[entity_medias] ([entity_code], [media_code], [sort_order], [tool_tip], [bottom_desc]) VALUES (671471, 512258, 7, N'25.10.09', N' "')
INSERT INTO [dbo].[entity_medias] ([entity_code], [media_code], [sort_order], [tool_tip], [bottom_desc]) VALUES (671471, 513950, 8, N'01.11.09', N' "')
INSERT INTO [dbo].[entity_medias] ([entity_code], [media_code], [sort_order], [tool_tip], [bottom_desc]) VALUES (671471, 516150, 9, N'09.11.09', N' "')
INSERT INTO [dbo].[entity_medias] ([entity_code], [media_code], [sort_order], [tool_tip], [bottom_desc]) VALUES (671471, 554976, 1, N' ". : ', N'')
INSERT INTO [dbo].[entity_medias] ([entity_code], [media_code], [sort_order], [tool_tip], [bottom_desc]) VALUES (671471, 801439, 1, N'1.11.13', N' ".')
INSERT INTO [dbo].[entity_medias] ([entity_code], [media_code], [sort_order], [tool_tip], [bottom_desc]) VALUES (671471, 802618, 2, N'8.11.13', N' ".')
INSERT INTO [dbo].[entity_medias] ([entity_code], [media_code], [sort_order], [tool_tip], [bottom_desc]) VALUES (671471, 803053, 3, N'15.11.13', N' ".')
INSERT INTO [dbo].[entity_medias] ([entity_code], [media_code], [sort_order], [tool_tip], [bottom_desc]) VALUES (671471, 805254, 4, N'22.11.13', N' ".')
INSERT INTO [dbo].[entity_medias] ([entity_code], [media_code], [sort_order], [tool_tip], [bottom_desc]) VALUES (671471, 805519, 5, N'29.11.13', N' ".')
INSERT INTO [dbo].[entity_medias] ([entity_code], [media_code], [sort_order], [tool_tip], [bottom_desc]) VALUES (671471, 806827, 6, N'6.12.13', N' ".')
INSERT INTO [dbo].[entity_medias] ([entity_code], [media_code], [sort_order], [tool_tip], [bottom_desc]) VALUES (671471, 810254, 7, N'20.12.13', N' ".')
INSERT INTO [dbo].[entity_medias] ([entity_code], [media_code], [sort_order], [tool_tip], [bottom_desc]) VALUES (671471, 810739, 8, N'27.12.13', N' ".')
INSERT INTO [dbo].[entity_medias] ([entity_code], [media_code], [sort_order], [tool_tip], [bottom_desc]) VALUES (671471, 815399, 9, N'3.1.14', N' ".')

PRINT(N'Add 19 rows to [dbo].[medias]')
GO
SET IDENTITY_INSERT [dbo].[medias] ON
INSERT INTO [dbo].[medias] ([media_code], [media_date], [media_desc], [media_type_code], [url], [file_extension], [can_enlarge]) VALUES (501043, '2009-09-06 20:16:00.000', N' ', 3, N'http://switch5.castup.net/frames/20040704_IBA_Popup/iba_gray.asp?ai=31&ar=2_2_2009-09-06_191735', N'wma ', 0)
INSERT INTO [dbo].[medias] ([media_code], [media_date], [media_desc], [media_type_code], [url], [file_extension], [can_enlarge]) VALUES (501845, '2009-09-09 15:17:00.000', N' ', 3, N'http://switch5.castup.net/frames/20040704_IBA_Popup/iba_gray.asp?ai=31&ar=50_1_2009-09-07_213257', N'wma ', 0)
INSERT INTO [dbo].[medias] ([media_code], [media_date], [media_desc], [media_type_code], [url], [file_extension], [can_enlarge]) VALUES (502467, '2009-09-11 16:33:00.000', N' ', 3, N'http://switch5.castup.net/frames/20040704_IBA_Popup/iba_gray.asp?ai=31&ar=2_2_2009-09-11_162457', N'wma ', 0)
INSERT INTO [dbo].[medias] ([media_code], [media_date], [media_desc], [media_type_code], [url], [file_extension], [can_enlarge]) VALUES (504610, '2009-09-21 20:03:00.000', N' "', 3, N'http://switch5.castup.net/frames/20040704_IBA_Popup/iba_gray.asp?ai=31&ar=1_2_2009-09-21_194836', N'wma ', 0)
INSERT INTO [dbo].[medias] ([media_code], [media_date], [media_desc], [media_type_code], [url], [file_extension], [can_enlarge]) VALUES (507269, '2009-10-04 16:39:00.000', N' "', 3, N'http://switch5.castup.net/frames/20040704_IBA_Popup/iba_gray.asp?ai=31&ar=1_2_2009-10-04_161919', N'wma ', 0)
INSERT INTO [dbo].[medias] ([media_code], [media_date], [media_desc], [media_type_code], [url], [file_extension], [can_enlarge]) VALUES (510555, '2009-10-18 19:49:00.000', N' ', 3, N'http://switch5.castup.net/frames/20040704_IBA_Popup/iba_gray.asp?ai=31&ar=1_2_2009-10-18_193009', N'wma ', 0)
INSERT INTO [dbo].[medias] ([media_code], [media_date], [media_desc], [media_type_code], [url], [file_extension], [can_enlarge]) VALUES (512258, '2009-10-25 19:07:00.000', N' "', 3, N'http://switch5.castup.net/frames/20040704_IBA_Popup/iba_gray.asp?ai=31&ar=1_2_2009-10-25_175703', N'wma ', 0)
INSERT INTO [dbo].[medias] ([media_code], [media_date], [media_desc], [media_type_code], [url], [file_extension], [can_enlarge]) VALUES (513950, '2009-11-01 17:31:00.000', N' "', 3, N'http://switch5.castup.net/frames/20040704_IBA_Popup/iba_gray.asp?ai=31&ar=1_2_2009-11-01_172325', N'wma ', 0)
INSERT INTO [dbo].[medias] ([media_code], [media_date], [media_desc], [media_type_code], [url], [file_extension], [can_enlarge]) VALUES (516150, '2009-11-09 17:57:00.000', N' "', 3, N'http://switch5.castup.net/frames/20040704_IBA_Popup/iba_gray.asp?ai=31&ar=1_2_2009-11-09_174608', N'wma ', 0)
INSERT INTO [dbo].[medias] ([media_code], [media_date], [media_desc], [media_type_code], [url], [file_extension], [can_enlarge]) VALUES (554976, '2010-04-25 21:02:00.000', N'', 1, N'', N'jpg ', 1)
INSERT INTO [dbo].[medias] ([media_code], [media_date], [media_desc], [media_type_code], [url], [file_extension], [can_enlarge]) VALUES (801439, '2013-11-08 11:42:00.000', N' "', 2, N'http://switch5.castup.net/frames/20040704_IBA_Popup/iba_gray.asp?ai=31&ar=IB2_2_2013-11-08_111731', N'wmv ', 0)
INSERT INTO [dbo].[medias] ([media_code], [media_date], [media_desc], [media_type_code], [url], [file_extension], [can_enlarge]) VALUES (802618, '2013-11-15 10:57:00.000', N' "', 2, N'http://switch5.castup.net/frames/20040704_IBA_Popup/iba_gray.asp?ai=31&ar=IB2_2_2013-11-15_105345', N'wmv ', 0)
INSERT INTO [dbo].[medias] ([media_code], [media_date], [media_desc], [media_type_code], [url], [file_extension], [can_enlarge]) VALUES (803053, '2013-11-18 11:38:00.000', N' "', 2, N'http://switch5.castup.net/frames/20040704_IBA_Popup/iba_gray.asp?ai=31&ar=IB2_2_2013-11-18_113508', N'wmv ', 0)
INSERT INTO [dbo].[medias] ([media_code], [media_date], [media_desc], [media_type_code], [url], [file_extension], [can_enlarge]) VALUES (805254, '2013-11-29 11:10:00.000', N' "', 2, N'http://switch5.castup.net/frames/20040704_IBA_Popup/iba_gray.asp?ai=31&ar=IB2_2_2013-11-29_110429', N'wmv ', 0)
INSERT INTO [dbo].[medias] ([media_code], [media_date], [media_desc], [media_type_code], [url], [file_extension], [can_enlarge]) VALUES (805519, '2013-12-01 14:47:00.000', N' "', 2, N'http://switch5.castup.net/frames/20040704_IBA_Popup/iba_gray.asp?ai=31&ar=IB2_2_2013-12-01_143134', N'wmv ', 0)
INSERT INTO [dbo].[medias] ([media_code], [media_date], [media_desc], [media_type_code], [url], [file_extension], [can_enlarge]) VALUES (806827, '2013-12-08 14:16:00.000', N' "', 2, N'http://switch5.castup.net/frames/20040704_IBA_Popup/iba_gray.asp?ai=31&ar=IB2_2_2013-12-08_140914', N'wmv ', 0)
INSERT INTO [dbo].[medias] ([media_code], [media_date], [media_desc], [media_type_code], [url], [file_extension], [can_enlarge]) VALUES (810254, '2013-12-27 12:26:00.000', N' "', 2, N'http://switch5.castup.net/frames/20040704_IBA_Popup/iba_gray.asp?ai=31&ar=IB2_2_2013-12-27_121951', N'wmv ', 0)
INSERT INTO [dbo].[medias] ([media_code], [media_date], [media_desc], [media_type_code], [url], [file_extension], [can_enlarge]) VALUES (810739, '2013-12-30 15:39:00.000', N' "', 2, N'http://switch5.castup.net/frames/20040704_IBA_Popup/iba_gray.asp?ai=31&ar=IB2_2_2013-12-30_152747', N'wmv ', 0)
INSERT INTO [dbo].[medias] ([media_code], [media_date], [media_desc], [media_type_code], [url], [file_extension], [can_enlarge]) VALUES (815399, '2014-01-24 12:16:00.000', N'___', 2, N'http://switch5.castup.net/frames/20040704_IBA_Popup/iba_gray.asp?ai=31&ar=IB2_2_2014-01-10_131024', N'wmv ', 0)
SET IDENTITY_INSERT [dbo].[medias] OFF

create function [fn_add_top1_media_info] 
(@entity int )
RETURNS @t TABLE ( entity_code int,
pic_media_code int, 
video_media_code int, 
audio_media_code int,
passport_media_code int,
file_media_code int,
hd_media_code int,

pic_file_ext char(4), 
video_file_ext char(4), 
audio_file_ext char(4), 
passport_file_ext char(4), 
file_file_ext char(4), 
hd_file_ext char(4), 

pic_tool_tip nvarchar(450),
video_tool_tip nvarchar(450),
audio_tool_tip nvarchar(450),
passport_tool_tip nvarchar(450),
file_tool_tip nvarchar(450),
hd_tool_tip nvarchar(450),

pic_bottom_desc nvarchar(275),
video_bottom_desc nvarchar(275),
audio_bottom_desc nvarchar(275),
passport_bottom_desc nvarchar(275),
file_bottom_desc nvarchar(275),
hd_bottom_desc nvarchar(275),

video_url varchar(300),
audio_url varchar(300),
hd_url varchar(300),

video_statDesc nvarchar(275),
audio_statDesc nvarchar(275),
hd_statDesc nvarchar(275),

can_enlarge bit)
AS

begin

insert into @t (entity_code ,
pic_media_code , 
video_media_code , 
audio_media_code ,
passport_media_code ,
file_media_code ,
hd_media_code ,

pic_file_ext , 
video_file_ext , 
audio_file_ext , 
passport_file_ext , 
file_file_ext , 
hd_file_ext , 

pic_tool_tip ,
video_tool_tip ,
audio_tool_tip ,
passport_tool_tip ,
file_tool_tip ,
hd_tool_tip ,

pic_bottom_desc ,
video_bottom_desc ,
audio_bottom_desc ,
passport_bottom_desc ,
file_bottom_desc ,
hd_bottom_desc ,

video_url ,
audio_url ,
hd_url,

video_statDesc,
audio_statDesc,
hd_statDesc,

can_enlarge)

select entity_code,
min([1]) as pic_media_code, 
min([2]) as video_media_code, 
min([3]) as audio_media_code,
min([4]) as passport_media_code,
min([5]) as file_media_code,
min([7]) as hd_media_code,

min([101]) as pic_file_ext, 
min([102]) as video_file_ext,
min([103]) as audio_file_ext,
min([104]) as passport_file_ext,
min([105]) as file_file_ext,
min([107]) as hd_file_ext,

min([201]) as pic_tool_tip,
min([202]) as video_tool_tip,
min([203]) as audio_tool_tip,
min([204]) as passport_tool_tip,
min([205]) as file_tool_tip,
min([207]) as hd_tool_tip,

min([301]) as pic_bottom_desc,
min([302]) as video_bottom_desc,
min([303]) as audio_bottom_desc,
min([304]) as passport_bottom_desc,
min([305]) as file_bottom_desc,
min([307]) as hd_bottom_desc,

min([402]) as video_url,
min([403]) as audio_url,
min([407]) as hd_url,

min([502]) as video_statDesc,
min([503]) as audio_statDesc,
min([507]) as hd_statDesc,

min(can_enlarge) as can_enlarge
from
(
select em.entity_code, em.media_code, m.file_extension, url,
  case when m.media_type_code=1 then convert(tinyint,m.can_enlarge) else null end as can_enlarge,
  tool_tip =  em.tool_tip,
      bottom_desc = em.bottom_desc,
    statDesc = isNull(dbo.Trim(em.bottom_desc),m.media_desc),
  --ROW_NUMBER() OVER(PARTITION BY em.entity_code,m.media_type_code ORDER BY em.entity_code DESC) as rownum,
  media_type = m.media_type_code,
  ext_type = m.media_type_code+100,
  tool_type = m.media_type_code+200,
      bottom_type = m.media_type_code+300,
  url_type = m.media_type_code+400,
  stat_type = m.media_type_code+500
 from entity_medias em 
inner join medias m 
 on em.media_code=m.media_code 
where em.sort_order = 1 and entity_code = @entity) as tmpPic 

pivot
(min(media_code)
for media_type in ([1],[2],[3],[4],[5],[7])
) as mvt
pivot
(min(file_extension)
for ext_type in ([101],[102],[103],[104],[105],[107])
) as fvt
pivot
(min(tool_tip)
for tool_type in ([201],[202],[203],[204],[205],[207])
) as tvt
pivot
(min(bottom_desc)
for bottom_type in ([301],[302],[303],[304],[305],[307])
) as bvt
pivot
(min(url)
for url_type in ([402],[403],[407])
) as uvt
pivot
(min(statDesc)
for stat_type in ([502],[503],[507])
) as svt

group by entity_code

select e.*,func.*

  from entity_medias e -- this would normally the entity table but not relevant to this simplified scenario

 outer apply dbo.fn_add_top1_media_info (e.entity_code ) func

where e.entity_code=671471

August 19th, 2014 11:11am

Just compare those queries two outputs:

-- The pivot base data.
SELECT  em.entity_code ,
        em.media_code ,
        m.file_extension ,
        url ,
        CASE WHEN m.media_type_code = 1 THEN CONVERT(TINYINT, m.can_enlarge)
             ELSE NULL
        END AS can_enlarge ,
        tool_tip = em.tool_tip ,
        bottom_desc = em.bottom_desc ,
        statDesc = ISNULL(em.bottom_desc, m.media_desc) ,
        media_type = m.media_type_code ,
        ext_type = m.media_type_code + 100 ,
        tool_type = m.media_type_code + 200 ,
        bottom_type = m.media_type_code + 300 ,
        url_type = m.media_type_code + 400 ,
        stat_type = m.media_type_code + 500
FROM    entity_medias em
        INNER JOIN medias m ON em.media_code = m.media_code
WHERE   em.sort_order = 1
        AND entity_code = 671471;		

-- Your query.
SELECT  e.* ,
        func.*
FROM    entity_medias e -- this would normally the entity table but not relevant to this simplified scenario
        OUTER APPLY dbo.fn_add_top1_media_info(e.entity_code) func
WHERE   e.entity_code = 671471;

Your query does not JOIN on the media code. E.g. like

SELECT  e.* ,
        func.*
FROM    entity_medias e
        OUTER APPLY dbo.fn_add_top1_media_info(e.entity_code) func
WHERE   e.entity_code = 671471
        AND e.media_code IN ( func.pic_media_code, func.video_media_code, func.audio_media_code, func.passport_media_code, func.file_media_code,
                              func.hd_media_code );

Now the row number looks better. But we cannot tell, what data to use from func..

Using a cursor meant, that you use the pivot base query and process the result in your consumer.

Free Windows Admin Tool Kit Click here and download it now
August 20th, 2014 4:31am

I need all the data from the function. based on the media codes associated with the entity, each media type is flattened. I get the top set of audio data, the top set of video data, and the top set of picture data. the other sets (in this example) have no data.

the outer select and the where choose which entities. (in my case a specific one 671471)

the function returns the top audio, top video, top picture, top passport, top file, top media, top HD info for the entity for each category that exists. that is why I used pivots to flatten by media type all the relevant data for each set 

why would the pivot on the client which would be the web server be faster than a function in an sp on the db server?

August 20th, 2014 5:22am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics