News for hackers. Stuff that matters.
Re:Show HN: PostgreSQL Index Advisor by sbstp on Sunday April 14, 2024 @01:32AM (#40028894)
I've often thought that a database that could automatically detect slow queries and create the necessary indexes would be neat. You run a load test on your application, which in turns calls the database and you collect all the queries it makes. Then the database automatically adjusts itself.
Re:Show HN: PostgreSQL Index Advisor by arronax on Sunday April 14, 2024 @02:40AM (#40029162)
Oracle DB is, or was, very close to that with its query profiles, baselines, and query patches. It wasn't automatic back in 2014 when I last worked on it, but all the tools were there. Heck, it was possible to completely rewrite a bad query on the fly and execute a re-written variant. I suppose it all stems from the fact that Oracle is regularly used under massive black boxes, including the EBS.

Also, the problem with automatic indexing is that it only gets you so far, and any index can, in theory, mess up another query that is perfectly fine. Optimizers aren't omniscient. In addition, there are other knobs in the database, which affect performance. I suppose, a wider approach than just looking at indexes would be more successful. Like Ottertune, for example.

Re:Show HN: PostgreSQL Index Advisor by Tostino on Sunday April 14, 2024 @10:26AM (#40031277)
The problem of new indexes messing up otherwise good queries is something I've battled on and off for the past decade with Postgres. Definitely annoying.
Re:Show HN: PostgreSQL Index Advisor by rand_r on Sunday April 14, 2024 @04:31PM (#40034210)
How would an index mess up another query? AFAIK indexes would only hurt write performance marginally per index, but most slow queries are read-only. I’ve tended to just add indexes as I go without thinking about it and haven’t run into issues, so genuinely curious.
Re:Show HN: PostgreSQL Index Advisor by adamcharnock on Sunday April 14, 2024 @07:03PM (#40035281)
While I don’t recall running into issues either, I can certainly see that a new index could cause the query planner to make a different decision. And that decision could - in some cases - end up being worse that the previous behaviour.

I definitely have seen the query planing make some peculiar choices in the past.

Re:Show HN: PostgreSQL Index Advisor by Tostino on Tuesday April 16, 2024 @08:46AM (#40051249)
This is what I ran into. Often times they were indexes with a similar cost as another, and that caused issues.

I think the main index type that bit me are the ones created by exclusion constraints. Often times it looks to the planner like "the right" index to use, but there is another (btree) that is way cheaper...the exclusion constraint is just there to ensure consistency.

In those cases to fix things, I added a WHERE clause to the index (e.g. WHERE 1=1), and the planner wouldn't consider that index unless it saw that same 1=1 condition in the queries WHERE clause.

Re:Show HN: PostgreSQL Index Advisor by paulryanrogers on Sunday April 14, 2024 @07:17PM (#40035384)
Indexes cost memory. It could push other indexes out of RAM. Or updates could increase IOPS, pushing them over an expensive threshold.
Re:Show HN: PostgreSQL Index Advisor by dmurray on Sunday April 14, 2024 @01:09PM (#40032585)
> Heck, it was possible to completely rewrite a bad query on the fly and execute a re-written variant.

Is there really such a thing as a bad query that can be rewritten to give the same results but faster? For me, that's already the query optimizer's job.

Of course there are "bad queries" where you query for things you don't need, join on the wrong columns, etc. And yeah the optimizer isn't perfect. But a query that you expect the query optimizer to "rewrite" and execute in an optimal way is a good query.

Re:Show HN: PostgreSQL Index Advisor by Scene_Cast2 on Sunday April 14, 2024 @02:10PM (#40033079)
One example is that in Presto, joins assume that left (IIRC) table is the big one you stream, and the right one is the small one you hash. One of the newer features was that in some cases, the optimizer is able to ignore the SQL order and correctly pick which table to hash and which one to stream.
Re:Show HN: PostgreSQL Index Advisor by legulere on Sunday April 14, 2024 @03:51PM (#40033869)
Isn't that just basic table/index statistics to know which table is the smallest?
Re:Show HN: PostgreSQL Index Advisor by Scene_Cast2 on Sunday April 14, 2024 @08:49PM (#40035926)
Not if you're joining on a filtered table or a result of another join.
Re:Show HN: PostgreSQL Index Advisor by atwebb on Sunday April 14, 2024 @10:39PM (#40036582)
>Is there really such a thing as a bad query that can be rewritten to give the same results but faster? For me, that's already the query optimizer's job.

I can't tell if your disclaimer covers it but, yes, there are lots of bad queries that take a little bit of a re-write and run significantly faster. Generally it is someone taking a procedural vs set based approach or including things they don't need to try and help (adding an index to a temp table when it is only used once and going to be full scanned anyways). That's outside the general data typing/generally missing indexes.

Re:Show HN: PostgreSQL Index Advisor by magicalhippo on Monday April 15, 2024 @12:25AM (#40037063)
Highly database dependent, as the query optimizer can only perform the tricks programmed into it.

For instance, we use SQLAnywhere at work (migrating to MSSQL), and it wasn't smart about IN (sub-query) so EXISTS was much faster.

Or, as I mentioned in another comment here, MSSQL performs much worse using a single OR in WHERE clause vs splitting into two queries and using UNION ALL, something which has no significant difference in SQLAnywhere.

For MSSQL I've found that even a dozen sub-queries in the SELECT part can be much faster than a single CROSS APPLY for fetching per-row data from another table.

Also the query might rely on certain assumptions that will in practice always hold in that application, but not in general. Especially around NULL, for example NOT IN vs NOT EXISTS[1].

[1]: https://www.mssqltips.com/sqlservertip/6013/sql-server-in-vs...

Re:Show HN: PostgreSQL Index Advisor by arronax on Monday April 15, 2024 @11:14AM (#40041663)
> Of course there are "bad queries" where you query for things you don't need, join on the wrong columns, etc.

You can inject a hint into the query, forcing it to use a plan that would not otherwise be used, for example. Although, fixing a plan through a baseline is way cleaner. Mostly, I just meant that as an extreme example of something you can do, not something you should do. And yes, the only reason to re-write the query is when the query itself is bad in that it asks for unnecessary data or misses a join column. Admittedly, that's an extremely dirty and dangerous thing to do, as it uncouples app from db, but it is possible.

Re:Show HN: PostgreSQL Index Advisor by elric on Sunday April 14, 2024 @02:01AM (#40029013)
I'm sure the database could, but it doesn't mean the database should. Indexes come at the cost of extra disk space, slower inserts, and slower updates. In some cases, some slower queries might be an acceptable tradeoff. In other cases, maybe not. It depends.
Re:Show HN: PostgreSQL Index Advisor by kiwicopple on Sunday April 14, 2024 @03:15AM (#40029293)
this is our posture for this extension on the supabase platform. we could automate the creation of the indexes using the Index Advisor, but we feel it would be better to expose the possible indexes to the user and let them choose
Re:Show HN: PostgreSQL Index Advisor by gneray on Sunday April 14, 2024 @09:24AM (#40030891)
this is the way ^^
Re:Show HN: PostgreSQL Index Advisor by dmurray on Sunday April 14, 2024 @01:16PM (#40032627)
You could tell it "you have a budget of X GB for disk space, choose the indexes that best optimize the queries given the budget cap."

Not perfect, because some queries may be more time-critical than others.

You could even annotate every query (INSERT and UPDATE as well as SELECT) with the dollar amount per execution you're willing to pay to make it 100ms faster, or accept to make it 100ms slower. Then let it know the marginal dollar cost of adding index storage, throw this all into a constraint solver and add the indexes which are compatible with your pricing.

Re:Show HN: PostgreSQL Index Advisor by d0100 on Sunday April 14, 2024 @01:22PM (#40032671)
Are the trade-offs measurable? If they are the database could just undo the index...

Not just indexing, but table partitions, materialized views, keeping things in-memory...

Re:Show HN: PostgreSQL Index Advisor by remus on Sunday April 14, 2024 @02:16PM (#40033134)
> Are the trade-offs measurable?

Yes, but you need the context about what is the correct tradeoff for your use case. If you've got a service that depends on fast writes then adding latency via extra indices for improved read speed may not be an acceptable trade off. It depends on your application though.

Re:Show HN: PostgreSQL Index Advisor by masklinn on Sunday April 14, 2024 @02:25AM (#40029099)
Because indexes have costs you need a much more complicated system which can feed back into itself and downgrade probationary indexes back to unindexed.
Re:Show HN: PostgreSQL Index Advisor by fulafel on Sunday April 14, 2024 @12:48PM (#40032438)
Several databases index everything, needed or not. (And sometimes have mechanisms to force it off for some specific data)
Re:Show HN: PostgreSQL Index Advisor by freedomben on Sunday April 14, 2024 @01:11PM (#40032600)
Even this isn't sufficient, because some problems with over-indexing don't become apparent until the size of a table gets much larger, which only happens a drop at a time. I suppose if it was always probationary and continually being evaluated, at some point it could recognize that for example INSERTs are now taking 1000x longer than they were 2 years ago. But that feels like a never-ending battle against corner cases, and any automatic actions it takes add significant complexity to the person debugging later.
Re:Show HN: PostgreSQL Index Advisor by ComodoHacker on Sunday April 14, 2024 @01:53AM (#40028982)
Big Guys do this. For big bucks, of course.
Re:Show HN: PostgreSQL Index Advisor by tuwtuwtuwtuw on Sunday April 14, 2024 @01:56AM (#40028995)
> big bucks

You get that feature in Azure SQL Database for $5/month.

Re:Show HN: PostgreSQL Index Advisor by tuwtuwtuwtuw on Sunday April 14, 2024 @01:53AM (#40028983)
That exists in Microsoft SQL Server. It can create new indexes, drop unused indexes, change query plans when it detect degradation and so on.
Re:Show HN: PostgreSQL Index Advisor by BrentOzar on Sunday April 14, 2024 @06:38AM (#40030174)
Source? I’ve been working with SQL Server for a couple of decades and I don’t believe it will automatically create or drop indexes under any circumstances. You might be thinking of Azure SQL DB.
Re:Show HN: PostgreSQL Index Advisor by Ciantic on Sunday April 14, 2024 @07:10AM (#40030293)
"Automatic tuning, introduced in SQL Server 2017 (14.x), notifies you whenever a potential performance issue is detected and lets you apply corrective actions, or lets the Database Engine automatically fix performance problems." [1]

I have used this in Azure SQL too, but according to that it should be in SQL Server.

https://learn.microsoft.com/en-us/sql/relational-databases/a...

Re:Show HN: PostgreSQL Index Advisor by couchand on Sunday April 14, 2024 @09:02AM (#40030798)
Good link!

> Automatic index management identifies indexes that should be added in your database, and indexes that should be removed. Applies to: Azure SQL Database

Re:Show HN: PostgreSQL Index Advisor by BrentOzar on Sunday April 14, 2024 @09:56AM (#40031065)
Read that link carefully: only automatic plan regression is available in SQL Server, not the automatic index tuning portion. The index tuning portion only applies to Azure SQL DB.
Re:Show HN: PostgreSQL Index Advisor by tuwtuwtuwtuw on Sunday April 14, 2024 @07:33AM (#40030380)
What's the point of asking for a source when you would find it on Google in one minute? Odd way of learning. Not like I brought up some debated viewpoint.
Re:Show HN: PostgreSQL Index Advisor by hobs on Sunday April 14, 2024 @08:06AM (#40030554)
Probably because most of the stuff you'd find in the top search results would include the GP's name.

Just a few sentences later "Automatic tuning in Azure SQL Database also creates necessary indexes and drops unused indexes" - that's not in on-prem SQL Server.

Re:Show HN: PostgreSQL Index Advisor by simplyinfinity on Sunday April 14, 2024 @12:32PM (#40032313)
Google the name of the person you're replying to :)
Re:Show HN: PostgreSQL Index Advisor by radicalbyte on Sunday April 14, 2024 @10:22AM (#40031246)
They've had a non-automatic "query advisor" in there forever, it operated on profiling data and was highly effective.
Re:Show HN: PostgreSQL Index Advisor by taspeotis on Sunday April 14, 2024 @10:05AM (#40031130)
That’s an Azure SQL thing, not MSSQL.
Re:Show HN: PostgreSQL Index Advisor by b3lm0nt on Sunday April 14, 2024 @01:09PM (#40032584)
Andrew Kane built dexter, which is an automatic indexer for Postgres.

https://github.com/ankane/dexter

https://ankane.org/introducing-dexter

Re:Show HN: PostgreSQL Index Advisor by ed_balls on Sunday April 14, 2024 @01:12PM (#40032605)
Default DB for App Engine (NDB) has this feature. Implicit indexes are tad annoying.
Re:Show HN: PostgreSQL Index Advisor by GordonS on Sunday April 14, 2024 @01:12PM (#40032607)
I might be misremembering, but IIRC RavenDB does this (it's a commercial document DB, written in C#).