grangerelixx

grangerelixx

Sorted Index and Order By

Hi

I am trying sorted index in elixir.

My migration has the following index

create index(:posts, ["date DESC"])

When doing sorted index, should we do the order_by step while querying? like below

Post |> order_by( [p], asc: p.date)

If we are to use sorted index, then why do we also need order_by? Basically what is the advantage of sorted index in this case?

Marked As Solved

joey_the_snake

joey_the_snake

There are a few things here…

  1. The order of a result set from a query is not guaranteed because it has an index. In general with SQL you cannot assume any particular order of the result set unless you specify the order by clause.
  2. The advantage of creating a b-tree index when using order by is that you can traverse the index in the same order you want the results to be in. This is faster than scanning the table which most likely won’t be stored in the same order.
  3. All b-tree indices are sorted (it helps ensure the lookup is fast). It just depends on if you want it sorted in ascending or descending order.

Also Liked

BartOtten

BartOtten

The accepted answer misses an important detail, as an index also works backwards. So if you have ASC index, a DESC query uses that index without having to scan the table.

So to extend the answer:

You might wonder why bother providing all four options, when two options together with the possibility of backward scan would cover all the variants of ORDER BY . In single-column indexes the options are indeed redundant, but in multicolumn indexes they can be useful. Consider a two-column index on (x, y) : this can satisfy ORDER BY x, y if we scan forward, or ORDER BY x DESC, y DESC if we scan backward. But it might be that the application frequently needs to use ORDER BY x ASC, y DESC . There is no way to get that ordering from a plain index, but it is possible if the index is defined as (x ASC, y DESC) or (x DESC, y ASC) .

Where Next?

Trending in Questions Top

lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New
tj0
I’ve been following the steps here for the upgrade from 1.6 to 1.7 and it has gone relatively smoothly all the way till the phoenix_view ...
New
cgraham
Hi! What is currently the best library/method for parsing text and tabular data out of PDF files in Elixir or Erlang?
New
stefanchrobot
Hi, I need a way to handle data migrations in my application. I found an article by @wojtekmach about manual migrations: Automatic and ma...
New
stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
kip
Localize is the next generation localisation library for Elixir. Think of it as ex_cldr version 3.0. The first version will be released ...
New
webofbits
Squid Mesh is an open source workflow automation runtime for Elixir applications. It is aimed at Phoenix and OTP apps that want to defin...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
kip
In 2021 I started a new library called Tempo with the objective of modelling time as a set of intervals - not as instants. In 2022 I gave...
New

We're in Beta

About us Mission Statement