The symptom
Some plans contain an index that you never created. SQL Server calls the operator an Index Spool (logical operation Eager Spool): it reads a whole table, builds an index on it in tempdb, and then seeks into that copy. SQLite calls it an AUTOMATIC INDEX. In both cases the optimizer decided that repeating a full scan for every outer row would cost more than building an index first. The index is discarded when the statement ends and is built again on the next execution.
It is the engine telling you exactly which index is missing. The query plan viewer flags both operators. The examples use a shop schema with 20,000 customers and 400,000 orders.
Diagnose it
SQL Server: Index Spool
This query fetches the latest order of each German customer. There is no index on Orders.CustomerId:
SET STATISTICS IO ON;
GO
SELECT c.CustomerId, x.OrderDate, x.Total
FROM dbo.Customers AS c
CROSS APPLY (
SELECT TOP (1) o.OrderDate, o.Total
FROM dbo.Orders AS o
WHERE o.CustomerId = c.CustomerId
ORDER BY o.OrderDate DESC
) AS x
WHERE c.Country = 'DE';
Table 'Worktable'. Scan count 4000, logical reads 1165641, physical reads 0
Table 'Orders'. Scan count 1, logical reads 2579, physical reads 0
Table 'Customers'. Scan count 1, logical reads 115, physical reads 0
Orders is scanned once, and nearly all the reads are on Worktable: that is the spool being built and then searched once per customer. The text plan names the operator and the columns it indexes:
SET SHOWPLAN_TEXT ON;
GO
SELECT c.CustomerId, x.OrderDate, x.Total
FROM dbo.Customers AS c
CROSS APPLY (
SELECT TOP (1) o.OrderDate, o.Total
FROM dbo.Orders AS o
WHERE o.CustomerId = c.CustomerId
ORDER BY o.OrderDate DESC
) AS x
WHERE c.Country = 'DE';
GO
SET SHOWPLAN_TEXT OFF;
|--Index Spool(SEEK:([o].[CustomerId]=[learnfx].[dbo].[Customers].[CustomerId] as [c].[CustomerId]))
その SEEK predicate of the spool is the key of the index you need. The columns the spool outputs are the ones to include.
SQLite: AUTOMATIC INDEX
SQLite builds an automatic index when a join has no index on either side. This query matches orders to customers by timestamp, and neither column is indexed:
EXPLAIN QUERY PLAN
SELECT c.customer_id, o.order_id
FROM customers AS c
JOIN orders AS o ON o.order_date = c.created_at;
|--SCAN c
`--SEARCH o USING AUTOMATIC COVERING INDEX (order_date=?)
The line names the table and the column: SQLite will read all of orders, build an index on order_date, and use it for each customer. Nothing is stored in the database file; the schema has no new index afterwards.
To see what the query would do without it, turn the feature off for the connection:
PRAGMA automatic_index = OFF;
EXPLAIN QUERY PLAN
SELECT c.customer_id, o.order_id
FROM customers AS c
JOIN orders AS o ON o.order_date = c.created_at;
PRAGMA automatic_index = ON;
|--SCAN c
`--SCAN o
Two nested scans: 20,000 passes over 400,000 rows. The automatic index is the better of two bad options, which is why automatic_index is on by default.
Fix it
Create the index the engine keeps building
In SQL Server, use the spool's seek columns as the key, add the ORDER BY column so the latest order is found without a sort, and include the other columns the query reads:
CREATE INDEX IX_Orders_CustomerId_OrderDate
ON dbo.Orders (CustomerId, OrderDate DESC) INCLUDE (Total);
GO
SELECT c.CustomerId, x.OrderDate, x.Total
FROM dbo.Customers AS c
CROSS APPLY (
SELECT TOP (1) o.OrderDate, o.Total
FROM dbo.Orders AS o
WHERE o.CustomerId = c.CustomerId
ORDER BY o.OrderDate DESC
) AS x
WHERE c.Country = 'DE';
Table 'Orders'. Scan count 4000, logical reads 12777, physical reads 0
Table 'Customers'. Scan count 1, logical reads 115, physical reads 0
The worktable is gone and each customer costs one seek.
CREATE INDEX orders_date_idx ON orders (order_date);
EXPLAIN QUERY PLAN
SELECT c.customer_id, o.order_id
FROM customers AS c
JOIN orders AS o ON o.order_date = c.created_at;
|--SCAN c
`--SEARCH o USING COVERING INDEX orders_date_idx (order_date=?)
Run ANALYZE in SQLite
SQLite chooses join order and automatic indexes from table sizes. Without statistics it has to guess them. Run ANALYZE once the tables hold realistic data, so that the plan you inspect is the plan production will get:
ANALYZE;
When to leave it alone
A permanent index costs space and slows every write to the table. For a report that runs once a night, letting the engine build a throwaway index can be the cheaper arrangement. Create the index when the query runs often, or when the build dominates its run time. In SQL Server, compare the reads on Worktable with the rest of the query to judge that.
The checklist
Work through it in order. Each step gives you the evidence for the next one.
- Look for Index Spool (Eager Spool) in SQL Server plans or AUTOMATIC INDEX in SQLite plans.
- Read the seek columns of the spool or the column named in the AUTOMATIC INDEX line.
- In SQL Server, check the Worktable reads in SET STATISTICS IO output.
- Create a permanent index on those columns, with the other columns the query reads included.
- Capture the plan again and confirm the spool or automatic index is gone.
- Weigh the index against how often the query runs and how heavily the table is written.
Questions that come up
What is an Eager Index Spool in SQL Server?
It is an operator that reads all rows from its input, builds a temporary index on them in tempdb, and then serves seeks from that index. The optimizer adds it under a nested loop when the table has no suitable index. The temporary index is rebuilt every time the query runs.
What does AUTOMATIC INDEX mean in SQLite?
SQLite will build a temporary index for the duration of the statement because the join or lookup has no permanent index to use. EXPLAIN QUERY PLAN shows it as SEARCH ... USING AUTOMATIC INDEX or AUTOMATIC COVERING INDEX, with the indexed column in parentheses.
Is an index spool bad?
It is better than the plan the optimizer would otherwise have chosen, but it is repeated work: the same index is built from scratch on every execution. If the query runs regularly, a permanent index removes that cost.
Should I turn off automatic_index in SQLite?
Usually not. Turning it off does not make the query faster; it replaces the temporary index with repeated full scans. It is useful as a diagnostic, to make missing indexes show up as slow queries during testing.
Where does the Worktable in STATISTICS IO come from?
Worktable is a temporary structure in tempdb used by spools, some sorts and hash operations. Large logical reads on Worktable next to a plan with an Index Spool are the cost of building and searching the spool.