Do you make these 5 database design mistakes?
thomaslarock.com
thomaslarock.com
Most databases have a lot of built in safety and functionality if you are using the correct datatypes, but that is something you can't benefit from if you are treating everything like a blob or a string.
(I don't keep to this rule for surrogate keys which will be int/bigint auto incrementing, even though I won't be doing any math on the values themselves)
But, if you store a SSN as say 123-45-6789 your using at least 11 bytes to store a 4 byte number. Use it as an index and you waste that space again, store it in a cache and you have less space for useful information, compare it with another SSN and it's slower etc. Now individually it's probably irrelevant but start doing this with several fields on a large application and you can noticeably slow things down even if the extra disk space seems cheap.
PS: 95% of the time it's probably the safest option, but it's still something you should consider based on how that data fit's in with the rest of your application.
Just a couple of problems: some day in the future the field could need another character beyond 0-9. In Spain National Identity Numbers were once just numbers, but they changed and added a letter for validation. If you had stored this as a number you would need to change data types, and suffer a massive application update.
Another problem: if you order a number field you get something like 1, 5, 100, 2000. But if you order a text field you would get 1, 100, 2000, 5. It's not nice to get the "number" order when you are expecting a "text" order.
But the issue is with storing them in numeric datatypes. They aren't guaranteed to be unique forever (and indeed, are alphanumeric in many jurisdictions) and the leading zero problem is a killer when you go to reconstitute them, both for performance and for logic reasons. Same goes for US ZipCodes.
This is why I have to have a very strong technical reason to make a non-math column numeric, especially with externally set data (like SSNs, SINs, Account codes, etc.) The people who set them could just start adding letters or symbols...and this isn't rare.
Plus it gets worse because people inevitably end up writing queries that parse the number with dashes, brackets, or other special delimiters. Those queries are CPU-intensive to do on the fly, and with databases like Oracle, DB2, and SQL Server charging by the CPU core, it becomes an expensive proposition as well.
I've been bitten too many times by the recommendation of using numeric types for [0-9]+ data. Unless it's fundamentally going to involve arithmetic I say leave it as some kind of string type.
In reality, you need to analyse your code and profile your application to see what actually is needed for your indices. Also essential is understanding the overhead that an index creates.
I don't see this as often today, however. I think that in a lot of cases developers put off creating indices of any sort until a performance problem materializes.
Which is perfectly fine.
1. Not having done proper index analysis in the first place. While you can't get it right 100% of the time if you are expecting tables to grow that large you really should think hard about thsi sort of thing as close to the start as practical.
2. Using a database system that can't perform an online index build without locking the whole table. I know that such an operation needs to aquire some locks during its activity no matter what system you use, which will create performance issues for your live site if you are not able to schedule the index change in a pre-planned "maintainence" downtime (i.e. an application that has significant "high availability" requirements), but requiring a full table lock here seems to be a fault in a system that claims to be "enterprise ready".
Space is cheap. What aren't as cheap are memory and I/O bandwidth: using large datatypes limits the size of working-set you can fit into a given amount of memory, and slows down the process of reading data from permenant storage into memory when needed and not already present.
Increasing the load on your I/O capability in this way is far more of a problem than the extra storage space consumed.
The other problem with guids is that if you cluster on a guid, then whenever you do inserts, you're inserting the data into random spots in the table. You end up fragmenting the bejeezus out of the table, doing page splits like crazy. If you defrag the table/indexes, you'll be right back where you started within a few days of doing inserts.
That penalty isn't immediately obvious in small environments, but by the time you're big enough to have performance problems and you call in a consultant, it's going to be an ugly discussion with management. "Hey, wish I could help you, but..." Don't get me wrong, that's not always the biggest bottleneck, but it makes for some awkward discussions.
In almost all the cases that I see GUIDs, they were totally unnecessary for the design. Even the developer who designed them could not give a reason why they needed to be GUIDs. A row unique across the entire universe? Really?
I'm not saying there are no cases...just that in most cases they negatively impact performance with little business or logic gain for that price. All design decisions come down to cost, benefit and risk.
I'm not actually certain as to the implementation.
You need to really understand the data you plan to put in.
However, they do have a formalised mathematical basis and I think you dismiss it too quickly. If you do spend the time to learn the maths then it will push your abilities that little bit further. You'll also likely find it quite easy to pick up given that you already understand the field well.