-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Extremely slow while invalidating all login sessions of a certain user #1653
Comments
Thank you for the report and the work put into your PR. I checked the code, and we we only query for Line 145 in 9dbf8b5
I haven't checked a full SQL dump but by scanning quickly through the code it appears that the
Please note that the migration system is filebased. New migrations have to be added in a new file that has a larger number than existing ones. In this case, you would create |
Also as a side note, iirc, you don't need extra keys for any things that are foreign keys, or unique keys, as the RDBMS already has indices for faster lookup for those. So changes such as this one do not actually make a difference :) |
Hi @aeneasr, Thanks for your reply! I did test what you said about "only adding index of subject on table". In fact this is the first index I tried to add. This image are the pgadmin explain analyze results(using query like The difference of the real delete command(the same user, start from the same database content) is like 5 seconds vs. ? minutes(I didn't really wait until it finish, it's just too long) Here are some references on other forums talked about this symptom: And there is a statement said: "Postgres doesn’t automatically create indexes on the referencing side of foreign key relationships." on this article. |
Thank you for the links, I didn't know that cascading deletes on foreign keys or unique fields don't actually have fast lookup indices! In that case, you're absolutely right, of course. This appears to be database specific. MySQL for example appears to create index keys automatically on foreign keys (but only InnoDB!): https://stackoverflow.com/questions/304317/does-mysql-index-foreign-key-columns-automatically But then again, this only works when you're doing In that case your changes make sense and we'd definitively accept a the patch as a PR! For it to be merged though, all SQL files would have to go to into a new file as I described in my previous comment :) Thank you! |
Hi @aeneasr , Is it OK that I also add indices for other foreign keys? My co-worker said there are other indices should be added which will be useful while revoking consent sessions. |
Hi @aeneasr , Foreign keys already have indices or have been set as primary key:
==========================================================
==========================================================
I think we also need to create indices for these foreign keys, otherwise the performance will drop again while during the delete query. |
Yes, that sounds perfect - thank you! :) |
Signed-off-by: rickwang <rickwang@synology.com>
Signed-off-by: rickwang <rickwang@synology.com>
Signed-off-by: rickwang <rickwang@synology.com>
Signed-off-by: rickwang <rickwang@synology.com>
closes #1653 Signed-off-by: rickwang <rickwang@synology.com>
Describe the bug
Hundreds of thousands of records in PostgreSQL could cost more than 10 minutes to invalidate all login sessions of a certain user.
Reproducing the bug
Steps to reproduce the behavior:
Server logs
No error logs in this case.
Server configuration
services:
hydra-migrate:
depends_on:
- postgresd
image: ${HYDRA_VERSION}
environment:
- DSN=postgres://${DB_USER}:${DB_PASSWORD}@${DB_HOST}:5432/${DB_NAME}?sslmode=disable
command:
migrate sql -e --yes
restart: on-failure
hydra:
depends_on:
- hydra-migrate
image: ${HYDRA_VERSION}
volumes:
- .:/config
ports:
- ${HYDRA_PUBLIC_PORT}:4444 # Public port
- ${HYDRA_ADMIN_PORT}:4445 # Admin port
command:
serve all --dangerous-force-http --config /config/config.yml
environment:
- LOG_LEVEL=debug
- URLS_SELF_ISSUER=${HYDRA_URLS_SELF_ISSUER} # must set to public port
- URLS_CONSENT=${HYDRA_URLS_CONSENT}
- URLS_LOGIN=${HYDRA_URLS_LOGIN}
- URLS_LOGOUT=${HYDRA_URLS_LOGOUT}
- DSN=postgres://${DB_USER}:${DB_PASSWORD}@${DB_HOST}:5432/${DB_NAME}?sslmode=disable
- SECRETS_SYSTEM=${SECRETS_SYSTEM}
restart: unless-stopped
postgresd:
image: postgres:9.6
volumes:
- ${DB_VOLUME}:/var/lib/postgresql/data
ports:
- "5433:5432"
environment:
- POSTGRES_USER=${DB_USER}
- POSTGRES_PASSWORD=${DB_PASSWORD}
- POSTGRES_DB=${DB_NAME}
Expected behavior
The request should finish in less a minute under this kind of amount.
Environment
Additional context
I already figured out that there are 6 indices needed to be add:
My fix patch is ready to send a pull request: rickwang7712@8b98807
Hope I can make contribution this time.
The text was updated successfully, but these errors were encountered: