Comparing a 360 ° PostgreSQL and MySQL

Comparing a 360 ° PostgreSQL and MySQL

First, the original

https://www.enterprisedb.com/blog/postgresql-vs-mysql-360-degree-comparison

Second, summary

In this paper, MySQL and PostgreSQL detailed comparison easy selection.

1, Why PostgreSQL

2. Why use MySQL

3, ease of use

4, Syntax

5, the data type

6, replication and clustering

7 views

8, the flip-flop

9, stored procedures

10, the query

11, partition

12, Table scalability

13, NoSQL capabilities

14, security

15, analysis functions

16, GUI tools

17, performance

18、Adoption

19, the best environment

Three, PG vs MySQL: choose which?

PostgreSQL and MySQL are the most popular open source database. MySQL is considered the world's most popular database, while PostgreSQL is considered the world's most advanced database. MySQL does not fully comply with the SQL standard, and many features are not supported on the PG. That's why a lot of reasons PG by developers like, and now PG increasingly popular.

A few years ago, Oracle acquired MySQL, MySQL result in two versions: Business and Community Edition. For the latter, because the Oracle control of MySQL development, the majority of users criticism.

PostgreSQL is the world's most popular database: He supports a large number of enterprise-class features and functionality. PG postgresql by the global community to develop the community by a group of outstanding development staff, for decades has been trying to ensure that PG has a wealth of features, and compete with other open-source database business. Community has also been a significant contribution from companies around the world.

1, Why PG

PG as a rich open source database function, can compete with Oracle. PG developers will also be used as a NoSQL database. Locally deployed in the cloud and PG is very simple to use, may be used in various internet docker containers.

PG fully supports ACID, developers and DBA very friendly, highly concurrent transactions across any domain, the best choice for complex applications, to meet a variety of applications based on WEB and mobile services. PG is also a very good data warehouse, used to run complex reports on large data queries.

2 , Why MySQL

MySQL has a community and commercial versions. Business Edition is managed by Oracle. As a relational database, deployment and use of very simple. But not suitable for applications requiring very high standard SQL. MySQL integration capabilities are limited, it is difficult to be part of a heterogeneous database environment.

MySQL for simple web applications or require simple schema, use SQL database operations execution. For complex applications for processing large amounts of data, MySQL is not a good choice.

3 , ease of use

PG can handle both structured and unstructured data, with all the characteristics of a relational database. MySQL SQL limitations and characteristics may bring challenges to its RDBMS applications to build efficient.

4 , Syntax

Most database SQL syntax are quite similar. However, MySQL does not support all of the SQL. They are relatively similar support for SQL and other databases. Such as query, PG and MySQL are:

SELECT * FROM employees;

5 , the data type

MySQL and PG supports many data types, from traditional data types (integer, date, timestamp) to complex types (json, xml, text). However, in complex real-time data queries are different.

PG supports more than traditional data types: numeric, strings, date, decimal, etc., but also supports unstructured data types: json, xml, hstore and other types of data networks, 'bit string, and ARRAYS, geographic data types.

MySQL does not support the type of geographic data.

From the beginning of 9.2, PG supports json data types. MySQL is relative, PG json support for the more advanced. He has some json designated operators and functions, json text search is very efficient. 9.4 start, json data may be stored in a binary format, it supports full-text indexing (GIN index) on the column, to perform a quick search in json document.

From the beginning of 5.7, MySQL supports json data types, later than PG. You can also create indexes on json column. However, support functions related to json relatively limited. It does not support full-text index column in json. Due to limitations of MySQL to SQL support, in terms of data storage and processing json, MySQL is not a good choice.

6 , replication and clustering

MySQL and PG have the ability to replicate and cluster to ensure data manipulation level distribution.

MySQL supports master - standby, a replication master multi prepared, i.e. by SQLs binlog guarantee that all data transfer to the standby machine. This is the only replication is asynchronous, semi-synchronous reason.

Advantages: backup machine can write. This means that once the collapse of the master, slave can immediately take over to ensure that the application works correctly. DBAs need to ensure that the slave becomes the master, and the new binlog copied to the original owners. When there are a lot of long SQL, replication will become slower.

MySQL also supports NDB Cluster, namely the multi-master replication mechanism. This type of copy beneficial to the required level extended transaction.

PG different replication and MySQL, he is based WAL file, copy the more reliable, faster, and more conducive to management. He also supported the standby mode and from a plurality of master, replicative form comprises a cascade. PG stream becomes the copy or physical copy replication, synchronization may be asynchronous.

By default, when the asynchronous replication, the Slave can satisfy the read request. If the requirements on the backup host as read data, it is necessary to set the synchronization replication. But the disadvantage is that once prepared machine on matters not submitted, the host will hang live.

You can use third-party tools Slony, Bucardo, Londiste, RubyRep and other table-level replication for archiving. These tools are based replication triggers. PG also supports logical replication. Initially logical extension support replication over pglogical, copied from the 10 start kernel support logic.

