r/DatabaseHelp • u/NotSeanPlott • 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:
- 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!
View all comments
Show parent comment
1
u/NotImplemented Feb 22 '17 edited Feb 22 '17
Yes, you would need to union them each time. The performance depends on the DBMS and the underlying data. But if you almost always use the list of orders with both CustomerTypes it might be slower than using a single table.
Concerning the efficiency of your initial approaches, see here: https://stackoverflow.com/a/8924965
However, also see a post lower here: https://stackoverflow.com/a/8925783
So if you can find a way to avoid the CASE/ISNULL check in any way, it could be beneficial for some queries.