Nota
L'accesso a questa pagina richiede l'autorizzazione. È possibile provare ad accedere o modificare le directory.
L'accesso a questa pagina richiede l'autorizzazione. È possibile provare a modificare le directory.
Si applica a:SQL Server
Database SQL di
AzureIstanza gestita di SQL di
AzureDatabase SQL in Microsoft Fabric
Quando FORCEPLAN è impostato su ON, l'ottimizzatore di query di SQL Server elabora una join nello stesso ordine in cui le tabelle appaiono nella clausola FROM di una query. Inoltre, impostare FORCEPLAN ON impone l'uso di un loop join annidato a meno che non siano necessari altri tipi di join per costruire un piano per la query, o vengano richiesti con join hints o query hint.
Convenzioni relative alla sintassi Transact-SQL
Sintassi
SET FORCEPLAN { ON | OFF }
Osservazioni:
SET FORCEPLAN di fatto sovrascrive la logica usata dall'ottimizzatore di query per elaborare una Transact-SQL istruzione SELECT. I dati restituiti dall'istruzione SELECT sono gli stessi, indipendentemente dall'impostazione. L'unica differenza è la modalità in cui SQL Server elabora le tabelle per soddisfare la query.
È inoltre possibile usare gli hint di Query Optimizer in query che hanno effetto sulla modalità di elaborazione dell'istruzione SELECT in SQL Server.
SET FORCEPLAN viene applicato a tempo di esecuzione o di esecuzione e non a tempo di analisi.
Autorizzazioni
SET FORCEPLAN I permessi sono predefiniti per tutti gli utenti.
Esempi
Nell'esempio seguente viene eseguito il join di quattro tabelle. L'impostazione dell'opzione SHOWPLAN_TEXT è abilitata in modo che SQL Server restituisca le informazioni sulla diversa modalità di elaborazione della query dopo l'abilitazione di SET FORCE_PLAN.
USE AdventureWorks2022;
GO
-- Make sure FORCEPLAN is set to OFF.
SET SHOWPLAN_TEXT OFF;
GO
SET FORCEPLAN OFF;
GO
SET SHOWPLAN_TEXT ON;
GO
-- Example where the query plan is not forced.
SELECT p.LastName, p.FirstName, v.Name
FROM Person.Person AS p
INNER JOIN HumanResources.Employee AS e
ON e.BusinessEntityID = p.BusinessEntityID
INNER JOIN Purchasing.PurchaseOrderHeader AS poh
ON e.BusinessEntityID = poh.EmployeeID
INNER JOIN Purchasing.Vendor AS v
ON poh.VendorID = v.BusinessEntityID;
GO
-- SET FORCEPLAN to ON.
SET SHOWPLAN_TEXT OFF;
GO
SET FORCEPLAN ON;
GO
SET SHOWPLAN_TEXT ON;
GO
-- Reexecute inner join to see the effect of SET FORCEPLAN ON.
SELECT p.LastName, p.FirstName, v.Name
FROM Person.Person AS p
INNER JOIN HumanResources.Employee AS e
ON e.BusinessEntityID = p.BusinessEntityID
INNER JOIN Purchasing.PurchaseOrderHeader AS poh
ON e.BusinessEntityID = poh.EmployeeID
INNER JOIN Purchasing.Vendor AS v
ON poh.VendorID = v.BusinessEntityID;
GO
SET SHOWPLAN_TEXT OFF;
GO
SET FORCEPLAN OFF;
GO
Vedi anche
SELECT (Transact-SQL)
SET Istruzioni (Transact-SQL)
SET SHOWPLAN_ALL (Transact-SQL)
SET SHOWPLAN_TEXT (Transact-SQL)