-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfeed.json
29 lines (29 loc) · 6.13 KB
/
feed.json
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
{
"version": "https://jsonfeed.org/version/1",
"title": "CafeSql",
"description": "",
"home_page_url": "https://pollusb.github.io/gh-pages",
"feed_url": "https://pollusb.github.io/gh-pages/feed.json",
"user_comment": "",
"author": {
"name": "Pollus"
},
"items": [
{
"id": "https://pollusb.github.io/gh-pages/how-to-improve-t-sql-query-performance.html",
"url": "https://pollusb.github.io/gh-pages/how-to-improve-t-sql-query-performance.html",
"title": "How to improve T-SQL query performance",
"summary": "T-SQL (Transact-SQL) is a dialect of SQL that is used to interact with Microsoft SQL Server databases. It allows you to write queries that can manipulate data, create tables, views, stored procedures, functions, triggers and more. However, writing efficient and performant T-SQL queries can be…",
"content_html": "<p>T-SQL (Transact-SQL) is a dialect of SQL that is used to interact with Microsoft SQL Server databases. It allows you to write queries that can manipulate data, create tables, views, stored procedures, functions, triggers and more. However, writing efficient and performant T-SQL queries can be challenging and requires some knowledge and skills. In this blog post, I will share some tips and best practices on how to optimize your T-SQL queries and improve their performance.</p>\n<h2 id=\"1-use-indexes-wisely\">1. Use indexes wisely</h2>\n<p>An index is a data structure that helps SQL Server find data faster by organizing it in a sorted order. Indexes can speed up your queries by reducing the number of pages that need to be scanned or read from disk. However, indexes also have some drawbacks: they consume disk space, they need to be maintained when data changes (which can cause overhead), and they can affect the performance of insert, update and delete operations.</p>\n<p>Therefore, you should use indexes wisely and only create them on columns that are frequently used in query predicates (such as WHERE, JOIN, ORDER BY or GROUP BY clauses). You should also avoid creating too many indexes on the same table or overlapping indexes that cover the same columns. As a best practice, most selective columns (those that have more distinct values) should be placed leftmost in the key of a non-clustered index.</p>\n<p>To find out which indexes are useful or unused for your queries, you can use tools such as <strong>Database Engine Tuning Advisor</strong> or <strong>Query Store</strong>. These tools can analyze your workload and suggest optimal indexes based on various factors such as query frequency, cost and impact.</p>\n<h2 id=\"2-avoid-unnecessary-joins\">2. Avoid unnecessary joins</h2>\n<p>A join is an operation that combines data from two or more tables based on a common column or condition. Joins are essential for relational databases but they can also be expensive in terms of performance. The more tables you join, the more complex the query becomes and the more resources it consumes.</p>\n<p>Therefore, you should avoid unnecessary joins whenever possible and only join tables that provide relevant information for your query result. You should also choose the appropriate join type for your scenario: inner join (returns only matching rows from both tables), left outer join (returns all rows from the left table plus matching rows from the right table), right outer join (returns all rows from the right table plus matching rows from the left table) or full outer join (returns all rows from both tables).</p>\n<p>To optimize your joins further, you can use techniques such as:</p>\n<ul>\n<li>Filtering data before joining: use WHERE clauses to eliminate unwanted rows from each table before joining them</li>\n<li>Reducing data size: use SELECT clauses to select only necessary columns from each table instead of selecting all columns (*)</li>\n<li>Using derived tables: use subqueries to create temporary tables that contain only relevant data for joining</li>\n<li>Using common table expressions (CTEs): use CTEs to define reusable expressions that can simplify complex queries involving multiple joins</li>\n<li>Using indexed views: use indexed views to pre-compute frequently joined data sets and store them as physical objects with indexes</li>\n</ul>\n<h2 id=\"3-use-set-based-operations-instead-of-cursors-or-loops\">3. Use set-based operations instead of cursors or loops</h2>\n<p>T-SQL supports both set-based operations and procedural logic such as cursors or loops. Set-based operations are those that operate on a whole set of rows at once using SQL statements such as SELECT, INSERT,\nUPDATE or DELETE. Procedural logic is those that operate on one row at a time using control flow statements such as WHILE,\nFOR or IF.</p>\n<p>Set-based operations are usually faster than procedural logic because they leverage SQL Server’s ability to process data in bulk using optimized algorithms and parallelism. Procedural logic often involves row-by-row processing which can cause high CPU usage,\nmemory consumption and disk I/O.</p>\n<p>Therefore, you should prefer set-based operations over procedural logic whenever possible and avoid using cursors or loops unless absolutely necessary. If you need to use cursors or loops for some reason,\nyou should try to minimize their impact by:</p>\n<ul>\n<li>Declaring cursors as local instead of global</li>\n<li>Choosing cursor types carefully based on their characteristics (forward-only vs scrollable,\nstatic vs dynamic vs keyset-driven)</li>\n<li>Closing cursors explicitly when done</li>\n<li>Using batch updates instead of single-row updates</li>\n<li>Limiting loop iterations by using break conditions</li>\n</ul>\n<h2 id=\"4-use-parameters-instead-of-literals\">4. Use parameters instead of literals</h2>\n<p>Parameters are placeholders</p>\n",
"image": "https://pollusb.github.io/gh-pages/media/posts/1/LinkedIn-background.jpg",
"author": {
"name": "Pollus"
},
"tags": [
"sqlserver"
],
"date_published": "2023-03-16T16:01:35-04:00",
"date_modified": "2023-03-17T18:26:43-04:00"
}
]
}