检查多个表中是否存在值

时间:2012-06-14 14:36:44

标签: c# performance entity-framework linq-to-entities

我的数据库中有这样的表格,例如CustomerMemberInstructorEmployee等。每个用户都有自己的电子邮件地址。我需要检查是否已经有给定电子邮件的用户。我在考虑:

  • 检查每个表格,如下所示:
    public bool IsEmailAddressExists(string email)
    {
        if (!Context.Customers.Any(c => string.Equals(c.Email, email, StringComparison.InvariantCultureIgnoreCase)))
            if (!Context.Members.Any(m => string.Equals(m.Email, email, StringComparison.InvariantCultureIgnoreCase)))
              ...
    }
  • 选择所有电子邮件并查看:
    public bool IsEmailAddressExists(string email)
    {
        var emails = Context.Customers.Select(c => c.Email).Union(Context.Members.Select(m => m.Email))...; //other unions
        return emails.Any(e => string.Equals(e, email, StringComparison.InvariantCultureIgnoreCase));
    }

有更多的表和许多用户,所以我想知道实现这种检查的最有效方法是什么。

谢谢。

2 个答案:

答案 0 :(得分:5)

听起来你有几个选择。

创建视图。您可以在数据库中创建一个仅显示电子邮件地址的视图。假设您正在使用MSSQL Server,例如:

CREATE VIEW EmailView AS
SELECT Email from Customers
UNION ALL
SELECT Email from Instructors
....

...然后使用绑定到该视图的实体,以便您可以检查电子邮件列表以查看该电子邮件是否已存在。有关详细信息,请查看documentation

规范化您的数据库。这些表中的每一个都是否共享除电子邮件之外的公共信息,例如,名字和/或姓氏?您可能值得花时间重新组织数据模型以将该信息放在“人员”表中,然后将其他表外键键入其中。 (如果您的用户是两个不同的东西,比如客户和讲师,这也会有所帮助。)

答案 1 :(得分:1)

在纯SQL中,这将是您最有效的,因为它会在匹配时立即停止搜索:

...作为存储过程:

CREATE PROCEDURE EmailExists 
    @email varchar(254) = NULL
AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    DECLARE @emailExists bit

    SET @emailExists = 0

    SELECT @emailExists = 1 WHERE EXISTS(SELECT 1 FROM Customer WHERE email = @email)
    IF @emailExists = 0
    BEGIN
        SELECT @emailExists = 1 WHERE EXISTS(SELECT 1 FROM Member WHERE email = @email)
        IF @emailExists = 0
        BEGIN
            SELECT @emailExists = 1 WHERE EXISTS(SELECT 1 FROM Instructor WHERE email = @email)
            IF @emailExists = 0
            BEGIN
                SELECT @emailExists = 1 WHERE EXISTS(SELECT 1 FROM Employee WHERE email = @email)
            END
        END
    END

    SELECT @emailExists
END

......作为一个标量值函数:

CREATE FUNCTION EmailExists 
(
    @email varchar(254)
)
RETURNS bit
AS
BEGIN
    DECLARE @emailExists bit

    SET @emailExists = 0

    SELECT @emailExists = 1 WHERE EXISTS(SELECT 1 FROM Customer WHERE email = @email)
    IF @emailExists = 0
    BEGIN
        SELECT @emailExists = 1 WHERE EXISTS(SELECT 1 FROM Member WHERE email = @email)
        IF @emailExists = 0
        BEGIN
            SELECT @emailExists = 1 WHERE EXISTS(SELECT 1 FROM Instructor WHERE email = @email)
            IF @emailExists = 0
            BEGIN
                SELECT @emailExists = 1 WHERE EXISTS(SELECT 1 FROM Employee WHERE email = @email)
            END
        END
    END

    -- Return the result of the function
    RETURN @emailExists
END

在使用Linq的C#中,您可以使用Any扩展名和||运营商。由于Any通常在SQL中被翻译为EXISTS并且对||进行了评估C#中的运算符是惰性的,一旦达到电子邮件的第一次发生,评估就会停止。

bool emailExists = customerEmails.Any(e => string.Equals(e, email, StringComparison.InvariantCultureIgnoreCase))
                   || memberEmails.Any(e => string.Equals(e, email, StringComparison.InvariantCultureIgnoreCase))
                   || instructorEmails.Any(e => string.Equals(e, email, StringComparison.InvariantCultureIgnoreCase))
                   || employeeEmails.Any(e => string.Equals(e, email, StringComparison.InvariantCultureIgnoreCase));