1

可能有更好的方法来创建这些视图。我的 SQL 经验有限,所以这就是我设计它的方式,我希望你们中的一些 SQL 专家可以为我指明一个更有效的方向。

在我看来,我基本上有 3 个表(有时是 4 个),这是基本结构:

Table USER
USER_ID | EMAIL | PASSWORD | CREATED_DATE

(索引:USER_ID)

Table USER_META
ID | USER_ID | NAME | VALUE

(索引:ID、USER_ID、NAME)

Table USER_SCORES
ID | USER_ID | GAME_ID | SCORE | CREATED_DATE

(索引:ID、USER_ID)

所有表都使用第一个 ID 列作为自增主键。

第二个表“USER_META”是我保存所有联系信息和其他杂项的地方。主要是 first_name、last_name、street、city 等 - 根据用户的不同,这可能是 4 个项目或 140 个项目,这就是我使用此表而不是 USER 表中有 150 列的原因。

对于报告、搜索和编辑,我需要来自 USER_META 的大约 20 个值,所以我的视图如下所示:

View V_USR_META
select USER_ID,EMAIL,
(select VALUE from USER_META 
   where NAME = 'FIRST_NAME' and USER_ID = u.USER_ID) as first_name,
(select VALUE from USER_META 
   where NAME = 'LAST_NAME'  and USER_ID = u.USER_ID) as last_name,
(select VALUE from USER_META 
   where NAME = 'CITY'  and USER_ID = u.USER_ID) as city,
(select VALUE from USER_META 
   where NAME = 'STATE'  and USER_ID = u.USER_ID) as state,
(select VALUE from USER_META 
   where NAME = 'ZIP'  and USER_ID = u.USER_ID) as zip,
    /* 10 more selects for different meta values here */
(select max(SCORE) from USER_SCORES 
   where USER_ID = u.USER_ID) as high_score,
(select top (1) CREATED_DATE from USER_SCORES 
   where USER_ID = u.USER_ID 
   order by id desc) as last_game
from USER u

这个get很慢,实际上还有很多子查询,这只是为了说明查询。我还必须查询其他一些表才能获得杂项。关于用户的信息。

我在搜索用户时使用视图,搜索使用名称或用户 ID 或电子邮件或分数等。当我在一个地方显示所有数据时,我还使用它来填充用户信息屏幕。

那么 - 有没有更好的方法来编写视图?

4

2 回答 2

1

所有这些相关子查询的替代方法是使用maxwith case

select u.USER_ID,
       u.EMAIL,
       max(case when um.name = 'FIRST_NAME' then um.value end) first_name,
       max(case when um.name = 'LAST_NAME' then um.value end) last_name
       ...
from USER u
       left join USER_META um 
                on u.user_id = um.user_id
group by u.user_id, u.email

然后你可以添加user_scores结果:

select u.USER_ID,
       u.EMAIL,
       max(case when um.name = 'FIRST_NAME' then um.value end) first_name,
       max(case when um.name = 'LAST_NAME' then um.value end) last_name
       ...,
       max(us.score) maxscore,
       max(us.created_date) maxcreateddate
from USER u
       left join USER_META um 
                on u.user_id = um.user_id
       left join USER_SCORES us
                on u.user_id = us.user_id
group by u.user_id, u.email
于 2013-10-29T04:58:59.393 回答
0
WITH Meta AS (
    SELECT USER_ID
          ,FIRST_NAME
          ,LAST_NAME
          ,CITY
          ,STATE
          ,ZIP
    FROM USER_META
    PIVOT (
        MAX(VALUE) FOR NAME IN (FIRST_NAME, LAST_NAME, CITY, STATE, ZIP)
    ) AS p
)
,MaxScores AS (
    SELECT USER_ID
          ,MAX(SCORE) AS Score
    FROM USER_SCORES
    GROUP BY USER_ID
)
,LastGames AS (
    SELECT USER_ID
          ,MAX(CREATED_DATE) AS GameDate
    FROM USER_SCORES
    GROUP BY USER_ID
)
SELECT USER.USER_ID
      ,USER.EMAIL
      ,Meta.FIRST_NAME
      ,Meta.LAST_NAME
      ,Meta.CITY
      ,Meta.STATE
      ,Meta.ZIP
      ,MaxScores.Score
      ,LastGames.GameDate
FROM USER
     INNER JOIN Meta
         ON USER.USER_ID = Meta.USER_ID
     LEFT JOIN MaxScores
         ON USER.USER_ID = MaxScores.USER_ID
     LEFT JOIN LastGames
         ON USER.USER_ID = LastGames.USER_ID
于 2013-10-29T07:57:21.057 回答