-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Commit from GitHub Actions (Generate Post)
- Loading branch information
1 parent
2f4ad9b
commit ab6fa25
Showing
3 changed files
with
174 additions
and
1 deletion.
There are no files selected for viewing
75 changes: 75 additions & 0 deletions
75
...ent/posts/deep-dive-into-postgres-and-jsonb:-advanced-performance-techniques.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
--- | ||
title: "Deep Dive Into Postgres and JSONB: Advanced Performance Techniques" | ||
date: 2023-09-13T01:26:26.332Z | ||
tags: ["postgresql","jsonb","database optimization"] | ||
authors: ["gpt-4"] | ||
--- | ||
|
||
|
||
PostgreSQL has long been recognized for its robustness and SQL compliance. Over the years, it has introduced many features more commonly found in document databases - one such great feature is JSONB, a binary representation of JSON-like data. This post will dive deep into JSONB, discussing its benefits, indexing systems, and techniques to boost its performance. | ||
|
||
|
||
## What is JSONB? | ||
|
||
JSONB is a binary representation of JSON-like data. It offers storage and retrieval of documents that can be queried like regular SQL relations but are also kept in a binary format for efficient storage. | ||
|
||
```sql | ||
CREATE TABLE users ( | ||
id SERIAL PRIMARY KEY, | ||
details JSONB NOT NULL | ||
); | ||
``` | ||
|
||
In the code snippet above, a `users` table has been created with a JSONB column `details`. The JSONB data type means you can flexibly insert any user-related data. | ||
|
||
|
||
## Benefits of JSONB | ||
|
||
JSONB provides a couple of distinct advantages: | ||
|
||
- **Structural validity**: You cannot insert a JSONB value that is not valid. It also guarantees that serialization will produce text that, when deserialized, will result in an equivalent value. | ||
- **Support for indexing**: JSONB columns can also be indexed, making search operations efficient. | ||
|
||
|
||
## Indexing in JSONB | ||
|
||
Understanding indexing is crucial for performance optimization. PostgreSQL supports two types of indexes useful with JSONB - GIN (Generalized Inverted Index) and GiST (Generalized Search Tree). | ||
|
||
```sql | ||
CREATE INDEX idxgin ON users USING gin (details); | ||
``` | ||
|
||
In the snippet above, a GIN index is used as they are best and faster for searches, containment, existence operators, and key text search. | ||
|
||
|
||
## Querying JSONB Data | ||
|
||
PostgreSQL has built-in operators and functions to query JSONB data. | ||
|
||
```sql | ||
-- To find users with name Derek | ||
SELECT * FROM users WHERE details @> '{"name": "Derek"}'; | ||
|
||
-- To get users with specific keys | ||
SELECT * FROM users WHERE details ? 'name'; | ||
|
||
-- To extract JSON in text form | ||
SELECT details->'name' FROM users; | ||
``` | ||
|
||
|
||
## Performance Optimization Techniques | ||
|
||
- **Use the right Index**: Depending on the operations performed on JSONB, choose the appropriate index. GIN is quicker for lookups, and GiST is faster for composite queries. | ||
- **Use `jsonb_path_ops`**: The `jsonb_path_ops` can be used in place of the default GIN operator. It only supports the containment operator but requires less disk space and is faster to build. | ||
|
||
```sql | ||
CREATE INDEX idxgin ON users USING gin (details jsonb_path_ops); | ||
``` | ||
|
||
- **Keep a normalized database design**: As tempting as it could be to throw everything into JSONB columns, keep your schema relational for data that won't benefit from JSONB. | ||
|
||
|
||
## Conclusion | ||
|
||
PostgreSQL's JSONB offers the best of both worlds - the flexibility of JSON with the rigor of SQL. However, as with any potent tool, understanding how to use it effectively is key. With the right indexing strategy and use cases, you can maximize the performance and utility of JSONB within PostgreSQL. |
86 changes: 86 additions & 0 deletions
86
.../data/completions/deep-dive-into-postgres-and-jsonb:-advanced-performance-techniques.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
{ | ||
"model": "gpt-4", | ||
"prompt": [ | ||
{ | ||
"role": "user", | ||
"content": "You are the author of a personal technical blog about software engineering and computer science subjects." | ||
}, | ||
{ | ||
"role": "user", | ||
"content": "New topics for blog posts should be based off of the technical skills, experience, and interests presented in the following resume:\n+++\ntitle = \"Résumé\"\ndescription = \"Derek Cuevas' Résumé\"\ndate = \"2023-06-11\"\nor = \"Derek Cuevas\"\n+++\n\n# Derek Cuevas\n\n[GitHub](https://github.com/derekcuevas) (contact email in bio) \n\n---\n\n## Summary\n\nSenior Software Engineer with 8 years of professional experience, specializing in systems software engineering and low-latency, real-time product development.\n\nExperienced in leading teams and delivering Emmy award-winning projects. Passionate about functional programming and building high performance real-time systems. Skilled in a variety of programming languages and technologies with expertise in [Rust](https://www.rust-lang.org/) and [TypeScript](https://www.typescriptlang.org/).\n\n---\n\n## Programming Languages\n\n- **Proficient:** Rust, TypeScript (& JavaScript), Clojure, Elm\n- **Experienced:** Swift, Elixir, Objective-C, SQL\n\n---\n\n## Technologies, Skills, and Interests\n\n- **Rust:** Tokio, Futures, Hyper, Actix, Warp\n- **Platforms and Services:** AWS, Docker, PostgreSQL, Redis, Apache Pulsar, RabbitMQ, Prometheus, GitLab CI/CD, Node.js, Deno, HTML & CSS\n- **Tooling:** Git, Vim, Bash\n- **Frameworks:** React, React Native, Redux, RxJs, SwiftUI, Phoenix\n- **Protocols:** HTTP, GraphQL, WebSockets, ØMQ, Pub/Sub, JSON, MessagePack, Protobuf, FlatBuffers\n- **Paradigms:** Functional Programming, Real-time Systems\n\n---\n\n## Professional Experience\n\n### Senior Software Engineer II (Lead), Second Spectrum (acquired by Genius Sports)\n(*January 2021 - Current*)\n\n[https://geniussports.com/](https://geniussports.com/)\n\n- Technical lead for the \"Real-time Augmentation Engine\" project's system team, developing low-latency and real-time augmented reality experiences for live TV broadcasters of sporting events.\n- Led the development and integration of core systems using `Rust`, `Tokio`, `MPSC channels`, `FFmpeg`, and `Pulsar`.\n- Oversaw operational management and monitoring systems, release management, simulation systems, and data ingestion/export pipelines.\n- Optimized the overall pipeline for 60fps real-time rendering over high definition broadcast feeds.\n- Key contributor to projects recognized with multiple Emmy awards, including the prestigious [George Wensel Technical Achievement Award](https://geniussports.com/customer-stories/cbs-and-genius-win-sports-emmy-for-romovision/) and Amazon's [Outstanding Interactive Experience](https://www.aboutamazon.com/news/entertainment/thursday-night-football-on-prime-video-wins-sports-emmy-award) at the `Sports Emmys`.\n\n### Senior Software Engineer I (Lead), Second Spectrum\n(*January 2019 - January 2021*)\n\n[https://www.secondspectrum.com/](https://www.secondspectrum.com/)\n\n- Led a team in designing and developing the company's first `GraphQL` service serving as a unified gateway for various microservices. Built upon the `Apollo` server framework leveraging `Node.js`, `TypeScript`, `PostgreSQL`, and `Redis` for caching and pub/sub.\n- Developed a dashboard built with `Three.js` for visualizing 3D player tracking datasets and highlighting data discrepancies.\n- Created a prototype `iOS` application for video highlights using `Swift`, `SwiftUI`, and the `Combine` framework.\n- Mentored interns and fellow engineers, fostering collaboration and knowledge sharing.\n\n### Software Engineer, Second Spectrum\n(*August 2016 - January 2019*)\n\n[https://www.secondspectrum.com/](https://www.secondspectrum.com/)\n\n- Developed the operational and quality assurance management application for the company's optical player tracking system. Stack included `Elixir` (with the `Phoenix` framework), `React`, and heavy use of `WebSockets` to sync and display corrections to operators in near real-time.\n- Improved operator efficiency by 100% through the implementation of an automated task selection system.\n- Led the development of the LA Clipper's live streaming web and mobile application \"Clippers CourtVision\". Stack included `Elm` for the web front-end and `React Native` for the mobile app.\n\n### Software Engineer Intern, MindGraph Medical\n(*September 2015 - January 2016*)\n\n- Designed, developed, and deployed the MindGraph Medical website using `Node.js`, `jQuery`, and `Bootstrap`.\n\n---\n\n## Education\n\n**B.S. Computer Science, San Diego State University, San Diego**\n\n(*Class of 2016*)\n\n- [Advanced Certificate in Web and Mobile Applications Development](https://cs.sdsu.edu/about/)\n\n---\n\n## Personal Projects\n\n- [FYGT](https://github.com/DerekCuevas/derekcuevas.github.io): An experimental blog built with `Hugo`, `Deno`, and `ChatGPT`.\n- [Advent of Code 2022](https://github.com/DerekCuevas/advent-of-code-2022): Solutions to Advent of Code programming problems using functional approaches in `Clojure`.\n- [Interview Cake Algorithms Problem Set](https://github.com/DerekCuevas/interview-cake-clj): Solutions to the interview cake programming problems also by exploring functional approaches in `Clojure`.\n- [Friend List](https://github.com/DerekCuevas/friend-list): A non-trivial programming challenge exploring asynchronous side effects with different programming patterns.\n- [Redux Meta Reducer](https://github.com/DerekCuevas/redux-meta-reducer): A `Redux` higher-order reducer for simplifying the state of fetched data.\n- [Avowal](https://github.com/DerekCuevas/avowal): An asynchronous form validation library for `JavaScript`.\n" | ||
}, | ||
{ | ||
"role": "user", | ||
"content": "Your previous 60 posts include: \"Exploring Elm Architecture: Building a SPA with Elm\", \"Exploring Functional Programming with Rust: An Introduction to Frunk\", \"Streamlining High-Performance Concurrency with Rust's Mio Crate\", \"Implementing A Concurrency Safe CRDT Set in Rust\", \"Leveraging Actix for High-Performance Web Services in Rust\", \"Mastering Dependency Injection in Rust: A Comprehensive Guide\", \"Demystifying the World of NATS Messaging System\", \"Mastering Lenses in Swift: A Comprehensive Guide\", \"Deep Dive into Pulsar's Schema Registry\", \"Mastering the Decorator Pattern in TypeScript: Best Practices and Examples\", \"Exploring the Benefits of Declarative Programming in React\", \"Understanding CQRS and Event Sourcing: A Comprehensive Guide\", \"Mastering Apache Flink for Stream Processing\", \"Demystifying Rust's Ownership Model: A Comprehensive Guide\", \"Mastering Java Generics: A Comprehensive Guide\", \"Mastering Reactive Programming with RxJS: Advanced Techniques\", \"A Comprehensive Guide to Threading in Rust\", \"Mastering the Observer Pattern with Rust\", \"Exploring the Power of Rust's Ownership Model: A Comprehensive Guide\", \"The Power of Rust's Clippy: A Comprehensive Guide\", \"Exploring the Disadvantages of Promises in JavaScript\", \"Exploring the Fundamentals of Tail-Call Optimization in JavaScript\", \"Mastering Distributed Systems with Apache BookKeeper\", \"Exploring the Power of Rust's Type System: A Comprehensive Guide\", \"Mastering Asynchronous Event-Driven Architecture with Apache Kafka\", \"Advanced Techniques for Distributed System Design using Apache Cassandra\", \"Exploring the Underlying Architecture of TypeScript\", \"Functional Reactive Programming with Elm: A Comprehensive Guide\", \"Exploring the Power of Clojure's Persistent Data Structures\", \"The Power of Rust's Multithreading Library: rayon\", \"The Power of the `async` Keyword in Rust: A Comprehensive Guide\", \"Mastering Cache Invalidation Strategies: A Comprehensive Guide\", \"Mastering Rust's Serialization and Deserialization: A Comprehensive Guide\", \"Mastering AWS S3 for High Availability: A Comprehensive Guide\", \"Exploring the Fundamentals of Reactive Programming\", \"Mastering Rust's Pinning: An Advanced Guide\", \"Understanding Higher Order Functions in TypeScript\", \"Mastering Async/Await in TypeScript: A Comprehensive Guide\", \"Building High Availability Systems with Apache ZooKeeper\", \"Mastering Rust's Unsafe Keyword: A Comprehensive Guide\", \"The Power of Immutable Data Structures in JavaScript\", \"Building a Highly Available System with Apache ZooKeeper\", \"Mastering Persistent Connections in Java\", \"Mastering Real-Time Communication with WebSockets in Elixir\", \"Understanding the Reactive Stream Pattern\", \"Understanding the Power of Rust's Macros\", \"Mastering Rust's Futures: An Advanced Guide\", \"Advanced Techniques for Working with Rust's Borrow Checker\", \"Understanding the Difference Between Block and Inline Elements in HTML\", \"Mastering Kafka Connect: A Comprehensive Guide\", \"Effective Error Handling in Swift: Best Practices and Patterns\", \"The Power of Kotlin's Coroutines: A Comprehensive Guide\", \"Mastering Asynchronous Programming in Rust: A Comprehensive Guide\", \"Building Custom Collectors in Java: A Comprehensive Guide\", \"The Art of Domain-Driven Design\", \"Exploring the Power of React's Context API: A Comprehensive Guide\", \"Mastering the Observer Pattern with Swift\", \"Mastering the Actor Model with Elixir\", \"Mastering Rust's Memory Safety Paradigm\", \"Mastering Quicksort Algorithm in Clojure\"" | ||
}, | ||
{ | ||
"role": "user", | ||
"content": "Come up with an advanced topic for an expert level reader that is about a particular single feature, fact, pattern, paradigm, convention, theory, framework, library, or best practice." | ||
}, | ||
{ | ||
"role": "user", | ||
"content": "Write a post about the chosen topic following the instructions given below:" | ||
}, | ||
{ | ||
"role": "user", | ||
"content": "Your writing style is academic, informative, and focused on detailed technical information." | ||
}, | ||
{ | ||
"role": "user", | ||
"content": "The topic should not be related to a previous post." | ||
}, | ||
{ | ||
"role": "user", | ||
"content": "Use computer code snippets (in the relevant programming language) in order to illustrate the information presented." | ||
}, | ||
{ | ||
"role": "user", | ||
"content": "Format the body of the post in the markdown markup language." | ||
}, | ||
{ | ||
"role": "user", | ||
"content": "Come up with a title for the post and include it on the first line." | ||
}, | ||
{ | ||
"role": "user", | ||
"content": "Include one to three tags for the post on the second line formatted as a comma separated list, for example: \"Tags: <tag1>, <tag2>\"." | ||
}, | ||
{ | ||
"role": "user", | ||
"content": "The post should be multiple sections in length." | ||
}, | ||
{ | ||
"role": "user", | ||
"content": "Do not include links to images in the post." | ||
}, | ||
{ | ||
"role": "user", | ||
"content": "Do not include contact information in the post." | ||
}, | ||
{ | ||
"role": "user", | ||
"content": "Do not add extra whitespace around lists." | ||
} | ||
], | ||
"completion": { | ||
"id": "chatcmpl-7y8vFE9DLw2hW4gLssqpZbkHRhwCQ", | ||
"object": "chat.completion", | ||
"created": 1694568333, | ||
"model": "gpt-4-0613", | ||
"choices": [ | ||
{ | ||
"index": 0, | ||
"message": { | ||
"role": "assistant", | ||
"content": "# Deep Dive Into Postgres and JSONB: Advanced Performance Techniques\nTags: PostgreSQL, JSONB, Database Optimization\n\n---\n\nPostgreSQL has long been recognized for its robustness and SQL compliance. Over the years, it has introduced many features more commonly found in document databases - one such great feature is JSONB, a binary representation of JSON-like data. This post will dive deep into JSONB, discussing its benefits, indexing systems, and techniques to boost its performance.\n\n---\n\n## What is JSONB?\n\nJSONB is a binary representation of JSON-like data. It offers storage and retrieval of documents that can be queried like regular SQL relations but are also kept in a binary format for efficient storage.\n\n```sql\nCREATE TABLE users (\n id SERIAL PRIMARY KEY,\n details JSONB NOT NULL\n);\n```\n\nIn the code snippet above, a `users` table has been created with a JSONB column `details`. The JSONB data type means you can flexibly insert any user-related data.\n\n---\n\n## Benefits of JSONB\n\nJSONB provides a couple of distinct advantages:\n\n- **Structural validity**: You cannot insert a JSONB value that is not valid. It also guarantees that serialization will produce text that, when deserialized, will result in an equivalent value.\n- **Support for indexing**: JSONB columns can also be indexed, making search operations efficient.\n\n---\n\n## Indexing in JSONB\n\nUnderstanding indexing is crucial for performance optimization. PostgreSQL supports two types of indexes useful with JSONB - GIN (Generalized Inverted Index) and GiST (Generalized Search Tree).\n\n```sql\nCREATE INDEX idxgin ON users USING gin (details);\n```\n\nIn the snippet above, a GIN index is used as they are best and faster for searches, containment, existence operators, and key text search. \n\n---\n\n## Querying JSONB Data\n\nPostgreSQL has built-in operators and functions to query JSONB data.\n\n```sql\n-- To find users with name Derek\nSELECT * FROM users WHERE details @> '{\"name\": \"Derek\"}';\n\n-- To get users with specific keys\nSELECT * FROM users WHERE details ? 'name';\n\n-- To extract JSON in text form\nSELECT details->'name' FROM users;\n```\n\n---\n\n## Performance Optimization Techniques\n\n- **Use the right Index**: Depending on the operations performed on JSONB, choose the appropriate index. GIN is quicker for lookups, and GiST is faster for composite queries.\n- **Use `jsonb_path_ops`**: The `jsonb_path_ops` can be used in place of the default GIN operator. It only supports the containment operator but requires less disk space and is faster to build.\n\n```sql\nCREATE INDEX idxgin ON users USING gin (details jsonb_path_ops);\n```\n\n- **Keep a normalized database design**: As tempting as it could be to throw everything into JSONB columns, keep your schema relational for data that won't benefit from JSONB.\n\n---\n\n## Conclusion\n\nPostgreSQL's JSONB offers the best of both worlds - the flexibility of JSON with the rigor of SQL. However, as with any potent tool, understanding how to use it effectively is key. With the right indexing strategy and use cases, you can maximize the performance and utility of JSONB within PostgreSQL." | ||
}, | ||
"finish_reason": "stop" | ||
} | ||
], | ||
"usage": { | ||
"prompt_tokens": 2231, | ||
"completion_tokens": 663, | ||
"total_tokens": 2894 | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters