Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.Sign up
Fix performance of including relationships #1800
Including a relationship causes very slow performance. I first noticed this when including a relationship that had a
Since my invoices table had thousands of rows, this query returns many duplicates of the same supplier.
Making the request use the
changed the title
fixed issue where relation query returns thousands of duplicate rows
Mar 26, 2018
That's weird. Looking at the failed test it seems like the two expected objects in the response array have their order swapped, even though they are correct individually. I've never seen this particular error before. Can you check if there could be some kind of race condition here? It's important that any async tests always return promises or call
Also, what Node version are you using?
Looking at the test more closely it's a bit weak since there shouldn't be any requirement (or guarantee) that the columns are retrieved in any particular order. I'll ignore that failed test, but I still need to review this more carefully to make sure no new bugs or breaking changes are introduced and I don't have time for that right now.
left a comment
I'm merging this even with the tests failing because they only fail for PostgreSQL and only because the order of the expected results isn't as expected, but there's also no
The issue of failing tests will be fixed with a later PR.
Apr 12, 2018
1 check failed
referenced this pull request
Apr 23, 2018
Apologies if this is not the place to put this - thank you for this great fix, but one (rare) GOTCHA is that
My solution was to create a "computed" column using
select * from table order by case when col1 is not null then col1 else col2 end
select *, case when col1 is not null then col1 else col2 end as order_col from table order by order_col
(obviously all achieved via knex, not raw sql)
No probs. In the instance that I'm fixing it it's a fairly complicated query, but this is the key bit:
for reference, this used to look like:
The "fixed" way is actually much neater and, like I said, I only wanted to post this in case someone stumbled across it in the future (relevant XKCD https://xkcd.com/979/)