Tagged articles
ORDER BY
43 articles · Page 1 of 1
This article explains why MySQL GROUP BY + ORDER BY queries become extremely slow on hundred-million-row tables even when EXPLAIN shows index usage, covering temporary tables, filesort, loose index scan limitations, scanning overhead, and solutions like pre-aggregated summary tables, partitioning, columnar engines, and query scope reduction.
ClickHouseIndex OptimizationMySQL
0 likes · 12 min read


liandkAug 13, 2026 · Databases
When query results appear unordered, the SQL ORDER BY clause lets you instantly arrange rows by ascending or descending values—such as timestamps, amounts, or multiple fields—providing clean, organized output for latest‑first displays, statistical sorting, and tidy list presentations.
ORDER BYSQLdatabase
0 likes · 2 min read

The article explains why MySQL may still report Using filesort despite a composite index that appears to satisfy both WHERE and ORDER BY clauses, covering index ordering rules, the impact of range conditions, mismatched ORDER BY field order, ASC/DESC mixing, and practical optimization techniques.
Composite IndexFilesortIndex Optimization
0 likes · 12 min read

This article walks through a real‑world MySQL slow‑query case, explains the inner workings of Join and Order by algorithms, shows how to interpret EXPLAIN and Optimizer_trace output, and demonstrates how adding an index can transform a costly Block Nested‑Loop Join into a fast Index Nested‑Loop Join, dramatically improving performance.
EXPLAINJoin AlgorithmsMySQL
0 likes · 13 min read

This article walks through a real-world slow‑SQL case, explains the inner workings of Join and Order‑by algorithms, demonstrates using EXPLAIN and optimizer_trace to trace execution, and shows how adding indexes and choosing the right join method can dramatically improve MySQL query performance.
EXPLAINJoin AlgorithmsMySQL
0 likes · 12 min read

This article explains why using variable assignment expressions inside ORDER BY clauses can trigger severe OCP alarms, analyzes the underlying source code in OceanBase, demonstrates unpredictable query results with examples, and offers safer alternative patterns.
Database WarningORDER BYOceanBase
0 likes · 8 min read

After upgrading a client from MySQL 5.7 to 8.0, query performance slowed dramatically due to changes in ORDER BY handling, and the article analyzes the root cause, presents verification tests, and recommends adding indexes to restore millisecond‑level response times.
MySQLORDER BYSQL Optimization
0 likes · 7 min read

This article walks through practical MySQL performance tuning methods—including pagination, join, subquery, ORDER BY, GROUP BY, and COUNT optimizations—illustrated with real‑world data volumes, step‑by‑step SQL examples, EXPLAIN analyses, index creation, and measurable query‑time improvements.
MySQLORDER BYPerformance Tuning
0 likes · 14 min read

This article explains how MySQL executes ORDER BY queries, the role of sort_buffer, the difference between full-field and rowid sorting, how parameters like sort_buffer_size and max_length_for_sort_data affect performance, and how proper indexing can eliminate the need for sorting altogether.
Index OptimizationMySQLORDER BY
0 likes · 12 min read

When a system’s data grows, MySQL queries can become slow and cause latency; this article presents practical optimization strategies—including query tuning, pagination, join improvements, subquery replacement, order‑by and group‑by enhancements, and count() efficiency—illustrated with sample tables, code snippets, and performance screenshots.
MySQLORDER BYPagination
0 likes · 13 min read

This article examines why combining ORDER BY with LIMIT in MySQL pagination can produce duplicate rows on subsequent pages, analyzes the optimizer’s priority‑queue behavior introduced in version 5.6, and presents practical solutions such as using unique sort keys, adding ID ordering, and understanding deep‑pagination limitations.
LIMITMySQLORDER BY
0 likes · 8 min read

This article investigates the nondeterministic behavior of MySQL when combining ORDER BY with LIMIT, reproduces the official example, identifies a critical row count threshold where the optimizer switches to a priority‑queue (heap) sort, and explains the underlying source‑code logic and its impact on query results.
MySQLORDER BYheap sort
0 likes · 19 min read

This article explains the true execution sequence of SQL statements—starting with FROM and JOIN, then WHERE, GROUP BY, HAVING, SELECT, DISTINCT, ORDER BY, and LIMIT—illustrating each step with diagrams, examples, and code snippets to clarify how databases process queries.
FROMHAVINGORDER BY
0 likes · 6 min read

This article explains the logical execution sequence of a typical SQL query, detailing each phase from FROM/JOIN/WHERE through GROUP BY, HAVING, SELECT, ORDER BY, and LIMIT, and illustrating the process with diagrams and code examples.
From Join WhereHAVINGLIMIT
0 likes · 8 min read