7 , view

MySQL supports the view, the view below the number of tables used by SQL is limited to 61. Physical view does not store data, does not support materialized views. View create simple SQL statement can be updated, to create complex SQL view may not update.

PG and MySQL similar. Create a simple SQL view can be updated, not complex. However, complex view can be updated by RULES. PG support materialized views and REFRESHED.

8 , the flip-flop

MySQL support INSERT, UPDATE, DELETE triggers AFTER and BEFORE the event. Different triggers execute dynamic SQL statements and stored procedures.

PG trigger more advanced. Support AFTER, BEFORE, INSTEAD OF trigger event. If you perform a complex SQL when the trigger wake-up can be done by function. PG may be performed dynamically in the trigger function:

CREATE TRIGGER audit

AFTER INSERT OR UPDATE OR DELETE ON employee

    FOR EACH ROW EXECUTE FUNCTION employee_audit_func();

9 , stored procedures

MySQL and PG support stored procedures, but only MySQL supports standard SQL syntax, and support highly sophisticated PG stored procedures. PG stored procedure to complete the functional form with RETURN VOID clause. PG supported languages ​​there are many: Ruby, Perl, Python, TCL, PL / pgSQL, SQL and JavaScript. And MySQL is not so much.

10 , inquiry

Restrictions to consider when using MySQL:

  1. Some UPDATE SQL return value does not meet the standard SQL

mysql> select * from test;

+------+------+

| c | c1   |

+------+------+

|   10 |  100 |

+------+------+

1 row in set (0.01 sec)

 

mysql> update test set c=c+1, c1=c;

Query OK, 1 row affected (0.01 sec)

Rows matched: 1  Changed: 1  Warnings: 0

 

mysql>  select * from test;

+------+------+

| c | c1   |

+------+------+

|   11 |   11 |

+------+------+

1 row in set (0.00 sec)

 

The expected standard form:

mysql>  select * from test;

+------+------+

| c | c1   |

+------+------+

|   11 |   10 |

+------+------+

 

  1. UPDATE, or DELETE statement can not be executed:

mysql> delete from test where c in (select t1.c from test t1, test t2 where t1.c=t2.c);

ERROR 1093 (HY000):

 

  1. Subquery can not use the LIMIT clause

mysql> select * from test where c in (select c from test2 where c<3 limit 1);

ERROR 1235 (42000):

 

MySQL does not support "LIMIT & IN / ALL / ANY / SOME clause." Nor support FULL OUTER JOINS, INTERSECT, EXCEPT and so on. It does not support Partial index, bitmap index, expression indexes. PG support features all the standard SQL. For developers who need to write complex SQL, PG is a good choice.

11 , partition

MySQL and PG support table partitioning, but the two sides have some limitations.

MySQL supports the partition types RANGE, LIST, HASH, KEY and COLUMNS (RANGE and LIST), also supports SUBPARTITIONING. However, when using the DBA may not be easy.

  1. MySQL8.0, only innodb and NDB storage engine supports table partitioning, other storage engine does not support.
  2. If the partition key column is not part of the primary key or unique key, it is impossible to partition the table.
  3. From 5.7.24 start phasing out support for the partition table on the table space, which means that DBA can not balance sheet partitions and disks IO.

mysql> create table emp (id int not null, fname varchar (30), lname varchar(30), store_id int not null ) partition by range (store_id) ( partition p0 values less than (6) tablespace tbs, partition p1 values less than(20) tablespace tbs1, partition p2 values less than (40) tablespace tbs2);

ERROR 1478 (HY000): InnoDB : A partitioned table is not allowed in a shared tablespace.

mysql>

 

PG partition table support and inheritance declaration table partitions. Declaration table partitions 10 was introduced, and MySQL similar, but the partition table inheritance is done by using triggers and rules. Partition types supported by RANGE, LIST, HASH. limit:

  1. And MySQL similar statement can only partition table on the primary key and unique keys
  2. Inheritance partition table, the child table can not inherit the primary key and unique keys.
  3. INSERT and UPDATE are not automatically to Hanson word table.

12 , scalability table

Segment table will cause performance problems become more and more, query on this table takes up more resources, spend more time. MySQL and PG need to consider various factors.

MySQL support B + tree indexes and partitions, which can improve performance on large tables. However, because they do not support the bitmap, partial indexes and functions, the DBA can better tune. And the partition table can not be put on a different table space, which also caused the IO could not be better balanced.

PG index of expression, partial index, bitmap indexing and full-text indexes can improve the performance of large tables. PG partition tables and indexes can be placed on different disks, to better enhance the scalability of the table. To achieve the level of table-level extensions, you can use citusdb, Greenplum, Netezza and so on. PG open source does not support the level of table partitioning, PostgresXC support, but his poor performance.

13 , storage

