MySQL vs PostgreSQL? Which should I choose for my Django project?

后端 未结 11 806
臣服心动
臣服心动 2020-11-30 21:27

My Django project is going to be backed by a large database with several hundred thousand entries, and will need to support searching (I\'ll probably end up using djangosear

相关标签:
11条回答
  • 2020-11-30 21:38

    Having gone down the road of MySQL because I was familiar with it (and struggling to find a proper installer and a quick test of the slow web "workbench" interface of postgreSQL put me off), at the end of the project, after a few months after deployment, while looking into back up options, I see you have to pay for MySQL's enterprise back up features. Gotcha right at the very end.

    Wioth MySql I had to write some ugly monster raw SQL queries in Django because no select distinct per group for retrieving the latest per group query. Also looking at postgreSQL's full-text search and wishing I had used postgresSQL.

    I recommend PostgreSQL even if you are familiar with MySQL, but your mileage may vary.

    0 讨论(0)
  • 2020-11-30 21:39

    All the answers bring interesting information to the table, but some are a little outdated, so here's my grain of salt.

    As of 1.7, migrations are now an integral feature of Django. So they documented the main differences that Django developers might want to know beforehand.

    Backend Support

    Migrations are supported on all backends that Django ships with, as well as any third-party backends if they have programmed in support for schema alteration (done via the SchemaEditor class).

    However, some databases are more capable than others when it comes to schema migrations; some of the caveats are covered below.

    PostgreSQL

    PostgreSQL is the most capable of all the databases here in terms of schema support; the only caveat is that adding columns with default values will cause a full rewrite of the table, for a time proportional to its size.

    For this reason, it’s recommended you always create new columns with null=True, as this way they will be added immediately.

    MySQL

    MySQL lacks support for transactions around schema alteration operations, meaning that if a migration fails to apply you will have to manually unpick the changes in order to try again (it’s impossible to roll back to an earlier point).

    In addition, MySQL will fully rewrite tables for almost every schema operation and generally takes a time proportional to the number of rows in the table to add or remove columns. On slower hardware this can be worse than a minute per million rows - adding a few columns to a table with just a few million rows could lock your site up for over ten minutes.

    Finally, MySQL has reasonably small limits on name lengths for columns, tables and indexes, as well as a limit on the combined size of all columns an index covers. This means that indexes that are possible on other backends will fail to be created under MySQL.

    SQLite

    SQLite has very little built-in schema alteration support, and so Django attempts to emulate it by:

    • Creating a new table with the new schema
    • Copying the data across
    • Dropping the old table
    • Renaming the new table to match the original name

    This process generally works well, but it can be slow and occasionally buggy. It is not recommended that you run and migrate SQLite in a production environment unless you are very aware of the risks and its limitations; the support Django ships with is designed to allow developers to use SQLite on their local machines to develop less complex Django projects without the need for a full database.

    0 讨论(0)
  • 2020-11-30 21:41

    large database with several hundred thousand entries,

    This is not large database, it's very small one.

    I'd choose PostgreSQL, because it has a lot more features. Most significant it this case: in PostgreSQL you can use Python as procedural language.

    0 讨论(0)
  • 2020-11-30 21:41

    Will this application be hosted on your own servers or by a hosting company? Make sure that if you are using a hosting company, they support the database of choice.

    0 讨论(0)
  • 2020-11-30 21:43

    For whatever it's worth the the creators of Django recommend PostgreSQL.

    If you're not tied to any legacy system and have the freedom to choose a database back-end, we recommend PostgreSQL, which achives a fine balance between cost, features, speed and stability. (The Definitive Guide to Django, p. 15)

    0 讨论(0)
  • 2020-11-30 21:47

    When a migration fails in django-south, the developers encourage you not to use MySQL:

    ! The South developers regret this has happened, and would
    ! like to gently persuade you to consider a slightly
    ! easier-to-deal-with DBMS (one that supports DDL transactions)
    
    0 讨论(0)
提交回复
热议问题