What causes a query to run slow?
Queries can become slow for various reasons ranging from improper index usage to bugs in the storage engine itself. However, in most cases, queries become slow because developers or MySQL database administrators neglect to monitor them and keep an eye on their performance.
- Do not use * in select Statment.
- Use Exists instead of Sub Query.
- Use Proper join instead of subqueries.
- Use “Where” instead of “Having” a clause.
- Apply index on necessary columns.
- For user-defined stored procedures avoid prefixes like “sp_”
...
Featured Work in Programming
- Instead of UPDATE, use CASE. ...
- Reduce nested views to reduce lags. ...
- Data pre-staging. ...
- Use temp tables. ...
- Avoid using re-use code. ...
- Avoid negative searches. ...
- Avoid cursors. ...
- Use only the correct number of columns you need.
look at the execution plan to find clues. Replace the * with the actual column names - don't know why, but it sometimes helps. add index to each column and try (until you find the best one) try to change the primary key's index type - try Clustered instead of Non-Clustered.
Statistics are one of the main factors that may affect query performance because it stores analytical information about the distribution of table column values. This statistical data is used by the optimizer to estimate how many rows will return when a query is executed.
- Ensure your TempDB database is configured optimally. ...
- Make sure you're running index maintenance frequently. ...
- Implement indexes that provide a benefit to your queries. ...
- Check your most expensive queries and stored procedures. ...
- Monitor your performance counters.
- TIP 2: Use SELECT <columns> instead of SELECT * ...
- Tip 3: Avoid running queries in a loop. ...
- Tip 4: Does My record exists? ...
- Tip 5: A big NO for correlated subqueries. ...
- Tip 6: Use wildcard characters wisely. ...
- Tip 7: Avoid using SQL function on the RHS of the operator.
Slow queries are frequently caused by combining two or more large tables together using a JOIN. Review the number of joins in your query, and determine if the query is pulling more information than is actually needed.
- Generate an Actual Execution Plan. ...
- Monitor Resource Usage. ...
- Use the Database Engine Tuning Advisor. ...
- Find Slow Queries With SQL DMVs. ...
- Query Reporting via APM Solutions. ...
- SQL Server Extended Events. ...
- SQL Azure Query Performance Insights.
- Create a directory to work in: mkdir db-analysis && cd db-analysis.
- Make it executable: chmod +x pt-query-digest.
- Download your slow-query. ...
- Run pt-query-digest over the log file: ./pt-query-digest slow-query.
How do you check query performance?
Use the Query Store page in SQL Server Management Studio
In Object Explorer, right-click a database, and then select Properties. Requires at least version 16 of Management Studio. In the Database Properties dialog box, select the Query Store page. In the Operation Mode (Requested) box, select Read Write.
- Ensure your TempDB database is configured optimally. ...
- Make sure you're running index maintenance frequently. ...
- Implement indexes that provide a benefit to your queries. ...
- Check your most expensive queries and stored procedures. ...
- Monitor your performance counters.

Checking Your Slow Query Logs
You must ensure that the variable slow_query_log is set to ON, while the slow_query_log_file determines the path where you need to place your slow query logs. If this variable is not set, it will use the DATA_DIR of your MySQL data directory.