SQL Practice Logo

SQLPractice Online

Real-World Database Selection: Examples

Module: Database-Specific Features

E-commerce Platform: PostgreSQL vs MySQL Decision

advanced

You are building an e-commerce platform with 100K products, 1M users, 10K orders/day. Requirements: (1) ACID transactions for orders/payments, (2) Complex queries for product search/filtering, (3) JSON data for product attributes, (4) Real-time inventory updates, (5) Analytics for sales reports. Budget: $500/month for database hosting. Team: 3 developers familiar with MySQL. Question: PostgreSQL or MySQL?

-- Requirement Analysis:

-- 1. ACID Transactions (Both support)

-- PostgreSQL: Full ACID with MVCC (non-blocking reads)

BEGIN;

INSERT INTO orders (user_id, total) VALUES (123, 99.99);

INSERT INTO order_items (order_id, product_id, quantity)

VALUES (LASTVAL(), 456, 2);

UPDATE products SET stock = stock - 2 WHERE product_id = 456;

COMMIT;

-- Non-blocking: Other users can read products table during transaction

-- MySQL: Full ACID with InnoDB (row-level locking)

START TRANSACTION;

INSERT INTO orders (user_id, total) VALUES (123, 99.99);

INSERT INTO order_items (order_id, product_id, quantity)

VALUES (LAST_INSERT_ID(), 456, 2);

UPDATE products SET stock = stock - 2 WHERE product_id = 456;

COMMIT;

-- Blocking: Other users wait for lock on products table row

-- 2. Complex Product Search (PostgreSQL advantage)

-- PostgreSQL: Full-text search + JSONB

SELECT product_id, name, price, attributes

FROM products

WHERE

to_tsvector('english', name || ' ' || description)

@@ plainto_tsquery('english', 'wireless headphones')

AND attributes->>'brand' = 'Sony'

AND (attributes->>'color')::jsonb ?| ARRAY['black', 'white']

AND price BETWEEN 50 AND 200

ORDER BY ts_rank(to_tsvector('english', name),

plainto_tsquery('english', 'wireless headphones')) DESC

LIMIT 20;

-- Fast: GIN index on tsvector, JSONB operators

-- MySQL: Basic search + JSON (slower)

SELECT product_id, name, price, attributes

FROM products

WHERE

MATCH(name, description) AGAINST('wireless headphones' IN NATURAL LANGUAGE MODE)