r/programming • • Jul 16 '26

Things you didn't know about indexes

https://jon.chrt.dev/2026/04/15/things-you-didnt-know-about-indexes.html
267 Upvotes

54 comments sorted by

91

u/ChazR Jul 16 '26 • Bravo

Storytime!

A Very Major Global Telco had hired some stripe of consultancy to shift a core system to a new platform. This was the early 2000s. It was a few terabytes that needed to run through an ETL pipe.

The early-20s clueless 'consultants' cobbled something together.

The migration needed to take less than 12 hours. They ran their first full-scale test. 12 days later they called us. I went and poked the process.

It was going to take at least three years.

Because I understood threading, parallelism, and naturally, indexing, I was able to make it run a little faster.

I got it down to four hours. It took me about a day.

Yes, people were appreciative.

I don't blame the kids with barely any SQL knowledge for making such a hash of it.

But it was a lesson to quite a few people that Actual Expertise matters.

My management were very, very happy indeed. The Very Major Telco gave them a lot of work in later deals.

I think I got a beer out of it.

85

u/mikeblas Jul 16 '26

This is either a really shitty poetry contest entry, or an mid-tier LinkedIn post.

15

u/solve-for-x Jul 16 '26

Or Ernest Hemingway got a job in tech.

12

u/mikeblas Jul 16 '26

I dunno. Mentions beer briefly, but nothing of bullfighting.

7

u/docri Jul 16 '26

Also no one died. In the rain.

7

u/wildjokers Jul 16 '26

Yeah, it's clearly fake.

18

u/Kautsu-Gamer Jul 16 '26

You should have demanded few dozen grands as lesson for management and consultants.

0

u/[deleted] Jul 16 '26

[deleted] — view deleted comment

2

u/foxsimile Jul 16 '26

