4 ms·
A few other tips: 1) A reminder that when you have a foreign key, such as Order table has a UserId column, and UserId has a FK to User -> Id table, there is an
by plasma 6y ago
A few other tips:
1) A reminder that when you have a foreign key, such as Order table has a UserId column, and UserId has a FK to User -> Id table, there is an implicit lookup that will happen on the Order table if you attempt to delete a User, to validate its deletion wont violate the Order UserId FK.
For example:
DELETE FROM User WHERE Id = 1
This will require the database to first check that won't violate a FK on Order.UserId:
-- Implicitly (something like this) is done to validate UserId = 1 is not in use on Order.UserId that has a FK to User.
SELECT Count(*) FROM Order WHERE UserId = 1
This can lead to poor update/delete performance on the User table (if the row was deleted or the User.Id was ever changed-- a convoluted example), unless you remember to place an index on Order.UserId column to help with FK enforcement.
2) If you use SQL Server, and you have a large table, and a few times a month for some reason queries that were working fine yesterday suddenly timeout, but then start to run quickly again for a while, its probably because of SQL Server suddenly deciding to recalculate statistics for its optimizer, and the query that is timing out, is the lucky query chosen to require the recalculation to happen first.
The recalculation usually involves SQL Server scanning the table before proceeding ahead with your query, and if the query times out, its re-computation of stats is rolled back (so it keeps failing, until it eventually gets to complete the query).
A workaround that has worked for us is to enable asynchronous statistics, so that the re-computation won't block the query that happened to trigger the re-computation in the first place:
https://www.mssqltips.com/sqlservertip/2904/sql-servers-auto-update-statistics-async-option/ https://www.mssqltips.com/sqlservertip/2904/sql-servers-auto...