Data storage is a key capability of the database. PG and MySQL offers a variety of options for storing data.

PG has a common memory characteristics: a space capable of accommodating the table tables, indexes, materialized views and other physical objects. May be performed by the object table stored in the spatial packets and different physical locations, you can enhance the ability IO. Before PG12 version does not support pluggable storage, 12 support pluggable architecture.

MySQL and PG similar, future tables with spatial characteristics. He supports pluggable storage engine. This is an advantage for MySQL.

14 , supports data model

NoSQL relational database capabilities to help deal with unstructured data, such as json, xml, text, etc.

MySQL, NoSQL capacity is relatively limited. 5.7 introduces json data type, it needs a long time to become more mature.

PG has a rich json ability, over the next three years is the need for developers to NoSQL capabilities of a good choice. Json jsonb and data types, so that the operation of PG to json faster and more efficient. The same can be established and the index B-tree indexes on GIN json data column. HSTORE and XML data types and other data in XML format processing complex text format. Support for spatial data types that PG is a complete multi-model database.

15 , security

Database security is not certified to access the database who plays a very important role. Security, including object level and connection level.

MySQL PRIVILEGES through ROLES and the rights to access the database, objects and connections. Each user needs to give access permissions.

GRANT ALL PRIVILEGES ON testdb.* TO 'testuser@'192.168.1.1’ IDENTIFIED BY 'newpassword';

GRANT ALL PRIVILEGES ON testdb.* TO 'testuser@'192.168.1.*’ IDENTIFIED BY 'newpassword';

You need to specify a password each time you empower Otherwise, the user can not connect.

MySQL also supports SSL connections. And external authentication system may PAM and LDAP integration. It is part of its Enterprise Edition.

PG using the GRANT command provides access through ROLES and PRIVILEGES. Connection authentication is relatively simple, set by pg_hba.conf authentication file:

host   database  user  address  auth-method  [md5 or trust or reject]

PG open source version also supports SSL connections, and external authentication systems can be integrated.

A set of analytic functions rows polymerization. There are two types of analytic function: window function and aggregation functions. Aggregate function execution and returns a polymeric aggregate value (sum, avg, min, max, etc.) set of records; analytic function returns the value of each record of the polymerization. MySQL and support multiple PG aggregate functions. MySQL8.0 only support window functions, PG has long supported.

PG support window functions:

Function name          

  description

CUME_DIST

Return the relative rank of the current row.

DENSE_RANK

Rank the current row within its partition without gaps.

FIRST_VALUE

Return a value evaluated against the first row within its partition.

LAG

Return a value evaluated at the row that is at a specified physical offset row before the current row within the partition.

LAST_VALUE

Return a value evaluated against the last row within its partition.

LEAD

Return a value evaluated at the row that is offset rows after the current row within the partition.

NTILE

Divide rows in a partition as equally as possible and assign each row an integer starting from 1 to the argument value.

NTH_VALUE

Return a value evaluated against the nth row in an ordered partition.

PERCENT_RANK

Return the relative rank of the current row (rank-1) / (total rows-1)

RANK

Rank the current row within its partition with gaps.

ROW_NUMBER

Number the current row within its partition starting from 1.

 

PG MySQL supports all windows functions, in addition to the following restrictions:

  1. Window functions can not appear in the UPDATE and DELETE
  2. Window function does not support DISTINCT
  3. Window function does not support NESTED

16 , a graphical interface tool

MySQL has Oracle's SQL Developer, MySQL workbench, dbeaver, omnidb and other monitoring tools have nagios, cacti, zabbix and so on. PG can also use Oracle's SQL Developer, pgAdmin, omnidb, dbeaver. Monitoring tools Nagios, Zabbix, and Cacti.

17 , performance

MySQL database performance tuning options is limited, many index types are not supported. Write an efficient SQL statements challenging. For large-scale data, MySQL is not a good choice. Innodb table space support only, and can not accommodate the partition table.

PG is ideal for any type of load: OLTP, OLAP, data warehouses. As more support index type, you can better improve performance. PG also have the option of collecting memory usage database, the partition table can be placed in different table spaces balance IO.

18Adoption

PG is the world's most advanced open source database. EnterpriseDB and 2ndQuadrant companies to ensure PG is used more users in the world.

MySQL represents the best choice for RDBMS and ORDBMS applications. Ever since the Oracle acquisition of MySQL dependence, MySQL adoption rates decreased, the progress of open source development is also affected, leading to criticism of MySQL users.

19 , the best environment

MySQL is popular LAMP stack, PG popular LAPP stack. LAPP stack on behalf of Linux, Apache, Postgres and Php / Python, and more and more popular. LAMP stack on behalf of Linux Apache MySQL / MongoDB and Php / Python.

发布了272 篇原创文章 · 获赞 82 · 访问量 26万+

Guess you like

Origin blog.csdn.net/yanzongshuai/article/details/104033619