Posts

Showing posts with the label sql

PostgreSQL Practice Series

Image
Introduction PostgreSQL is a powerful open-source relational database management system, developed with many powerful features, high performance and famous for its reliability applied across various diverse projects. The outstanding advantages include: High data integrity and full ACID compliance. Powerful extensibility, allowing users to define their own data types, functions and custom indexes. Support for a wide variety of data types from structured (SQL) to unstructured (JSON/JSONB, XML). A strong development community, continuously updated and optimally secure. Detail Installing PostgreSQL with Docker Explanation of PostgreSQL Operations TOAST Storage Strategies Common PostgreSQL Statements Data Type Numeric Types Character Types Date and Time Types Boolean, Binary and UUID Types Casting Design Schema Normalization and Denormalization Data Integrity OLTP and OLAP Table relationships Aggregate Constraint Index B-Tree B-Tree Index B-Tree Index with order Using Hash Index GIN Index G...

Resolve N+1 Query Problem

Image
Introduction N+1 Query Problem is a performance issue that occurs when an application executes 1 initial query to fetch a list of N records and then executes N additional sub-queries to fetch related data for each record. The total number of queries sent to the Database will be N + 1 . As N increases (for example N = 1000), the application must execute 1001 SQL statements, causing an I/O bottleneck , increasing latency and overloading the Database . Example Suppose you need to display 10 products (N = 10) along with the list of variants for each product: First Query (1): Get the list of 10 products: SELECT * FROM products LIMIT 10 Next N Queries (N = 10): Iterate through each product to get variants. SELECT * FROM product_variants WHERE product_id = 1 ; SELECT * FROM product_variants WHERE product_id = 2 ; ... SELECT * FROM product_variants WHERE product_id = 10 ; Total: 10+1=11 SQL queries. If fetching 1,000 products, the number of queries spikes to 1,001 SQL ...