Complex operations and patterns for advanced use cases.
Managing multiple database connections and switching between them.
Topics covered:
- Creating connections without default connection
- Adding multiple named connections
- Switching between connections with
connection() - Managing connections to different database types
- Connection reuse and cleanup
- Multi-database applications
Bulk inserts and data loading from files.
Topics covered:
- Bulk inserts with
insertMulti() - Loading data from CSV files with
loadFromCsv() - Loading data from XML files with
loadFromXml() - Batch size configuration
- Performance optimization for large datasets
- Error handling in bulk operations
Note: For JSON loading, see 05-file-loading/.
INSERT or UPDATE operations (UPSERT).
Topics covered:
- MySQL REPLACE operations
- INSERT ON DUPLICATE KEY UPDATE (MySQL)
- INSERT ON CONFLICT (PostgreSQL)
- INSERT OR REPLACE (SQLite)
- Dialect-specific upsert strategies
- Handling unique constraints
- Batch upserts
Subqueries in SELECT, WHERE, and FROM clauses.
Topics covered:
- Scalar subqueries in SELECT
- Subqueries in WHERE conditions
- EXISTS and NOT EXISTS
- Subqueries with IN operator
- Subqueries in FROM clause (derived tables)
- Correlated subqueries
- Multiple subqueries in one query
- QueryBuilder subquery support
MERGE statement operations (INSERT/UPDATE/DELETE based on match conditions).
Topics covered:
- MERGE with table source
- MERGE with subquery source
- MERGE with QueryBuilder source
- WHEN MATCHED clause (UPDATE operations)
- WHEN NOT MATCHED clause (INSERT operations)
- PostgreSQL native MERGE support
- MySQL emulation via INSERT ... ON DUPLICATE KEY UPDATE
- SQLite emulation via INSERT OR REPLACE
- Synchronizing data between tables
- Dialect-specific MERGE implementations
SQL Formatter for pretty-printing SQL queries during debugging.
Topics covered:
- Unformatted SQL output (default behavior)
- Formatted SQL with proper indentation and line breaks
- Complex queries with JOINs, GROUP BY, ORDER BY
- Multiple WHERE conditions formatting
- Subquery formatting
- CTE (Common Table Expressions) formatting
- Human-readable SQL for debugging
Advanced analytics with window functions (MySQL 8.0+, PostgreSQL 9.4+, SQLite 3.25+).
Topics covered:
- ROW_NUMBER() - Sequential numbering within partitions
- RANK() - Ranking with gaps for ties
- DENSE_RANK() - Ranking without gaps
- LAG() - Access previous row data
- LEAD() - Access next row data
- Running totals - Cumulative sums
- Moving averages - Rolling statistics
- FIRST_VALUE() / LAST_VALUE() - First and last values in window
- NTILE() - Divide into buckets/quartiles
- Multiple window functions - Combining window functions
Basic CTE usage with Common Table Expressions.
Topics covered:
- Simple CTE - Temporary result sets with Closure
- CTE with QueryBuilder - Using query builder instances
- CTE with raw SQL - Direct SQL in CTEs
- Multiple CTEs - Combining multiple CTEs
- Column lists - Explicit column definitions
- CTE with JOIN - Joining CTEs with tables
Recursive CTE usage for hierarchical data.
Topics covered:
- Category hierarchy - Tree traversal
- Employee chain - Management hierarchy
- Depth limits - Controlling recursion depth
- Subordinate counts - Aggregating hierarchical data
Materialized CTEs for performance optimization.
Topics covered:
- Materialized CTEs - Cached CTE results
- Performance optimization - Faster repeated queries
- Dialect support - PostgreSQL native, MySQL/SQLite emulation
Full-text search across all databases.
Topics covered:
- MySQL FULLTEXT - Native full-text search
- PostgreSQL tsvector - Text search vectors
- SQLite FTS5 - Full-text search extension
- Cross-database FTS - Unified API across all databases
SQL set operations for combining query results.
Topics covered:
- UNION - Combine queries, remove duplicates
- UNION ALL - Combine queries, keep duplicates
- INTERSECT - Find common rows
- EXCEPT - Find rows in first query not in second
- Multiple UNION - Chaining set operations
- UNION with aggregation - Combining aggregated results
- UNION with filters - Complex filtering
php 01-connection-pooling.phpPDODB_DRIVER=mysql php 01-connection-pooling.phpPDODB_DRIVER=pgsql php 01-connection-pooling.php- Connection pooling reduces overhead in multi-database applications
- Bulk operations are significantly faster than individual inserts
- Upserts reduce the need for SELECT-then-INSERT/UPDATE patterns
- MERGE statements provide atomic INSERT/UPDATE operations based on match conditions
- Subqueries can sometimes be replaced with JOINs for better performance
- Subqueries - Subqueries in SELECT, WHERE, FROM
- CTEs - Common Table Expressions
- Window Functions - Window functions (ROW_NUMBER, RANK, LAG, LEAD)
- Full-Text Search - Cross-database FTS
- Set Operations - UNION, INTERSECT, EXCEPT
- Bulk Operations - insertMulti, bulk updates
- UPSERT Operations - onDuplicate/INSERT...ON CONFLICT
- Connection Management - Multiple connections
Explore more advanced topics:
- Data Management - File loading, batch processing, export helpers
- Performance - Caching, profiling, EXPLAIN analysis
- Architecture - Read/write splitting, sharding
- Reliability - Exception handling, connection retry