๐ SQL, like a pink asterisk, stands between "SELECT" and "FROM table_with_an_incomprehensible_name_that_I_need".
๐ Feeling poetic today, aren't we?
๐ฌ But aside from that, let's talk about some tips to improve your query performance (but first, leave a follow, or you might accidentally DROP the user database in production next time):
- When using SELECT, you don't always need to fetch all columns from the table, save resources;
- If you need a subquery as a filter, and its result is not influenced by the main query, sometimes it's more performant to incorporate it into a WITH clause and use it within the main query, saving you from calling it for each record in the table;
- Limit the number of joins on massive tables or restrict their scope;
- ORMs are nice but add an extra layer between you and the database, making things heavier. Use them sparingly;
- Watch out for non-clustered indexes; too many on a table can slow down queries instead of speeding them up, especially on very large tables, they can consume a lot of disk space;
- If you have tables accumulating gigabytes of data, consider periodic purging. Data like logs is rarely useful after 15 years;
- If you find yourself repeatedly running a complex query involving multiple tables, consider creating a dedicated view. Read-only access to the view will be faster, and it will always stay up to date.
๐ Feel free to suggest more in the comments below
#sql #database #developer #performanceTips