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

2

u/NotImplemented Feb 22 '17

But as with any query, the best option is to run 'explain' plans on each version and see how the optimizer chooses to execute each, then make your decision based on the generated plan.

This is important! Never try to optimize without checking the query plan and measuring first.