by combatentropy · 9y ago · 0 comments
There were three problems with having the rule in Rails:
1. The need for an index was easily overlooked.
2. The rule would be bypassed if a different app used the same database.
3. The rule wasn't even foolproof. Only a database constraint would guarantee uniqueness when two users are saving at the same instant.
The problem is, SQL is hard. We should not forget how much a programmer must learn. For example: Ruby, Rails, Linux command line, HTML, CSS, JavaScript, vi, how to exit vi, etc. Each of these takes years to master.
SQL is especially SQuirreLy. However, it can't possibly be worse than learning the myriad JavaScript frameworks and complicated server-build tools that are completely optional for 99% of us. My advice: Don't do a SPA. Spend your time on SQL instead :D
If you didn't want to write SQL and use constraints, then why would you even use a database (over just plain files)? That's what they are designed for. RDBMS developers are probably all sitting around giggling (nihilistically) over experiences with users who think their database is slow on account of their failure to actually use it in the way it was optimized to be used.
This problematic state of affairs is only compounded by the fact that ORMs don’t properly and easily handle taking constraint, validation, and other data-focused application logic and actually writing it into the schema (some ORMs are better/worse at this than others, of course). They all seem to just default to letting as much of it be application-level code as possible. This then leaves new developers with the impression this is where such code belongs, so they never experience anything telling them it should be anywhere else. It’d be great to see more education offered within ORM docs, but the really important stuff seems to often be there only if you already know what you’re looking for. I’ve seen so many models defined over the years without indexes on the most obvious columns that are queried a bajillion times. Or maybe you can set a unique constraint on certain fields without a bunch of effort—sqlalchemy is decent at this in the Python world—but then you have to have unique validators for forms, rather than relying on the db to complain about a violation of the the constraint, and the ORM catching it in a friendly way devs can bubble up when needed. Oh, and then some insist on adding client-side validation of that same constraint. So, you take a constraint that matters at the db level, and is already easily handled at the db level, and people are writing client and server validation to enforce a condition the db is already supposed to be enforcing.
There’s madness here. Maybe it’s just me, though.
At the far end, any programming logic you put in the DB will be impossible to scale if needed. You can only have one DB, but any number of Rails servers.
Database constraints are a matter of preference and skill set. They make some things much harder when you have to delete things is certain convoluted orders, and I can get the correctness through tests. Your decisions may differ. That's fine.
You should always put indexes on things you query by. Be vigilant. This is the one case where "only optimize when you have a performance problem" rule doesn't apply.
> any programming logic you put in the DB will be impossible to scale if needed.
> You can only have one DB, but any number of Rails servers.
The difference in processing time between a database with no rules and one with all of them is usually negligible. > You should always put indexes on things you query by
I have put indexes on things, and PostgreSQL still decided it was more efficient to do a sequential scan. There are cases where they don't make a difference ("low cardinality" or something like that). Also, indexes slow down writes and eat disk space. Usually each is just a little. But it can add up sometimes.The advice I see most is to run EXPLAIN and judge from that. Even after you've added the index and run ANALYZE, run EXPLAIN again to make sure that the database is using the index and that it makes a difference.
Sometimes it's better not to index, if you have a table that is updated often and queried in varying, complex, rarely repeated ways.
Special cases though, usually write-only analytic tables that are queried once in a blue moon and have incredibly high traffic (~millions of rows per hour).
I wholeheartedly agree that developers learn SQL way too little and way too late (I blame ORM/AR overuse early on) but SPA/SQL are possibly the least mutually exclusive issues I can imagine.
This was learned the hard way: creative sales people manipulating sales data (via ODBC & Excel on NT3.51 no less). Since all logic was in the applications, they had a field day when they bypassed those.
This was 1996. Not much has changed.
Off topic: learning to exit vi takes "years to master"?
Joke aside, the basics of SQL can be learned in a very short time. Before fancy ORMs all novice programmers managed to get by using SQL in big-ridden PHP projects ;). I for example, got my introduction from querying a national database leak and than writing a simple blog with tricks I've learned from sqlzoo.