News for hackers. Stuff that matters.
Re:Advanced SQL and database books and resources by branko_d on Monday May 11, 2020 @06:10AM (#23140529)
If you are an application programmer needing to query the database, I wholeheartedly recommend "SQL Performance Explained" by Markus Winand, or his website: https://use-the-index-luke.com/
Re:Advanced SQL and database books and resources by _gni9 on Monday May 11, 2020 @06:38AM (#23140691)
that was eye-opening indeed. Before reading his blog, I had no clue how indexes worked and how to write SQL that match the index structure (for instance the order of conditions in the where clause matter if you want to leverage a multi-column index).
Re:Advanced SQL and database books and resources by MarkusWinand on Monday May 11, 2020 @08:20AM (#23141293)
> for instance the order of conditions in the where clause matter if you want to leverage a multi-column index

Ups, if this is you take away, then I've done something wrong.

Let me correct that: The order of columns in an index matters, not the order of conditions in the where clause.

Re:Advanced SQL and database books and resources by rsecora on Monday May 11, 2020 @02:32PM (#23145402)
This is the best part of HN.

Not only you did a comment on previous post, but it's a comment from the author of an awesome guide [https://use-the-index-luke.com].

Markus, I can assure you, that your guide has saved a lot of machine-computed-hours over the world with less energy consumption/wasted. Great work. :)

Re:Advanced SQL and database books and resources by etiennebch on Monday May 11, 2020 @08:51AM (#23141529)
Ah thanks for the clarification !
Re:Advanced SQL and database books and resources by mhotchen on Monday May 11, 2020 @01:33PM (#23144694)
This book has easily been one of the most influential on my career. Having excellent SQL skills has been my secret weapon for a while now.
Re:Advanced SQL and database books and resources by therealdrag0 on Monday May 11, 2020 @11:43AM (#23143325)
And IIRC this holds MongoDB and I'd assume other non-SQL DBs.

If you have an index on <companyId, userId>, and you query with a userID only, that index wont be used. But if the index was <userId, companyId> then that index would be used. Or if you supplied both userId and companyId in your query, then either index would work.

Re:Advanced SQL and database books and resources by JoshuaDavid on Monday May 11, 2020 @06:01PM (#23147623)
And yes, this occasionally means that adding a

    where id in (select id from company)
sometimes will switch your query from doing a full table scan to using an index, fixing your problem for long enough to prepare a fix to add the appropriate index. Not that I've ever had to do something like that or anything.
Re:Advanced SQL and database books and resources by branko_d on Monday May 11, 2020 @05:51PM (#23147525)
> If you have an index on <companyId, userId>, and you query with a userID only, that index wont be used.

Surprisingly, it could be used in some circumstances, just not for the regular seek.

If the index is small (compared to the base table), the DBMS may decide to perform a full index scan (instead of the full table scan), especially if your SELECT list doesn't contain columns which are not in the index.

And Oracle can employ so called "skip scan" if it realizes that the number of distinct companies is small. This is essentially a separate seek under each distinct company.

Re:Advanced SQL and database books and resources by mrits on Monday May 11, 2020 @12:05PM (#23143570)
I think you just need to realize that an index on <companyId, userId> is a single index
Re:Advanced SQL and database books and resources by tarasmatsyk on Monday May 11, 2020 @01:21PM (#23144554)
Ha-ha, man, that is a gem comment I am getting your book
Re:Advanced SQL and database books and resources by eatonphil on Monday May 11, 2020 @08:40AM (#23141438)
I have also enjoyed his blog. Here's another way to understand indexes by implementing them in a simple SQL database. This includes both storing/retrieving indexed values and also deciding whether or not a query has an applicable index.

https://notes.eatonphil.com/database-basics-indexes.html