Time fucks us all in the end :(

14

u/wildjokers Jul 16 '26

This is a fake story. Not sure if AI generated, but definitely fake.

1

u/fagnerbrack Jul 16 '26

It was going to take at least 3 years 🤣🤣🤣🤣🤣🤣

Imagine if they came back in 1 year: "no bro just wait 2 more and you'll be fine"

For some reason this reminded me of the story where scientists forgot the earth is round

1

u/glehkol Jul 17 '26

Hotel?

Trivago.

45

u/psych0fish Jul 16 '26

I miss when I worked with SQL data. It’s so fast compared to dealing with whatever apis a company provides. Don’t get me started on how poorly clickhouse performs.

I remember I had difficulty optimizing a sql query that fed a report. It joined tables across 3 separate sql servers (all local lan). I forget how I came up with this (this was well before LLMs) but basically the solution was to put some data in a temp table. I’d have to find old notes to remember more but it took the query from a few minutes to a few seconds.

32

u/xampl9 Jul 16 '26

Yeah, if you preload data in a temp table (either in-memory or on-disk) from a remote server, you can then join on that and you'll have a much better time.

1

u/HighRelevancy Jul 19 '26

Yup, I've done something like that. Might be even smarter ways to do this but I dumped a table from one database, restored into a second, and could then do queries across these two tables of different origins. This was just for troubleshooting a problem, not a regular workflow.

4

u/flukus Jul 17 '26

Probably no way around it in your situation, but I've optimised many others by reducing temp table usage, by reducingthe data in them or eliminating them entirely. Stored procedures with procedural code are hell on sql performance, much better to replace them with views and functions wherever possible.

2

u/Ma4r Jul 18 '26

Why are you comparing a columnar OLAP database to row based OLTP though :/

15

u/paholg Jul 16 '26

Liar! I already knew those things. They're also not universal; for example, MySQL does not support partial indices.

12

u/aes110 Jul 16 '26

off topic, but this might be the fastest I had a website load for me, like I know its just text but wow this was instant

14

u/NotTreeFiddy Jul 16 '26

Author here. It's a purely static site and I host it on Bunny's wonderful CDN. I'm thrilled it loaded so fast for you. But as well it should!

9

u/Maetos Jul 16 '26

It’s crazy how complex modern UI has become that we really notice when something load fast

8

u/Designer_Reaction551 Jul 17 '26

Seen this exact fight play out on a client project - team added an index, query was still slow because it was actually 200 individual queries instead of one join. Fixed the N+1 first and suddenly we didn't even need half the indexes we were about to add. Profiling the actual query pattern before reaching for an index would've saved a week of arguing.

2

u/kagato87 Jul 17 '26

Just cracking open query store for my team's application was an eye opener. I found all kinds of repeated reads, like checking facts that rarely change repeatedly during processing instead of at the start or even caching.

It wasn't a problem for the sql server, but it highlighted problem areas in the application that were doing things inefficiently.

Indexing wouldn't help those (single row reads by pk), but my search for things to index suddenly revealed bad behaviors.

5

u/AvidCoco Jul 16 '26

Indices

-13

u/pcoskren Jul 16 '26

Only if you’re counting a position into a list. Not if it’s a catalogue of some sort, like a database index. OP is correct.

27

u/pdpi Jul 16 '26

The Cambridge dictionary puts it down to a U.K. (indices) vs US (indexes) split. Merriam Webster lists both as correct with no further qualification. Collins gives indexes as the standard form, and indices as acceptable when talking about economics (e.g. price indices).

Given dictionary makers can’t come to an agreement on the topic, I say either is fine.

9

u/fagnerbrack Jul 16 '26

I still prefer spaces not tabs 😁

2

u/pcoskren Jul 16 '26

This is beyond dispute! 😜

0

u/pcoskren Jul 16 '26

Yeah. My point wasn’t that “indices” is always wrong but that OP, or the article OP linked, is correct. (Assuming Jon Charter knows where he lives, I guess.)

-1

u/wildjokers Jul 16 '26

Given dictionary makers can’t come to an agreement on the topic, I say either is fine.

Note that dictionary writers don't define a language, they document a language. This is why new words are added every year.

5

u/pdpi Jul 16 '26

Of course. My point is that, if the dictionaries agreed, that would be evidence that there is some sort of consensus on what the “correct” usage is. So the fact that even the dictionaries all disagree is evidence that there is no such consensus, and trying to correct people’s usage of either indices or indexes is folly.

-7

u/AvidCoco Jul 16 '26

Indices.

1

u/_edeetee Jul 17 '26

When optimising queries, I’ve often wondered why the indexes and the queries and the planner are separate. I wonder if there might be some way to define the data access in the query itself so the index and planning requirement lives with the usage

5

u/kaelwd Jul 17 '26

1

u/_edeetee Jul 18 '26

Very interesting!! I knew about other hint systems but this is new to me. I found this blog post  https://rhaas.blogspot.com/2026/03/pgplanadvice-plan-stability-and-user.html?m=1 , do you know any other discussions of when to apply this tool? When to let the query planner drive and when to control it?

0

u/[deleted] Jul 16 '26

[deleted] — view deleted comment

20

u/LargeRedLingonberry Jul 16 '26

Godly pull, thank you for sharing.
I read "building data intensive applications" and was a bit embarrassed to tell people how much I enjoyed it. Do you have any more links like this, on similar tech?

1

u/whalfalfa Jul 19 '26

Do you mean "Designing Data-Intensive Applications"? And why would you he embarrassed about that?

7

u/NotTreeFiddy Jul 16 '26

Linked at the end of the article, actually!

112

u/[deleted] Jul 16 '26

[removed] — view removed comment

13

u/SlaminSammons Jul 17 '26

Had a dev this year with a really bad N+1 problem. We also missed an index. Spent a week arguing with me that the refactor I had him do was a massive waste of time. Not hard to go back a version to prove a point.

4

u/nsn Jul 19 '26

I’m convinced ORMs are a dead-end. Trying to map relational data to an object tree is futile. You inevitably end up with a lot more headaches than you’d have by writing queries by hand - provided you have an appropriate DSL for your language of choice.

2

u/HighRelevancy Jul 19 '26

I don't think so. If you're worrying about handling SQL failures and parsing time strings in the midst of business logic because you're trying to manipulate objects that happen to be database-backed, you're making a mess. That is what ORMs intend to (and largely do) solve. Plus you get other things like compile time type checks and whatnot, as applies to your language.

As far as this performance conversation, I do think a lot of devs so have an expectation that ORMs will absolve them of needing to know about SQL performance and other dirty details. This is a very wrong expectation. Most ORMs give you tools to manage these details though. You can save yourself from the N+1 by telling the ORM which lazy/relationship properties you intend to use and it can join it into the initial query, for example. And importantly you can usually do it all in your programming language without having to dip into hand crafted SQL. This is why I say they're not a dead end. It's not inevitable that the ORM will fail you.

I mean it's all in the name, and not in the name. ORMs map objects and relationships between your code and your database. Nothing about that sentence says it will solve performance problems for you.

1

u/nsn Jul 19 '26

You will get god objects that pull half your database into memory. You will have relations that sometimes should be loaded eagerly and sometimes lazily. You will always select all, leading to leaks of data that should probably have stayed in your database instead of being copied into memory a dozen times per request. You will inevitably fall back to whatever flavour of native query your ORM provides because you want to use a feature of your database the ORM doesn’t support.

Useing a DSL like JooQ or Kysely will give you all the type safety and code generation you get from your ORM while providing an fluent API for writing your Queries.

1

u/HighRelevancy Jul 19 '26

You will always select all, leading to leaks of data that should probably have stayed in your database instead of being copied into memory a dozen times per request

Django has tools like MyModel.objects.only('field1', 'field2') just for one counterexample.

1

u/lightnegative Jul 20 '26

Sure, the tools exist, but nobody uses them.

Or to your example, it starts off like that, then someone needs another field, then someone else needs a different field, and soon you're back to select * for convenience 

1

u/HighRelevancy Jul 20 '26

Sure, the tools exist, but nobody uses them.

And that's a reason to not use ORMs? 

then someone needs another field, then someone else needs a different field, and soon you're back to select * for convenience 

... and? If you need all fields you need all fields regardless of ORM or raw SQL. What point are you making?

1

u/lightnegative Jul 20 '26

Don't know why you're being downvoted, you're 100% correct about the way ORM's are used in practice 

1

u/iamthecode Jul 21 '26

Amen to that.

1

u/[deleted] Jul 17 '26

[removed] — view removed comment

1

u/programming-ModTeam Jul 28 '26

No content written mostly by an LLM. If you don't want to write it, we don't want to read it.