135 points · by davidw · 12y ago · 100 comments
I want to convince my boss to use Postgres instead of MySQL in a new project we've just started. Does anybody know about a good comparison of both databases, or a nice list of unique Postgres features? I have been googling a little bit but the findings weren't that good.
- Timestamp with time zone
- More robust, fewer crashes, less corruption of data
- More features (JSON data type, partial indexes, function/expression indexes, window functions, CTEs, hstore, ranges/sequences/sets, materialized views, too many to list)
- More disciplined (doesn't do things like auto-truncate input to get it to fit into a column)
- Not owned by Oracle, it's actively developed, regular major release schedule, developers/maintainers are talented and trustworthy, etc.
- Better Python driver (don't know about other languages)
- Choice of languages for database functions/procedures (Python, JS, etc.)
- Better partitioning support
- Better explain output, explain analyze, buffers
- Multiple indexes allowed per table in a query (I hear MySQL has made a little progress here since I last used it)
THAT, I think, is the biggest sell, at least to me. The fact that PgSQL does so much and so well.
I used Pg for full-text search in the past and the fact that I did not have to bother with setting up Solr or an interface was a wonderful feature. The search index lived right alongside my data.
Yeah, right. Partitioning in PostgreSQL is as braindead as setting sequence value in Oracle. IMHO the devs made wrong choice: https://wiki.postgresql.org/wiki/Table_partitioning#Possible... . You want a partition for every month of stored data? Prepare to copy & paste a lot as you need a set of triggers for all child tables. Oh, the table You are spiting was referenced as foreign keys in other tables? Drop the constraint, it isn't supported even if is the same column you are partitioning over.
I run a Postgres instance that partitions daily (for reasons of space management). When creating the partitions in advance, it is easy enough to alter the trigger (or a function called by the trigger) in the same script that creates the tables.
DDL via copy-edit-paste is just asking for problems anyway. Those operations should be automated.
This blew my mind when I switched from MySQL (which doesn't even support transactions for inserts/updates in most engines).
For example, 'str1' + 'str2' results in '0' and a warning in the default mysql config. A real rdbms should fail and tell you + is not for string concatenation in that system instead of running your query and perhaps overwriting your data and then warning you about it after the fact ('That query you just ran is broken, but we overwrote your data anyways - hey, here's a warning.').
Broken, horrible design.
Auto-increment primary keys get reused when, say, you create a record, then erase it, then pull the power plug. Mysql does not save the highest number used, meaning that you turn on the machine again, add a new record, and it has the same ID as the previous one.
My very subjective impression is that every time I have anything to do with Mysql, sooner or later, some weird thing like that jumps out and bites someone in the ass.
Granted, Mysql is catching up in terms of features like that, but for a long time, many people were using it without them (MYISAM).
Use MySQL when your application is incompatible with other databases. 99 times out of 100, that means that the database has inconsistencies, and the application depends on them, so be carefull when doing that.
Further I've written a couple of posts that don't directly compare to MySQL, but many of the points are pretty relevant for MySQL vs. Postgres
http://www.craigkerstiens.com/2012/04/30/why-postgres/
http://www.craigkerstiens.com/2012/05/07/why-postgres-part-2/I've had issues with Postgres in the past, but it's trustworthy software developed by a trustworthy team.
strict treatment of invalid data
mysql views with aggregates DISTINCT GROUP BY HAVING LIMIT UNION or UNION ALL Subquery in the select list are too slow to be useable
mysql has no check constraints
mysql has no conditional/expression indexes
mysql only has broken geospatial support
mysql has no full text indexing on innodb tables
postgresql has an extensible type system (and booleans!)
postgresql has sequences
postgresql has table inheritance
mysql triggers don't fire on cascaded actions or replication
mysql has no window functions
mysql can't set default values to be a function (there is a workaround for only dates, and only one per table)
postgresql has schemas
postgresql has a transactional DDL (you can roll back an alter table)
rollbacks in mysql are incredibly slow, and if you cancel one it can corrupt the table
mysql limit can't accept variables!?
mysql subqueries are limited: can't modify and select from the same table
mysql stored procedures can't have default args
mysql functions (and thus triggers) can't use prepare/execute, so no dynamic sql
mysql functions can't be called recursively
mysql triggers can't alter the table they are being called against
mysql stored procedures can't be called from dynamic sql (prepare)
mysql can't log error producing queries
mysql slow query log has a resolution of seconds
it has this in recent versions, but it's shockingly bad at matching.
I would love to work building highly scalable systems, but I don't get to do it at my current position, and all the job offers out there require having experience doing it. Looks like a chicken-and-egg problem to me.
Normaly a job is not so specific that you can't start into some path like that, and once you are on the path, you can change positions.
But, if your job is that specific, well, that sucks. You'll have to build something on your free time.
There's this thing I wish existed but AFAIK doesn't: a guide that shows computer people the relative salaries of a bunch of different specialties. So people who have one specialty but would enjoy something else can see what the salaries are like.
Glassdoor is a very rough start on this.
For postgres itself, make sure you know what all the settings in the configuration means, why they make sense 90% of the time and definitely do not make sense in 10% of the time (such as a low memory server with super-fast SSD disk arrays). And in general ofcourse a good knowledge of SQL, index usage/performance. Postgres extensions (arrays, JSON, etc.) are decent, but in my experience it's something you can get into relatively fast if you are solid on the rest.
I've always found this documentation to be great.
If you've worked with RDBMS' before, you can skip around the chapters. If not, I'd read up through chapter 14 and go from there.
You should be able to easily install it on whatever OS you're running.
Play a space battle MMO, but using SQL. The game is also completely open source so if you want to find out how to write an entire application layer in PostgreSQL, it is a pretty good example of that.
http://www.craigkerstiens.com/categories/postgres/
http://postgresguide.com/
http://www.postgresweekly.comHere's a concrete (made-up) example: I want to write a webapp where people would sign up to do some personal tracking. They create variables they are interested in (weight, mood, calory intake, etc.) And then enter their data daily. So for each customer I need to have a different database with different columns, and I want them to be able to add/delete variables "on the fly". Is it very straightforward, and hard to get wrong? Or are there "best practices" for this sort of thing? Thanks.
[1] Generic Data Model: http://c2.com/cgi/wiki?GenericDataModel
1. SaaS tenancy models. This is the data separation.
2. Custom fields would be usually represented as an EAV model (Entity-Attribute-Value).
3. Don't ALTER in production if you can help it. If you're going to do it, use migrations which are scripts which first add columns, then transform data, then reapply constraints.
From what I've seen, applications that use EAV tend to evolve to suffer from bad cases of the "inner platform effect":
http://en.wikipedia.org/wiki/Inner-platform_effect
NB There is nothing "wrong" with using EAV - just that it seems prone to misuse (a bit like XML).
EAV is a sort of inner platform thing I agree, but the correct solution i.e a document store with full field level indexing that works with enterprise loads doesn't exist (yet). CouchDB was promising on that front but didn't go all the way.
XML is fine. Just don't stick it in database columns (my favourite chunk of pain!)
If you really need to separate data by customer then consider using PostgreSQL schemas. But I'd steer away from any solution that involves adding and dropping columns for different customers.
Since primary key is clustered in MySQL, I choose an appropriate key. Is there an equivalent mechanism or is periodically running the cluster command the only option?
That had me laughing.