This article explains the step‑by‑step execution order of a standard SQL query, covering FROM/JOIN, WHERE, GROUP BY, HAVING, SELECT, DISTINCT, ORDER BY and LIMIT, and provides illustrative code snippets and diagrams to clarify each stage.
FROMHAVINGLIMIT
0 likes · 5 min read


ITPUBNov 12, 2022 · Databases
This article explains how MySQL executes ORDER BY queries, compares the default sorting algorithm with the rowid method, and provides practical optimization steps such as indexing, adjusting sort_buffer settings, and choosing the appropriate algorithm based on data size and memory.
IndexMySQLORDER BY
0 likes · 7 min read

A MySQL 5.6+ optimizer bug causes queries that filter by uid and order by id with a small LIMIT to use a costly full table scan instead of the appropriate idx_uid_stat index, and the article explains the root cause, shows optimizer_trace output, and offers two practical work‑arounds.
MySQLORDER BYOptimizer
0 likes · 8 min read

The article explains why using LIMIT together with ORDER BY in MySQL 5.6 can cause duplicate records on subsequent pages, analyzes the priority‑queue optimization that makes the sort unstable, and offers practical solutions such as indexing, proper pagination logic, and query adjustments.
LIMITMySQLORDER BY
0 likes · 8 min read

The article explains the true execution order of SQL queries, detailing each step from FROM and JOIN through WHERE, GROUP BY, HAVING, SELECT, DISTINCT, ORDER BY, and LIMIT, and illustrates the concepts with examples and code snippets.
FROMHAVINGLIMIT
0 likes · 6 min read

Learn how to effectively sort query results using ORDER BY, apply ASC/DESC rules, combine multiple fields, use aliases, integrate functions, and limit result sets with LIMIT for pagination, while avoiding common pitfalls such as duplicate sorting values and expression misuse in MySQL.
LIMITMySQLORDER BY
0 likes · 16 min read

In a production environment, a pagination export feature caused duplicate and missing records due to MySQL’s nondeterministic ordering when multiple rows share the same ORDER BY value, and the article explains the root cause, official documentation, and a reliable solution using additional sorting columns.
LIMITMySQLORDER BY
0 likes · 10 min read

This article explains Cobar’s sharding middleware and presents an optimization technique for handling ORDER BY and LIMIT queries across multiple databases, detailing the original approach, its inefficiencies with deep pagination, the step‑by‑step improved algorithm, performance analysis, limitations, and practical applicability.
CobarLIMITORDER BY
0 likes · 8 min read

Using MySQL's ORDER BY with LIMIT can yield an unexpected row order because duplicate values in the sorting column cause nondeterministic results; adding a unique secondary column like id stabilizes the order, and the official documentation explains how LIMIT influences query planning and optimization.
LIMITMySQLORDER BY
0 likes · 6 min read


ITPUBAug 19, 2021 · Databases
When using MySQL pagination with LIMIT together with ORDER BY, the second page can unexpectedly contain rows from the first page because MySQL 5.6's priority‑queue optimization performs an unstable heap sort, causing nondeterministic ordering for equal values and leading to duplicate results.
LIMITMySQLORDER BY
0 likes · 7 min read

This article explains the internal execution process of MySQL's ORDER BY, compares full‑field and rowid sorting, shows how indexes, sort_buffer size and max_length_for_sort_data affect performance, and provides practical optimization techniques such as composite and covering indexes.
Covering IndexMySQLORDER BY
0 likes · 13 min read

This article explains how MySQL processes ORDER BY clauses, illustrates the execution steps with a full‑field index, discusses the impact of sort_buffer and max_length_for_sort_data, and shows how adding composite and covering indexes can eliminate filesort and improve query performance.
Index OptimizationMySQLORDER BY
0 likes · 6 min read

This article explains MySQL's ORDER BY execution process, comparing full-field sorting with rowid sorting, detailing how sort_buffer, temporary files, and index usage affect performance, and showing how composite and covering indexes can eliminate sorting altogether.
Composite IndexCovering IndexIndex Optimization
0 likes · 13 min read

This article explains why MySQL 5.6 may return duplicate rows on the second page when using ORDER BY with LIMIT, analyzes the optimizer’s priority‑queue behavior, and provides practical solutions such as indexing, proper pagination understanding, and handling of sorting edge cases.
LIMITMySQLORDER BY
0 likes · 7 min read

