-
Notifications
You must be signed in to change notification settings - Fork 2.2k
graph: fix inefficient query for IsPublicNode #10356
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
Summary of ChangesHello @Abdulkbk, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request addresses a significant performance bottleneck in the Highlights
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
This pull request refactors the IsPublicV1Node SQL query to improve performance, particularly for the SQLite backend. The change replaces an OR condition in a JOIN with a UNION ALL construct, which is a sound optimization strategy to help the query planner use indexes more effectively. The change is logical and well-explained. I have one minor suggestion regarding SQL formatting to ensure consistency.
9c698ba to
ca19c8e
Compare
|
can you create some stress tests to see whether union is actually better ? |
ca19c8e to
08ce7a8
Compare
I added a benchmark as the first commit, the following is what I found after running: go test -tags=test_db_sqlite -bench=BenchmarkIsPublicNode > sqlite_or.txt
# then
go test -tags=test_db_sqlite -bench=BenchmarkIsPublicNode > sqlite_union.txt
# finally
benchstat sqlite_or.txt sqlite_union.txt
The fact that this benchmark is run with approximately 8k nodes means that, if the number of nodes is increased to around ~80k, the difference will become more noticeable. |
08ce7a8 to
a0fa327
Compare
|
@Abdulkbk, remember to re-request review from reviewers when ready |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hello @Abdulkbk, I ran the benchmark as is and got the following numbers.
go test -tags=test_db_sqlite -bench=BenchmarkIsPublicNode
*** SELECT WITH UNION ***
goos: linux
goarch: amd64
pkg: github.com/lightningnetwork/lnd/graph/db
cpu: AMD Ryzen 7 7840HS w/ Radeon 780M Graphics
BenchmarkIsPublicNode-16 122 9719683 ns/op
--- BENCH: BenchmarkIsPublicNode-16
test_sqlite.go:49: Creating new SQLite DB for testing
PASS
ok github.com/lightningnetwork/lnd/graph/db 119.003s
Then changed the go.mod line 216:
from replace github.com/lightningnetwork/lnd/sqldb => ./sqldb
to //replace github.com/lightningnetwork/lnd/sqldb => ./sqldb
And ran it again :
*** SELECT USING OR ***
goos: linux
goarch: amd64
pkg: github.com/lightningnetwork/lnd/graph/db
cpu: AMD Ryzen 7 7840HS w/ Radeon 780M Graphics
BenchmarkIsPublicNode-16 93 11786240 ns/op
--- BENCH: BenchmarkIsPublicNode-16
test_sqlite.go:49: Creating new SQLite DB for testing
PASS
ok github.com/lightningnetwork/lnd/graph/db 120.685s
The BenchmarkIsPublicNode using SELECT WITH UNION took 9.7ms and using SELECT WITH OR 11.7ms.
| graph := MakeTestGraph(b) | ||
|
|
||
| // Create a graph with a reasonable number of nodes and channels. | ||
| numNodes := 8000 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After changing the number of nodes to 16000 I got:
*** SELECT WITH UNION ***
go test -tags=test_db_sqlite -bench=BenchmarkIsPublicNode
goos: linux
goarch: amd64
pkg: github.com/lightningnetwork/lnd/graph/db
cpu: AMD Ryzen 7 7840HS w/ Radeon 780M Graphics
BenchmarkIsPublicNode-16 93 11939604 ns/op
--- BENCH: BenchmarkIsPublicNode-16
test_sqlite.go:49: Creating new SQLite DB for testing
PASS
ok github.com/lightningnetwork/lnd/graph/db 212.488s
Then changed the go.mod line 216:
from replace github.com/lightningnetwork/lnd/sqldb => ./sqldb
to //replace github.com/lightningnetwork/lnd/sqldb => ./sqldb
*** SELECT USING OR ***
And ran it again :
goos: linux
goarch: amd64
pkg: github.com/lightningnetwork/lnd/graph/db
cpu: AMD Ryzen 7 7840HS w/ Radeon 780M Graphics
BenchmarkIsPublicNode-16 48 24982592 ns/op
--- BENCH: BenchmarkIsPublicNode-16
test_sqlite.go:49: Creating new SQLite DB for testing
PASS
ok github.com/lightningnetwork/lnd/graph/db 228.831s
The BenchmarkIsPublicNode using SELECT WITH UNION took 11.9ms and using SELECT WITH OR 24.9ms.
a0fa327 to
398364b
Compare
In this commit we add a benchmark to test the performance of IsPublicNode query.
In this commit we updated the IsPublicV1Node query to use UNION instead of OR, since sqlite struggles to efficiently use multiple indexes in a single query involving OR conditions across different columns. We use UNION ALL since the query doesn't care about duplicates.
398364b to
004d49c
Compare
|
Thanks @MPins, for taking a look. I've addressed the feedback. |
fixes #10337 (partially)
Change Description
In this PR, we rewrite the query for
IsPublicV1Node, which returns a boolean based on if a node has a public or private channel. This particularly fixes an issuesqlitebackend as it struggles to efficiently use multiple indexes across seperate columns in one query.Step to test