by magicalhippo · 2y ago · 0 comments
Learn your DB server. Check the query plans often. You might get surprised. Tweak and recheck.
Usually EXISTS is faster than IN. Beware that NOT EXISTS behaves differently than EXCEPT in regards to NULL values.
Instead of joining tables and using distinct or similar to filter rows, consiser using subquery "columns", ie in SELECT list. This can be much faster even if you're pulling 10+ values from the same table, even if your database server supports lateral joins. Just make sure the subqueries return at most one row.
Any query that's not a one-off should not perform any table scans. A table scan today can mean an outage tomorrow. Add indexes. Keep in mind GROUP BY clause usually dictates index use.
If you need to filter on expressions, say where a substring is equal something, you can add a computed column and index on that. Alternatively some db's support indexing expressions directly.
Often using UNION ALL can be much faster than using OR, even for non-trivial queries and/or multiple OR clauses.
edit: You can JOIN subqueries. This can be useful to force the filtering order if the DB isn't being clever about the order.
One interesting thing I found about Postgres that's probably true of others too, often you can manually shard INSERT (SELECT ...) operations to speed them up linearly with the number of CPU cores, even when you have like 10 joins. EXPLAIN first, find the innermost or outermost join, and kick off a separate parallel query operating on each range of rows (id >= start AND id < end). For weird reasons, I relied on this a lot for one job 6 years ago. Postgres has added parallelism in versions 10+, but it's still not this advanced afaik.
Wonder if it'd be useful at all for live applications or just data processing. For the former, would need to somehow execute all reads at the same MVCC version even though they're separate connections.
What does this mean? Running
SELECT
column1,
(
SELECT column2, column3, ...
FROM table_b
WHERE table_a.id = table_b.a_id
)
FROM table_a
Results in "subquery must return only one column" as I expected. You mean returning the multiple columns as a record / composite type?Keep in mind GROUP BY clause usually dictates index use.
The reason for this wasn't immediately apparent to me. For those who were curious, this blog post walks through it step by step: https://www.brentozar.com/archive/2015/06/indexing-for-group...
> The reason for this wasn't immediately apparent to me.
The key thing to remember is that grouping is essentially a sorting operation, and it happens before your other sorts (that last part isn't necessarily as obvious).
SELECT
column1,
(
SELECT column2
FROM table_b
WHERE table_a.id = table_b.a_id
) as b_column2,
(
SELECT column3
FROM table_b
WHERE table_a.id = table_b.a_id
) as b_column3
FROM table_a
It might look like a lot more work, but in my experience it's usually a lot faster. YMMV but check it.If table_b has an index on id,column2,column3 (or on id INLUDEing column2,column3) I would expect the equivalent JOIN to usually be faster. If you have a clustered index on Id (which is the case more often than not in MS SQL Server and MySQL/InnoDB) then that would count for this unless the table is much wider than those three columns (so the index with its selective data would get many rows per page more than the base data).
Worst (and fairly common) case with sub-selects like that is the query planner deciding to run each subquery one per row from table_a. This is not an issue if you are only returning a few rows, or just one, from table_a, but in more complex examples (perhaps if this fragment is a CTE or view that is joined in a non-sargable manner so filtering predicates can't push down) you might find a lot more rows are processed this way even if few are eventually returned due to other filters.
There are times when the method is definitely faster but be very careful with it (test with realistic data sizes and patterns) because often when it isn't, it really isn't.
Yeah I guess I should have specified that this technique usually works best when done in the outer query, not buried deep inside.
It can be particularly effective if you fetch partial results, ie due to pagination or similar.
That said, these things aren't set in stone. I shared my experience, but my first tip goes first :)
Frequently this is trivial, sometimes it's not.
If there will be multiple hits but it doesn't matter that much, there's the obvious TOP 1 or MIN(col) and such.
It's a tradeoff between accidentally breaking the query and returning unexpected data.
Note that if you used join you could have bigger issues as the join would succeed but now you got multiple rows where you didn't expect.
Cross apply (select top 1 ... ) x
https://github.com/ankane/pghero
https://news.ycombinator.com/item?id=41299148#41300220 (mentioned among the author's other helpful tools and Ruby gems)
> helpful to identify slow queries in production, remove duplicate indexes, see missing indexes, keep an eye on table size, etc
--
I haven't recently put in the effort to find a copy of SQL Sentry from back when the full-featured edition was briefly free but even the "always free" version was helpful working with MSSQL query plans.
https://www.solarwinds.com/free-tools/plan-explorer
(NOTE: Not sure how free-but-pushy it is these days, but years ago it wasn't bad.)
Oftentimes the well-designed queries behave unexpectedly, because the column statistics are not updated or when the data is fragmented for big tables (e.g. random PK insertion).
I disagree.
There are queries where a table scan is the most efficient access strategy. These are typically analytical/aggregation queries that usually query the whole table. And sometimes getting only 50% of all rows is better done using a table scan as well.
I also don't see how a (read only) "table scan" could leave to an outage. It won't block concurrent access. The only drawback is that it results in a higher I/O load - but if the server can't handle that, it would assume it's massively undersized.
Outage might "just" mean slow enough that customer can't get their work done in time. For the customer it's the same.
The JSON functions most RDBMS offer are awesome for that. One subquery to get a JSON if you have multiple results for the field then you only have to decode it on the app side.
That very much depends on your data.
If you have something else in mind, do feel free to elaborate.
I agree with you that one should seek when possible as part of normal query optimization, but depending on your data, it could also just easily be something you can live with forever.
What reason might the lack of indexes suddenly become a critical issue? And what other things might you be scrambling to deal with at the same time? Tables might fill quickly when a favorable review comes in, or some world even results in churn in your system.
Just make the damned index. You Are Going to Need It. And what’s the harm in making it?