This article explains why combining LIMIT with ORDER BY in MySQL can cause duplicate rows on subsequent pages, analyzes the priority‑queue optimization that makes sorting unstable, and offers practical solutions such as indexing, stable ORDER BY clauses, and a deeper understanding of pagination behavior.
Database PerformanceLIMITMySQL
0 likes · 7 min read

This article explains why using ORDER BY with LIMIT for pagination in MySQL can yield inconsistent results when the ordering column contains duplicate values, describes MySQL's limit optimization behavior, and provides a reliable solution by adding a secondary unique sort key.
LIMITMySQLORDER BY
0 likes · 5 min read

When you combine ORDER BY with LIMIT in MySQL, the result set may appear in a different order than expected, especially if the ordered column contains duplicate values, and understanding the optimizer's behavior and adding deterministic columns can resolve this issue.
LIMITMySQLORDER BY
0 likes · 6 min read

This article explains common misconceptions about index selectivity and filtering, demonstrates how composite indexes can avoid slow queries, and shows why order‑by may still trigger filesort, providing practical tips and SQL examples for better MySQL index design.
Database PerformanceIndex OptimizationMySQL
0 likes · 7 min read

This article investigates why moving MySQL’s LIMIT clause outside a subquery can cause the ORDER BY sorting to be ignored in a LEFT JOIN, analyzes the optimizer’s behavior, and recommends placing both ORDER BY and LIMIT at the outermost query level for correct results.
LIMITMySQLORDER BY
0 likes · 5 min read

The author discovers a puzzling MySQL bug where moving the LIMIT clause outside a nested SELECT causes the final result set to be ordered incorrectly, explains how ORDER BY inside subqueries is ignored during LEFT JOIN processing, and recommends placing ORDER BY and LIMIT at the outermost query level.
LIMITMySQLORDER BY
0 likes · 5 min read

MySQL can avoid a filesort for ORDER BY clauses that match an index’s direction by scanning the index forward or backward, and MySQL 8.0’s descending indexes let mixed‑direction sorts be satisfied similarly; for older versions you can emulate this by retrieving rows in index order and reversing groups in the application.
Descending IndexMySQLORDER BY
0 likes · 9 min read

This article walks through MySQL's ORDER BY execution, detailing the full‑field sorting process, when MySQL switches to row‑id sorting due to large row sizes, and how composite and covering indexes can optimize or eliminate the sorting step.
Database PerformanceIndex OptimizationMySQL
0 likes · 8 min read


ITPUBOct 25, 2017 · Databases
This article explains how MySQL processes ORDER BY, compares conventional, optimized, and priority‑queue sorting algorithms, shows how to leverage indexes, and provides practical tuning tips such as adjusting max_length_for_sort_data and sort_buffer_size to avoid costly temporary files and random I/O.
Index OptimizationMySQLORDER BY
0 likes · 12 min read

A recent order‑fulfillment outage was traced to a MySQL query that used the primary‑key index because an ORDER BY id ASC LIMIT 350 forced a full scan, but tweaking the ORDER BY or removing it reduced execution time from 20 seconds to under one second.
Index OptimizationLIMITMySQL
0 likes · 6 min read


ITPUBSep 7, 2017 · Databases
This guide explains how MySQL decides whether to use an index for range queries, LIKE patterns, and ORDER BY operations, detailing key_len calculation, index key vs filter vs table filter concepts, and practical examples for BETWEEN and sorting optimization.
BETWEENIndexMySQL
0 likes · 10 min read


ITPUBOct 20, 2016 · Databases
A lottery system that relied on MySQL’s SELECT without ORDER BY unintentionally gave a single player multiple top‑prize wins because InnoDB reordered results by primary key, revealing a subtle database engine rule that can corrupt random selection logic.
InnoDBLotteryMySQL
0 likes · 6 min read


ITPUBNov 23, 2015 · Databases
In MySQL 5.6, using LIMIT together with ORDER BY on a non‑unique column can produce nondeterministic result sets, a known bug that persists regardless of indexing, and the article shows how adding a secondary unique sort key resolves the uncertainty for reliable pagination.
Database BugLIMITMySQL
0 likes · 6 min read

MySQL's ORDER BY can either use an ordered index to return rows directly or apply one of two sorting algorithms—original and the MySQL 4.1 enhanced version—to sort data in memory, with the newer method reducing I/O by avoiding a second table read at the cost of higher memory usage.
IndexMySQLORDER BY
0 likes · 3 min read

This article explains why MySQL ORDER BY may trigger a filesort when no suitable index exists, demonstrates the issue with a product query, and shows how adding a composite index on category_id and price lets the database return rows already sorted, removing the need for an extra sorting step.
EXPLAINFilesortIndex
0 likes · 2 min read
