-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Performance Improvement Suggestion: Replace OR with UNION in IDataStorage SQL Queries for PostgreSQL and MSSQL #1599
Comments
Hello @onurogurtani , Thank you for your feedback. While using I’m reconsidering whether we should have added certain columns as indexes during table creation. In fact, most queries in IDataStorage use the majority of columns in their WHERE conditions. So, which columns should we designate as indexes to optimize performance? At the moment, it seems that What do you think about this? @maisa92 @xiangxiren Of course, replacing Thanks! |
Hello @yang-xiaodong We are using the following two nonclustered indexes:
We handle around 1 million transactions daily. Success records are deleted after one day, leaving approximately 1 million records in the table at any given time. We are using 6 pods on Kubernetes, with the following CapOptions configuration:
|
Yang, I'm interested in this topic. Are there any updates? |
Hi @onurogurtani , I am working on this PR and need to point out that in PostgreSQL So, in PostgreSQL and MySQL we will still continue to using the following SQL. SELECT "Id", "Content", "Retries", "Added", "ExpiresAt"
FROM {_pubName}
WHERE "Version" = @Version
AND (
("ExpiresAt" < @TwoMinutesLater AND "StatusName" = '{StatusName.Delayed}')
OR
("ExpiresAt" < @OneMinutesAgo AND "StatusName" = '{StatusName.Queued}')
)
FOR UPDATE SKIP LOCKED; |
Yang, would you like to try this? We can solve the problem you mentioned using a subquery:↳ Another alternative could be to completely separate these two queries.
|
This has an error in PostgreSQL :
It can works in MySQL but does not utilize indexes.
|
The explain for SQL in MySQL:
|
I think that by running the two queries independently or separating them into different methods, we can effectively overcome the limitations with FOR UPDATE SKIP LOCKED in PostgreSQL when using UNION ALL. This strategy also allows for better index utilization in both PostgreSQL and MySQL, improving overall performance. |
We have applied the same query conditions on both sides of the |
I'm excited to try it out; thank you! |
… the query SQL to utilize the optimized indexes. (#1599)
Hi Yang, I think the development is complete. How can I test it? Are you planning to release a preview package? |
@onurogurtani Version |
Thank you :) |
Fixed in version 8.3.1 |
@yang-xiaodong We encountered this error after upgrading to 8.3.1.
|
Include almost the table columns in index? Why? "INCLUDE" looks like something harmful. |
Hello,
I am using CAP in a .NET Core project, and I would like to raise a concern regarding the SQL queries generated in the IDataStorage implementations for PostgreSQL and MSSQL. Currently, we have queries that use the OR operator, which looks like this:
PostgreSQL:
MSSQL:
Our DBA team has suggested replacing the OR operator with UNION in these queries to improve performance, especially when dealing with large datasets. This is due to the fact that the OR operator can sometimes lead to suboptimal index usage and degrade query performance.
Here’s how the updated queries in the IDataStorage implementations would look with UNION:
PostgreSQL:
MSSQL:
This change could potentially improve the performance of the queries in the IDataStorage implementations, and I'd appreciate it if you could consider this suggestion. It would be great to see this improvement in future releases of the CAP library.
Thank you!
Best regards,
Onur
The text was updated successfully, but these errors were encountered: