Finding blocking/locking queries in MS SQL (mssql)

You may find this query useful: SELECT * FROM sys.dm_exec_requests WHERE DB_NAME(database_id) = ‘YourDBName’ AND blocking_session_id <> 0 To get the query itself use this one: SELECT text,* FROM sys.dm_exec_requests CROSS APPLY sys.dm_exec_sql_text(sql_handle) WHERE DB_NAME(database_id) = ‘YourDBName’ AND blocking_session_id <> 0

Can I optimize a SELECT DISTINCT x FROM hugeTable query by creating an index on column x?

This is likely not a problem of indexing, but one of data design. Normalization, to be precise. The fact that you need to query distinct values of a field, and even willing to add an index, is a strong indicator that the field should be normalized into a separate table with a (small) join key. … Read more

Is possible to reuse subqueries?

You can take the aggregations out into a CTE (common table expression): with minima as (select t.id, t.type, min(value) min_value from table2 t where t.type in (1,2,3,4) group by t.id, t.type) select a.id, a.name, (select min_value from minima where minima.id = subquery.id and minima.type = 1) as column1, (select min_value from minima where minima.id = … Read more

How can I force a subquery to perform as well as a #temp table?

There are a few possible explanations as to why you see this behavior. Some common ones are The subquery or CTE may be being repeatedly re-evaluated. Materialising partial results into a #temp table may force a more optimum join order for that part of the plan by removing some possible options from the equation. Materialising … Read more

What is the optimal way to compare dates in Microsoft SQL server?

Converting to a DATE or using an open-ended date range in any case will yield the best performance. FYI, convert to date using an index are the best performers. More testing a different techniques in article: What is the most efficient way to trim time from datetime? Posted by Aaron Bertrand From that article: DECLARE … Read more

techhipbettruvabetnorabahisbahis forumu