数据库设计问题:处理 GUID 周围的地方

发布于 2024-09-11 18:51:36 字数 502 浏览 0 评论 0原文

这是一个简化的示例。

UserDetails (UserID INT UserGUID UNIQUEIDENTIFIER Name VARCHAR(50) Age INT)
UserRatings (UserID INT Rating INT Date DATETIME)
UserVotes   (UserID INT Votes INT Date DATETIME)

UserGUID 仅存在于主表中。

在应用程序层中,仅解析 GUID,而不解析 INTS。这是为了防止竞争对手猜测我网站上的一些关键数字(例如用户数)。

在与表 UserRatings 和 UserVotes 相关的 SPROCS 中,我必须声明 UserID 并根据在每个过程开始时解析的 UserGUID 来设置它。

这种情况不仅发生在 Users 表中,而且发生在具有主表和分支表的所有其他对象中,因此到处都有大量这些 DECLARE/SET 类型的过程。

这是一个好的设计吗?

Here's a simplified example.

UserDetails (UserID INT UserGUID UNIQUEIDENTIFIER Name VARCHAR(50) Age INT)
UserRatings (UserID INT Rating INT Date DATETIME)
UserVotes   (UserID INT Votes INT Date DATETIME)

The UserGUID only exists in the main table.

In the application layer only GUID's are parsed around, never INTS. This is to prevent competitors from guessing at some key numbers on my site like (e.g. user count).

In the SPROCS relating to tables UserRatings and UserVotes, I have to DECLARE UserID and SET it based on the UserGUID that is getting parsed back at the start of every procedure.

This occurs not just for the Users table but for every other object that has a main table and branched tables so there are tons of these DECLARE/SET type procedures all over the place.

Is this a good design?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

怪异←思 2024-09-18 18:51:40

我将删除 uniqueidentifier UserGUID 列并仅使用普通的 int UserID。如果您想让 ID 随机化以进行混淆/混淆,那么只需删除身份并使用一些函数/过程,在每次插入新用户时随机生成一个值在 1 到 2,147,483,647 之间的普通 int。

I would remove the uniqueidentifier UserGUID column and just use a the plain int UserID. If you want to make the IDs random to confuse/obfuscate, then just remove the identity and use some function/procedure that randomly generates a plain int with a value between 1 and 2,147,483,647 every time you INSERT a new user.

染柒℉ 2024-09-18 18:51:39

UserGUID仅存在于主程序中
桌子。仅在应用层
GUID 被解析,而不是 INTS。
这是为了防止竞争对手
猜测我的一些关键数字
类似网站(例如用户数量)。

对我来说似乎很合理。

在与表相关的SPROCS中
UserRatings 和 UserVotes,我必须
DECLARE UserID 并根据
正在解析回来的 UserGUID
在每个程序开始时。

您还可以定义一个涵盖 UserDetails+UserRatings 以及 UserDetails+UserVotes(甚至同时包含所有三个表)的视图,其中包括来自 UserDetails 表。这将使查询表变得更加容易,并且您不必首先从 GUID 中提取 ID 并使用它来查询相关表。

更新:例如,如果您需要频繁查询UserRatings,您可以创建一个如下所示的视图:

CREATE VIEW dbo.UserRatingsWithDetails
AS 
    SELECT 
       ud.UserGuid, ud.Name, ud.Age,
       ur.UserID, ur.Rating, ur.Date
    FROM
       dbo.UserDetails ud
    INNER JOIN
       dbo.UserRatings ur ON ur.UserID = ud.UserID

然后您可以从该视图中进行选择,而无需运行单独的SELECT< /code> 首先:

SELECT UserID, Name, Rating, Date
  FROM dbo.UserRatingsWithDetails
 WHERE UserGuid = @SomeGuidValue

使用正确的索引(在 UserRatings 中的外键字段上),这是一个非常高性能的 JOIN - 不用担心!

The UserGUID only exists in the main
table. In the application layer only
GUID's are parsed around, never INTS.
This is to prevent competitors from
guessing at some key numbers on my
site like (e.g. user count).

Seems quite reasonable to me.

In the SPROCS relating to tables
UserRatings and UserVotes, I have to
DECLARE UserID and SET it based on the
UserGUID that is getting parsed back
at the start of every procedure.

You could also define a view spanning UserDetails+UserRatings, as well as UserDetails+UserVotes (or even all three tables at once), which include the GUID from the UserDetails table. This would make querying the tables easier and you wouldn't have to first extract the ID from the GUID and use that to query the table in question.

Update: if you e.g. need to query UserRatings frequently, you could create a view like this:

CREATE VIEW dbo.UserRatingsWithDetails
AS 
    SELECT 
       ud.UserGuid, ud.Name, ud.Age,
       ur.UserID, ur.Rating, ur.Date
    FROM
       dbo.UserDetails ud
    INNER JOIN
       dbo.UserRatings ur ON ur.UserID = ud.UserID

and then you can select from that view without running a separate SELECT first:

SELECT UserID, Name, Rating, Date
  FROM dbo.UserRatingsWithDetails
 WHERE UserGuid = @SomeGuidValue

With the right indices (on the foreign key field in UserRatings), this is a very highly performant JOIN - no worries here!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文