A SQL Heuristic: ORs Are Expensive
ethanseal.com
A SQL Heuristic: ORs Are Expensive
1–10 of 82 posts
Re: A SQL Heuristic: ORs Are Expensive
#2Re: A SQL Heuristic: ORs Are Expensive
#3Another huge benefit that we're realizing as we move some of our heaviest tables to this pattern is that it makes it really easy to index a core set of fields in Elasticsearch, along with a tag to associate it with a particular domain model. This has drastically cut down on our search-specific denormlization and let us avoid expensive index schema updates.
Re: A SQL Heuristic: ORs Are Expensive
#4I find query planning (and databases in general) to be very difficult to reason about, basically magic. Does anyone have some recommended reading or advice?
Re: A SQL Heuristic: ORs Are Expensive
#5Re: A SQL Heuristic: ORs Are Expensive
#6This sort of thing is why looking at generated SQL while developing instead of just trusting the ORM to write good queries is so important. I find query planning (and databases in general) to be very difficult to reason about, basically magic. Does anyone have some recommended reading or advice?
Re: A SQL Heuristic: ORs Are Expensive
#7Can we agree that this is only applies to queries where all the filter conditions use cols with indexes? If no indexes can be used, a single full table scan with OR surely is faster than multiple full table scans.
Re: A SQL Heuristic: ORs Are Expensive
#8This sort of thing is why looking at generated SQL while developing instead of just trusting the ORM to write good queries is so important. I find query planning (and databases in general) to be very difficult to reason about, basically magic. Does anyone have some recommended reading or advice?
Re: A SQL Heuristic: ORs Are Expensive
#9Can we agree that this is only applies to queries where all the filter conditions use cols with indexes? If no indexes can be used, a single full table scan with OR surely is faster than multiple full table scans.
Absolutely. Though I don't recall seeing multiple sequential scans without a self-join or subquery. A basic filter within a sequential scan/loop is the most naive/simplest way of performing queries like these, so postgres falls back to that. Also, fwiw, BitmapOr is only used with indexes: https://pganalyze.com/docs/explain/other-nodes/bitmap-or .