News for hackers. Stuff that matters.
Re:Introducing “Database Performance at Scale”: A Free, Open... by gajus on Thursday October 05, 2023 @12:22PM (#37780573)
What's wrong with "select id from table"? (presumably "where" skipped for brevity of the example)
Re:Introducing “Database Performance at Scale”: A Free, Open... by CBLT on Thursday October 05, 2023 @12:33PM (#37780728)
It looks like what they're describing is implementing database joins in the application: 2 network round trips, first one for ids from a table and the second to get values from another table with foreign keys corresponding to the ids.
Re:Introducing “Database Performance at Scale”: A Free, Open... by WanderPanda on Thursday October 05, 2023 @12:59PM (#37781064)
Isn‘t implementing joins in the application what microservices are all about?
Re:Introducing “Database Performance at Scale”: A Free, Open... by _jal on Thursday October 05, 2023 @02:39PM (#37782345)
No, microservices are all about senior management ability to map applications to people to yell at.
Re:Introducing “Database Performance at Scale”: A Free, Open... by nicoburns on Thursday October 05, 2023 @04:22PM (#37783820)
You're getting confused with NoSQL ;)
Re:Introducing “Database Performance at Scale”: A Free, Open... by macintux on Thursday October 05, 2023 @06:54PM (#37785264)
I have supported a microservices environment where each service had its own database, and indeed "foreign keys" were mostly application-level concerns. A real pain, and I don't think any of us would have chosen that approach in hindsight.

I hope there are better ways to design microservices.

Re:Introducing “Database Performance at Scale”: A Free, Open... by cogman10 on Thursday October 05, 2023 @12:37PM (#37780766)
IDK if postgres has this problem but mssql does. If you do "Select * from table where ID in (blah)"

it ends up with some pretty terrible performance.

It's often a lot faster to do something like

Insert into #tempTable ids

select from table JOIN #tempTable t on t.id = table.id

Re:Introducing “Database Performance at Scale”: A Free, Open... by WanderPanda on Thursday October 05, 2023 @01:01PM (#37781090)
I can‘t believe this is a thing in 2023! If this is faster, shouldn‘t the database do that transparently in the background?!
Re:Introducing “Database Performance at Scale”: A Free, Open... by cogman10 on Thursday October 05, 2023 @02:32PM (#37782250)
At least in our case, it comes down to expectations.

For the `IN` query the DB doesn't know how many elements are in the list. In MSSQL, it would default to assuming "well, probably a short list" which blows out performance when that's not the case.

When you first insert into the temp table, the DB and reasonably say "Oh, this table has n elements" and switch the query plan accordingly.

In addition, you can throw an index on the temp table which can also improve performance. Assuming the table you are querying against is indexed on ID, when you have another table with IDs that are indexed it doesn't have to assume random access as it pulls out each id. (Effectively, it just has to navigate the tree nodes in order rather than needing to do a full look into the tree).

Re:Introducing “Database Performance at Scale”: A Free, Open... by pritambaral on Thursday October 05, 2023 @12:31PM (#37780697)
I read it as "`where` skipped at code time because the table is never going to be too large".
Re:Introducing “Database Performance at Scale”: A Free, Open... by dragonwriter on Thursday October 05, 2023 @12:45PM (#37780881)
What's wrong is replacing a single query with a join with multiple round trips implementing part of the join logic in application code because you are using an RDBMS as if it were a fairly dumb repository of dusconnected tables.

  SELECT id FROM table1
Is fine, if what your app wants is the ids from table1. Its not good if it is just a prelude to:

  SELECT * FROM table2
  WHERE table1_id in (...ids from previous query...)
instead of just doing:

  SELECT table2.*
  FROM table1 
  INNER JOIN table2 ON (
    table2.table1_id == table1.id
  )
Re:Introducing “Database Performance at Scale”: A Free, Open... by muzaffarpur on Thursday October 05, 2023 @01:55PM (#37781751)
Most of the query optimisers would automatically convert subqueries into join.
Re:Introducing “Database Performance at Scale”: A Free, Open... by dragonwriter on Thursday October 05, 2023 @02:25PM (#37782155)
No query optimizer will automatically convert to separate queries connected by application logic that the optimizer can’t see shuttling data between them into a single join.
Re:Introducing “Database Performance at Scale”: A Free, Open... by stvltvs on Thursday October 05, 2023 @02:04PM (#37781881)
Not if the dev has removed the context of which table the id values come from.
Re:Introducing “Database Performance at Scale”: A Free, Open... by muzaffarpur on Thursday October 05, 2023 @02:25PM (#37782150)
Can you explain bit further. If the subquery do not have context where the value comes from, I would think query will error out?
Re:Introducing “Database Performance at Scale”: A Free, Open... by 10000truths on Thursday October 05, 2023 @03:23PM (#37783054)
What you're saying is that:

  SELECT * FROM table2
  WHERE table1_id in (SELECT ids FROM table1 WHERE ...)
will be optimized to a join by the query planner (which may or may not be true, depending on how confident you are in the stability of the implementation details of your RDBMS's query planner). But in most circumstances, there is no subquery, it's more like:

  SELECT * FROM table2
  WHERE table1_id in (452345, 529872, 120395, ...)
Where the list of IDs are fetched from an API call to some microservice, or from a poorly used/designed ORM library.
Re:Introducing “Database Performance at Scale”: A Free, Open... by mr_toad on Thursday October 05, 2023 @10:39PM (#37786637)
What’s really bad is where they (or the ORM) generate a seperate select statement for each ID in a loop.
Re:Introducing “Database Performance at Scale”: A Free, Open... by sgarland on Friday October 06, 2023 @07:14PM (#37797523)
And this doesn’t even get into bizarre edge cases, like how Postgres treats foo = ANY(ARRAY[‘bar’, ‘baz’]) differently than foo IN (VALUES((‘bar’), (‘baz’))). PROTIP: the latter is often MUCH faster. I’ve learned this twice this week.
Re:Introducing “Database Performance at Scale”: A Free, Open... by josephg on Thursday October 05, 2023 @03:26PM (#37783100)
Lots of application developers don’t really understand how sql works. So instead of learning to do a join (or do a sub query or anything like that) they just issue multiple queries from their application backend.

The posters up thread are talking about novice Python / JavaScript / whatever code which issues queries in sequence: first a query to get the IDs. Then once that query comes back to the application, the IDs are passed back to the database for a single query of a different table.

The query optimizer can’t help here because it doesn’t know what the IDs are used for. It just sees a standalone query for a bunch of IDs. Then later, an unrelated query which happens to use those same IDs to query a different table.

Re:Introducing “Database Performance at Scale”: A Free, Open... by osigurdson on Friday October 06, 2023 @12:56AM (#37787230)
Wow, it is hard to believe anyone would use the first option.