r/DatabaseHelp • • Feb 21 '17

What is faster? Does it matter?

Hello Again everyone!

I have a question about ways to query: http://imgur.com/a/X1nvt

That links to a example table setup and ERD.

The database stores either the AccountID or ContactID, this validation is done by the application I assume.

Again I'm a noob and I'm curious which approach I should take. I need to access either the Contacts Fullname or the Accounts Name.

I believe have two options, either use:

  1. Case
    When order.CustomerType = 1 then account.name
    When order.customertype = 2 then contact.fullname
    End

2.isnull(account.name, contact.fullname)

Would one approach be more efficient? The benefit I see of option 1 is if the Application's validation fails and there are two values, or more customer types are added.

I'd appreciate your thoughts!

Thanks!

2 Upvotes

6 comments sorted by

View all comments

Show parent comment

1

u/wolf2600 Feb 22 '17

Could you have a case where an Order record has both an AccountID and a ContactID? And the CustomerType is used to determine which value for Name is used?

If you are certain that only one or the other will every be present for a specific order, you could even do:

Order
-----------
AttrName (PK)
CustomerType
AcctContactID

Where the AcctContactID would be either the AcctID or the ContactID, and the CustomerType would indicate which type it is.

But if you're not able to modify the table structure, or if an Order might have both an Account and a Contact, your existing solution will work fine.