4 ms·
What's wrong with "select id from table"? (presumably "where" skipped for brevity of the example)
by gajus 3y ago
What's wrong with "select id from table"? (presumably "where" skipped for brevity of the example)
- pritambaral 3y agoI read it as "`where` skipped at code time because the table is never going to be too large".
- CBLT 3y agoIt 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.
- WanderPanda 3y agoIsn‘t implementing joins in the application what microservices are all about?
- _jal 3y agoNo, microservices are all about senior management's ability to map applications to people to yell at.
- nicoburns 3y agoYou're getting confused with NoSQL ;)
- macintux 3y agoI 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.
- cogman10 3y agoIDK 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
- WanderPanda 3y agoI can‘t believe this is a thing in 2023! If this is faster, shouldn‘t the database do that transparently in the background?!
- cogman10 3y agoAt 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 can 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).
- dragonwriter 3y agoWhat'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 )
- muzaffarpur 3y agoMost of the query optimisers would automatically convert subqueries into join.
- stvltvs 3y agoNot if the dev has removed the context of which table the id values come from.
- muzaffarpur 3y agoCan you explain bit further. If the subquery do not have context where the value comes from, I would think query will error out?
- 10000truths 3y agoWhat 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.
- 3y ago