Last month I got a chance to optimize some queries of a postgre sql database in my current organization. I was totally new to this database. SQL Server, mySQL, MongoDB, and now, postgres. I never say a 'no' if some work related to database comes to me, and especially, if the database belongs to the RDBMS family, then, I can never say a 'no', as I am always keen to learn new and new trends.
My thought for postgres was wrong earlier. I used to think that it is less popular than mySQL, but, I was wrong. When I started working on it, I found it much better mySQL in some scenarios, like - query optimization(using execution plan and statistics).
Technically, Postgres is "better SQL" as it is more standardized. With better query planning, so that you can follow the relational model more closely if it suits you. Socially, MySQL has been very popular for a long time and many people are familiar with it. Postgres is neither owned by a major conglomerate with a questionable track record nor forked into half a dozen broadly similar projects.
Some differences between mySql and Postgres I consolidated from Quora , can be read from the following link :
By the way, Wiki says : PostgreSQL is developed by the PostgreSQL Global Development Group, a diverse group of many companies and individual contributors. It is free and open-source software, released under the terms of the PostgreSQL License, a permissive free-software license. Postgres, is an object-relational database (ORDBMS) – i.e. a RDBMS, with additional "object" features – with an emphasis on extensibility and standards compliance; on macOS, PostgreSQL is the default database – for web hosting and it is also available for Microsoft Windows and Linux. PostgreSQL is ACID-compliant and transactional. PostgreSQL has updatable views and materialized views, triggers, foreign keys. Supports functions and stored procedures, and other expandability.
Postgres execution plan is more detailed and deeper than mysql. See the below execution plan (Explain output) of a query.
Limit (cost=68.72..68.84 rows=50 width=44)
-> Sort (cost=68.72..69.28 rows=226 width=44)
Sort Key: tq.TQ
-> Hash Left Join (cost=35.26..61.21 rows=226 width=44)
Hash Cond: (p.Id = tq.PlayerId)
-> Hash Left Join (cost=26.66..48.37 rows=226 width=40)
Hash Cond: (p.Id = f2.PlayerId)
Join Filter: (f.FundingLineId = 2)
-> Hash Left Join (cost=17.85..35.89 rows=226 width=40)
Hash Cond: (p.Id = f.PlayerId)
-> Hash Left Join (cost=8.40..23.33 rows=226 width=32)
Hash Cond: (p.CountryId = c.Id)
Join Filter: (p.CountryId > 0)
-> Seq Scan on players p (cost=0.00..11.26 rows=226 width=33)
-> Hash (cost=5.40..5.40 rows=240 width=7)
-> Seq Scan on country c (cost=0.00..5.40 rows=240 width=7)
-> Hash (cost=6.22..6.22 rows=258 width=12)
-> Seq Scan on funds f (cost=0.00..6.22 rows=258 width=12)
Filter: (FundingLineId = 1)
-> Hash (cost=5.58..5.58 rows=258 width=8)
-> Seq Scan on funds f2 (cost=0.00..5.58 rows=258 width=8)
-> Hash (cost=5.49..5.49 rows=249 width=8)
-> Seq Scan on playertq tq (cost=0.00..5.49 rows=249 width=8)
Online tool to understand execution plan metadata in described … https://explain.depesz.com/
Even postgres admin also given GUI like sql server for understanding there plan.
Thanks for reading
My thought for postgres was wrong earlier. I used to think that it is less popular than mySQL, but, I was wrong. When I started working on it, I found it much better mySQL in some scenarios, like - query optimization(using execution plan and statistics).
Technically, Postgres is "better SQL" as it is more standardized. With better query planning, so that you can follow the relational model more closely if it suits you. Socially, MySQL has been very popular for a long time and many people are familiar with it. Postgres is neither owned by a major conglomerate with a questionable track record nor forked into half a dozen broadly similar projects.
Some differences between mySql and Postgres I consolidated from Quora , can be read from the following link :
By the way, Wiki says : PostgreSQL is developed by the PostgreSQL Global Development Group, a diverse group of many companies and individual contributors. It is free and open-source software, released under the terms of the PostgreSQL License, a permissive free-software license. Postgres, is an object-relational database (ORDBMS) – i.e. a RDBMS, with additional "object" features – with an emphasis on extensibility and standards compliance; on macOS, PostgreSQL is the default database – for web hosting and it is also available for Microsoft Windows and Linux. PostgreSQL is ACID-compliant and transactional. PostgreSQL has updatable views and materialized views, triggers, foreign keys. Supports functions and stored procedures, and other expandability.
Postgres execution plan is more detailed and deeper than mysql. See the below execution plan (Explain output) of a query.
Limit (cost=68.72..68.84 rows=50 width=44)
-> Sort (cost=68.72..69.28 rows=226 width=44)
Sort Key: tq.TQ
-> Hash Left Join (cost=35.26..61.21 rows=226 width=44)
Hash Cond: (p.Id = tq.PlayerId)
-> Hash Left Join (cost=26.66..48.37 rows=226 width=40)
Hash Cond: (p.Id = f2.PlayerId)
Join Filter: (f.FundingLineId = 2)
-> Hash Left Join (cost=17.85..35.89 rows=226 width=40)
Hash Cond: (p.Id = f.PlayerId)
-> Hash Left Join (cost=8.40..23.33 rows=226 width=32)
Hash Cond: (p.CountryId = c.Id)
Join Filter: (p.CountryId > 0)
-> Seq Scan on players p (cost=0.00..11.26 rows=226 width=33)
-> Hash (cost=5.40..5.40 rows=240 width=7)
-> Seq Scan on country c (cost=0.00..5.40 rows=240 width=7)
-> Hash (cost=6.22..6.22 rows=258 width=12)
-> Seq Scan on funds f (cost=0.00..6.22 rows=258 width=12)
Filter: (FundingLineId = 1)
-> Hash (cost=5.58..5.58 rows=258 width=8)
-> Seq Scan on funds f2 (cost=0.00..5.58 rows=258 width=8)
-> Hash (cost=5.49..5.49 rows=249 width=8)
-> Seq Scan on playertq tq (cost=0.00..5.49 rows=249 width=8)
Online tool to understand execution plan metadata in described … https://explain.depesz.com/
Even postgres admin also given GUI like sql server for understanding there plan.
Thanks for reading
Thank you so much it is actually a very nice blog written to provide adequate information about Postgre and SQL and its related aspects.
ReplyDeleteSSIS Postgresql Write
Thank you for putting up a descriptive post on a very useful aspect of SQL. This actually helped me a lot to understand this topic.
ReplyDeleteSSIS PostgreSql Write