r/SQL • • 17d ago

Discussion Tell me your stories about FillFactor.

I just had an interview question on it. It’s something I know but never use. I thought it was one of those features folks stopped using because of solid state drives. The interviewer disagreed.

FillFactor a percentage setting on an index that tells rebuilds how full to make the pages. That way you can avoid expensive page splits.

6 Upvotes

14 comments sorted by

4

u/SQLBek 17d ago

SQL Server - FILL FACTOR is still important because page DENSITY is still important. I am guessing that it is external fragmentation that you are thinking is no longer relevant.

And for the most part, it's fragmentation is not as impactful, but not because of SSDs. I cover this in my EightKB online deep dive presentation that you can find on YouTube, but in a nutshell, fragmentation is representative of the data pages logical placement within a data file. That's decoupled from how those data pages may wind up ultimately being written down to storage, whatever it may be - spinning rust, direct attached SSD, virtualized disks, or a SAN (for example).

1

u/2-buck 17d ago

I didn’t explain that well. Really I just want to hear the what peoples adventures are with it. I’m not a DBA. I’m just a developer. But sometimes we discuss lowering fill factor. And I suggest doing fewer rebuilds and let the table reach their own density. Folks don’t like that idea because of avg_fragmentation_in_percent. So I point out that that’s for external fragmentation and that they should look at avg_page_space_used_in_percent.

And then we get into the problem of ola hallengrens indexOptimize script. It doesn’t check the right one. And they don’t want to mess with it.

1

u/raks1991 17d ago

One practical reason maintenance scripts key off avg_fragmentation_in_percent is that sys.dm_db_index_physical_stats returns NULL for avg_page_space_used_in_percent in LIMITED mode, and LIMITED is the default and the cheap mode people run against big indexes. Getting density means SAMPLED (a 1 percent page sample, approximate) or DETAILED (every page), which the docs rank as progressively slower:

SELECT index_id, index_level, page_count, avg_page_space_used_in_percent
FROM sys.dm_db_index_physical_stats(
     DB_ID(), OBJECT_ID(N'dbo.YourTable'), NULL, NULL, 'SAMPLED');

One correction to 'it only applies on rebuild': the ALTER INDEX docs say REORGANIZE also compacts pages, and the compaction is based on the existing fill factor. What's limited to create and rebuild is an explicit FILLFACTOR = n.

1

u/SteveDBA 17d ago

I think you’re confusing fill factor with disk defragmentation. It’s not really about spinning disks versus SSDs.

I’ve seen environments where fill factor was set to 80% and index maintenance hadn’t been performed in nearly two years. Some indexes were over 100 GB. Just getting regular index rebuilds back in place made a significant difference in stored procedure performance, with CPU usage and disk I/O dropping considerably.

That said, I wouldn’t recommend 80% across the board. It really depends on the workload and insert/update patterns.

1

u/2-buck 17d ago

Was it set to 80 during the 2 years of no index maintenance? My understanding it has no impact on any instruction except rebuild.

1

u/SteveDBA 17d ago

Yes, 80% was the default during that time. But fill factor wasn’t really the issue. Some of the indexes had fragmentation as high as 98%, and rebuilding them made a significant difference in performance.
I did adjust fill factor on certain indexes, moving some to 90% based on their insert and update patterns.

1

u/luigicanu 16d ago

Not my story, but this is a great presentation by Jeff Moden.
https://www.youtube.com/watch?v=JYYHalwjBwA

1

u/2-buck 16d ago

He commented and sent this. I watched it. Amazing! He learned from what I’m going through and then dove deep into the pages and figured it out. He suffered the post rebuild mondays. And then took advice from Brent Ozark and Eric darling and suffered the opposite lesson. Impressive.

2

u/Thadrea Data Science Manager 17d ago

No particular stories, but it absolutely is not something that has gone away because of SSDs.

There is still going to be a performance impact from the order the rows are stored in on the disk. It may be smaller than it was with magnetic hard drives, but it's still there.

2

u/ResidentPomegranate6 17d ago

fillfactor only sticks on rebuild, reorg ignores it, and ascending keys insert right-edge anyway so 100 is fine there.

2

u/Significant_Tune9219 17d ago

The SSD argument covers the random I/O cost, but a page split still costs CPU, extra log records, and leaves two half-full pages, so the index gets bigger and scans read more pages regardless of the storage. Where it matters most is indexes on keys that insert into the middle, like GUIDs or natural keys, while an ever-increasing identity key mostly splits at the end and is fine at 100. Setting a lower value on everything just bloats indexes, so checking page splits and fragmentation per index before changing it is the usual approach.

1

u/raistlin49 17d ago

Here's a story about fill factor...join a new shop...they're complaining about chronic index fragmentation regardless of daily maintenance...tables have nonsequential UNIQUEIDENTIFIER PKs and a bunch of indexes with FILLFACTOR=100. Guess what happens when you put those together? Page split on literally every insert. As soon as you get some free space on all those half pages...boom...index maintenance...back to page splits lol

1

u/markwdb3 When in doubt, test it out. 17d ago edited 17d ago

Which DBMS software? How it works, and if it even exists for a given DBMS, varies.

0

u/The_Orracle 17d ago

Literally just used it tonight!

DECLARE u/ObjectID int = OBJECT_ID(N'dbo.Customer_D', N'U');

IF u/ObjectID IS NULL

THROW 50000, 'dbo.Customer_D was not found or is not visible.', 1;

SELECT

i.index_id,

i.name AS IndexName,

i.type_desc AS IndexType,

i.is_unique AS IsUnique,

i.is_disabled AS IsDisabled,

i.has_filter AS HasFilter,

i.filter_definition AS FilterDefinition,

i.fill_factor AS 'FillFactor',

SUM(p.row_count) AS ApproxRows,

CAST(SUM(p.used_page_count) / 128.0 AS decimal(18, 2)) AS UsedMiB,

CAST(SUM(p.reserved_page_count) / 128.0 AS decimal(18, 2)) AS ReservedMiB,

t.name AS AccountNumberType,

c.max_length AS AccountNumberMaxBytes,

c.is_nullable AS AccountNumberIsNullable

FROM sys.indexes AS i

JOIN sys.dm_db_partition_stats AS p

ON p.object_id = i.object_id

AND p.index_id = i.index_id

JOIN sys.columns AS c

ON c.object_id = i.object_id

AND c.name = N'CustomerAccountNumber'

JOIN sys.types AS t

ON t.user_type_id = c.user_type_id

WHERE i.object_id = u/ObjectID

GROUP BY

i.index_id, i.name, i.type_desc, i.is_unique, i.is_disabled,

i.has_filter, i.filter_definition, i.fill_factor,

t.name, c.max_length, c.is_nullable

ORDER BY i.index_id;

SELECT

i.name AS IndexName,

ic.key_ordinal AS KeyOrdinal,

c.name AS ColumnName,

t.name AS DataType,

c.max_length AS MaxBytes,

c.is_nullable AS IsNullable,

ic.is_included_column AS IsIncluded

FROM sys.indexes AS i

JOIN sys.index_columns AS ic

ON ic.object_id = i.object_id

AND ic.index_id = i.index_id

JOIN sys.columns AS c

ON c.object_id = ic.object_id

AND c.column_id = ic.column_id

JOIN sys.types AS t

ON t.user_type_id = c.user_type_id

WHERE i.object_id = u/ObjectID

AND i.type IN (1, 2)

AND (ic.key_ordinal > 0 OR ic.is_included_column = 1)

ORDER BY i.index_id, ic.is_included_column, ic.key_ordinal, ic.index_column_id;