How to: Change actual execution method from “row”

2019-07-31 13:48发布

I am having some major issues. When inserting data into my database, I am using an INSTEAD OF INSERT trigger which performs a query.

On my TEST database, this query takes much less than 1 second for insert of a single row. In production however, this query takes MUCH longer (> 30 seconds for 1 row).

When comparing the Execution plans for both of them, there seems to be some CLEAR differences:

  1. Test has: "Actual Execution Method: Batch"
  2. Prod has: "Actual Execution Method: Row"
  3. Test has: "Actual number of rows: 1"
  4. Prod has: "Actual number of rows 92.000.000"

Less than a week ago production was running similar to test. But not anymore - sadly.

Can any of you help me figure out why?

I believe, if I can just get the same execution plan for both, it should be no problem.

Prod  environment database - Using Row method and no predicate (really slow)

Test environment database - using Batch method and predicate (Fast)

3条回答
祖国的老花朵
2楼-- · 2019-07-31 13:54

You don't get to force row vs. batch processing directly in SQL Server. It is a cost-based decision in the optimizer. You can (as you have noticed) force a plan that was generated that uses batch mode. However, there is no specific "only use batch mode" model on purpose as it is not always the fastest. Batch mode execution is like a turbo on a car engine - it works best when you are working with larger sets of rows. It can be slower on small cardinality OLTP queries.

If you have a case where you have 1 row vs. 92M rows, then you have a bigger problem with having a problem that has high variance in the number of rows processed in the query. That can make it very hard to make a query optimal for all scenarios if you have parameter sensitivity or the shape of the query plan internally can create cases where sometimes you have only one row vs. 92M. Ultimately, the solutions for this kind of problem are either to use option(recompile) if the cost of the compile is far less than the variance from having a bad plan or (as you have done) finding a specific plan in the query store that you can force that works well enough for all cases.

Hope that helps explain what is happening under the hood.

查看更多
再贱就再见
3楼-- · 2019-07-31 13:59

I have found a somewhat satifying solution to my problem.

By going into Query store of the database, using Microsoft SQL Server Management Studio, I was able to Force a specific plan for a specific query - but only if the plan was already made by the query.

查看更多
时光不老,我们不散
4楼-- · 2019-07-31 14:18

Sometimes using query hint OPTION(hash Join) helps to force a query plan to use batch processing mode. The following query that uses AdventureWorks2012 sample database demonstrates what I am saying.

SELECT s.OrderDate, s.ShipDate, sum(d.OrderQty),avg(d.UnitPrice),avg(d.UnitPriceDiscount)
FROM Demo d 
join Sales.SalesOrderHeader s
on d.SalesOrderID=s.SalesOrderID
WHERE d.OrderQty>500
GROUP BY s.OrderDate,s.ShipDate

The above query uses row mode. With the query hint it then uses batch mode.

SELECT s.OrderDate, s.ShipDate, sum(d.OrderQty),avg(d.UnitPrice),avg(d.UnitPriceDiscount)
FROM Demo d 
join Sales.SalesOrderHeader s
on d.SalesOrderID=s.SalesOrderID
WHERE d.OrderQty>500
GROUP BY s.OrderDate,s.ShipDate
OPTION(hash Join)
查看更多
登录 后发表回答