October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsClean PCRecommendedOne scan can reveal what keeps slowing WindowsLook for cleanup and repair opportunities.Run ScanOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk4 min

What Reversing a D1 Composite Index Changes in the Query Plan

Reversing a D1 composite index changes the query plan only when the reversed key order lets SQLite skip a separate sort. Here is how to check it with EXPLAIN QUERY PLAN.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Reversing the sort direction of a composite index in Cloudflare D1 changes the query plan only when the reversed key order lets SQLite satisfy the query’s ORDER BY (or a filter) without a separate sort. Whether that happens depends on your SQL, your schema and the planner’s cost estimate, so the only reliable answer comes from comparing EXPLAIN QUERY PLAN output before and after the change.

Why index direction can change the plan at all

D1 is built on SQLite, so its query planner follows SQLite’s rules. SQLite can read an index backwards, which means an index declared ASC can serve a DESC ordering and the reverse. What the index cannot do is produce an arbitrary mix of directions. A multi-column index is stored ordered by its leftmost key first, with each following key breaking ties. The planner can use the index for ordering only when the requested ORDER BY matches that sequence, or its exact reverse, after any leading columns fixed by equality constraints are removed.

As an Amazon Associate I earn from qualifying purchases.

That is why direction matters. Reversing an index is not a generic speedup. It is a change in which orderings the index can deliver directly. If the query already gets its order from the existing definition, the reversal can leave the plan unchanged. If the query’s ordering is the exact opposite of what the index provides, the reversal may remove a sort. If the query mixes directions across several sort keys, a reversal may still leave a sort in place.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Working through a two-column example

Take an index on (account_id ASC, created_at DESC) and a query that filters on one account:

  • Query A: WHERE account_id = ? ORDER BY created_at DESC. The index already stores each account’s rows newest first, so the planner can walk the matching range in order.
  • Query B: WHERE account_id = ? ORDER BY created_at ASC. The planner can walk the same range backwards. The equality on account_id fixes the leading key, so the reverse traversal gives oldest first within that account.
  • Query C: WHERE account_id = ? ORDER BY created_at ASC, id DESC with an index that has no id key. The index cannot supply the second term, so a temporary sort remains.

Reversing a full index definition flips every key’s direction at once. A reverse traversal therefore gives you the opposite of the whole stored sequence, never one column flipped alone. If your query needs created_at ASC, status ASC while the index stores created_at DESC, status ASC, no single reversal satisfies it, and you need a different index definition or an accepted sort.

Reading the plan output

SQLite’s EXPLAIN QUERY PLAN output, documented at https://a1.sqlite.org/eqp.html, is the evidence to compare. Three details matter most:

  • The index name. The plan should name the index you intended. If it names another index or a full table scan, the planner chose a different strategy.
  • A temporary B-tree line. A line such as USE TEMP B-TREE FOR ORDER BY means SQLite is sorting results itself. Its absence suggests the index supplies the order, though you should confirm against the query shape.
  • The verb. SEARCH usually indicates a lookup constrained by an index key. SCAN can also describe iterating through an index in order, so read the full detail line instead of treating every SCAN as a whole-table read.

Cloudflare’s guidance on indexes, updated August 10, 2026, states that a multi-column index is used when a query references its leftmost column or a leftmost prefix, and recommends EXPLAIN QUERY PLAN to confirm index use: https://developers.cloudflare.com/d1/best-practices/use-indexes/.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

How to test a direction change in D1

An existing index cannot be altered in place, so the test is a drop and recreate. Keep the table and query fixed, and work through these steps:

  1. Record the current definition. Query SELECT sql FROM sqlite_schema WHERE type = 'index' AND tbl_name = 'events';, replacing events with your table. For per-column detail, run PRAGMA index_xinfo('idx_account_created'); with your index name, which shows each key’s sort direction.
  2. Capture the baseline plan. Run EXPLAIN QUERY PLAN on the exact SELECT, including its WHERE, ORDER BY and LIMIT clauses, and save the output.
  3. Swap the index. Run DROP INDEX idx_account_created;, then CREATE INDEX idx_account_created ON events (account_id ASC, created_at ASC); or whichever direction you are testing.
  4. Refresh statistics. Run PRAGMA optimize; after creating the index, as Cloudflare recommends, so the planner’s statistics reflect the new definition.
  5. Compare plans. Rerun the same EXPLAIN QUERY PLAN and compare the index name, the temporary B-tree line and the scan or search wording.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Plan changes are not runtime results

A changed plan shows a different strategy, not a faster query. Runtime depends on data volume, cache state, selectivity and row width, so measure both before and after on representative data. D1 bills by rows read and written, so the row counts a query reads are also worth recording alongside latency. The official sources do not publish a benchmark for how much index direction alone changes runtime, so treat any percentage you encounter elsewhere with caution.

The planner also works from cost estimates. An index that fits the query can still be ignored if the planner estimates another route to be cheaper. SQLite describes this cost-based approach in its query planning documentation at https://www.sqlite.org/queryplanner.html. If the plan does not change after a reversal, check whether the planner already had a comparable strategy, and whether PRAGMA optimize refreshed the statistics it uses.

Checklist before you keep a reversed index

  • The index’s key order and directions match the query’s ORDER BY sequence, or its exact reverse, after equality-constrained leading keys are removed.
  • The WHERE clause constrains the leftmost key or a leftmost prefix.
  • The plan names the intended index and no temporary B-tree appears for ordering.
  • Rows read and latency were measured on representative data, both before and after the change.
  • Other queries that use the same table were checked, because a direction that helps one query may leave another sorting.

For the detailed syntax of statements in D1, see Cloudflare’s SQL statements reference, last updated April 21, 2026: https://developers.cloudflare.com/d1/sql-api/sql-statements/.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

“

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Outdated Drivers Are Slowing You DownFree scan - exact matches
Windows Errors? Fix Them Before They SpreadFree repair scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.