What is Page Life Expectancy (PLE), what makes it drop, and how can I manage memory better? Abusing disks slows many database servers, and it’s often something you can fix with tuning and not spending extra money on better disks or more memory. It takes a very long post to get through all of that, but if you stick with me through this then you’ll be looking at your servers from new angles with an effort that will be noticed by the Sys Admins, SAN Admin, the users, and your boss.
Before we being, there are some ground rules we need to get out of the way defining PLE and understanding there are external memory factors. There’s no shame in skipping that and going straight to the focus of this post. Personally, I find the next two sections a little dry, but I’m also my harshest critic!
Before we get too deep into it, lets make sure we’re on the same page on a couple things.
Page Life Expectancy is the number of seconds the average page of data has been in the buffer pool. Keeping the data in memory gives SQL Server quicker access to it instead of making the long, slow trip to disk. While none of the counters in SQL Server means everything on its own, this one can open your eyes and lead you towards issues that can be resolved.
Keep in mind that SQL reads the data pages from the buffer pool, always from the buffer pool. If the data you need isn’t there then SQL Server does a physical read to put it there. After that’s done it will do a logical read to use the page that’s now in memory. If you want to dive into the detail you can do it here, specifically with the reading pages and writing pages links on that page.
That physical read is going to disk, the slowest part of your server, to read the page(s) from disk, be processed by the CPU, possibly decrypting it if you’re using TDE, then placing it into memory. This makes PLE critical because, even if you ignore the extra load you just placed on the I/O system and CPU, you’re waiting for an additional, slower action to take place.
The speed of your disk does matter, but it also doesn’t really matter. This is an EXTRA step with SQL Server doing a physical read THEN a logical read, not instead of a logical read. Also, your persisted storage is the slowest part of your server, be it spinning disks, SSD, flash, or anything else. The expensive stuff just isn’t AS slow.
You can find your page life expectancy in sys.dm_os_performance_counters using my post on OS Perf Counters. That post will also help you realize how much load you take off of your disks by raising PLE, then you’re really going to start to understand the burning drive picture you find there. While it’s great that I have the code out there to get this yourself, the tracking of this counter should be coming from your monitoring software.
The problem is that many people see the 300 value for Page Life Expectancy you can still find documented is wrong, very wrong. If you have a server with 64 GB of memory with 56 GB allocated to SQL Server, that means you’re reading about 56 GB of data from disk every 300 seconds. If you look at Page 36 of Troubleshooting SQL Server – A Guide for the Accidental DBA by Jonathan Kehayias and Ted Krueger you’ll see an actual scalable version of this; PLE should be 300 for every 4 GB of RAM on your server. That means for 64 GB of memory you should be looking at closer to 4,800 as what you should view as a critical point.
The reason you see 300 written everywhere is because we were limited by 32-bit servers for so long while at the same time SQL Server documentation was really being developed well. Those servers could only have 4 GB of memory, thus making this number make sense as well as the Buffer Pool / 4 GB * 300 formula. I’ll go so far as to say that you should be cautious of anything that doesn’t have a formula to it because our servers keep getting faster, our databases keep getting bigger, and static values…don’t.
Jonathan Kehayias also put this in his blog post Finding What Queries in the Plan Cache Use a Specific Index, and that had a great series of comments going back and forth between him and Brent Ozar. Brent is discussing not putting so much faith in PLE and watching wait stats instead as different speed I/O systems can greatly sway the impact of low PLE or even make it so a high PLE just isn’t high enough. They’re both right, and if you can understand the intent of each one then you’ll be in a much more comfortable place when tuning your servers.
If you force me to take a side on that post, Jonathan wins out over Brent here. This is because Jonathan is talking about having a higher PLE, which in turn reduces load on the disks and typically reduces the amount of work that needs to be done to execute a query. Brent is basically implying that money may have already be thrown at the issue, which negates the current symptoms on what could be a less scalable solution. However, you can’t discount Brent’s logic here as the things that are limiting your server’s performance at this time lie in the Wait Stats, not this counter.
Quick Rant: We’re Not Alone
So, you want higher PLE to show that you’re using your resources better, and the only way PLE goes up is by leaving data in the data cache once it’s there. The problem is, there’s a lot going on that wants to work the other way. Even if you have 64 GB of memory dedicated to SQL Server to host a single 64 GB database it won’t all fit in memory, that’s because other things want to play as well.
First, your memory isn’t completely dedicated to the buffer pool. Memory pressure from the OS or VMs changing size can drop the size of the buffer pool has unless you’re using “lock pages in memory”, which is not recommended in most situations especially on VMs. If you’re not on a completely dedicated box, which means a physical box with no apps, no GUI, no RDP sessions, no SQL Server, no network card driver, etc., then this can happen. Wait a minute…you can’t uninstall SQL Server to make sure SQL Server’s buffer pool remains untouched, yet SQL Server uses memory outside of this area for any purpose that isn’t specifically assigned to the buffer pool and that memory usage can get out of control. If there’s memory pressure then some pages are forced out of data cache to drop the size of the buffer pool, leaving you open to more contention and a lower PLE.
Second, your buffer pool isn’t dedicated to your data cache as it also houses your proc cache, which are execution plans stored to be run again. These plans take a lot of CPU to create and reusing them saves you a lot of resources, so you can’t complain that you’re sharing space here. SQL Server determines the amount of the buffer pool to assign to each, and it’s possible for either side to have unnecessary data in it. I’m going to focus on the data cache here, so I’ll defer the abuse of proc cache conversation to Kimberly Tripp. There’s more to it than just her post, but this is a great place to start understanding what’s there and not in use.
Focus, Focus, Focus
Ok, so we know what PLE is and that external factors can affect it, but that’s not really why you’re here, is it? Are you here because you have your data cache and you want to manage it to keep your PLE going up…maybe a little. My guess is that you’re here because you heard that PLE is critical to your server’s performance and you want to increase performance by raising this counter the right way. You do that in two ways.
First, make sure you have as much room as possible. If your server can handle more memory it’s often cheaper to buy that much memory than it is to intervene on the database side. Tuning and purging scales well, will help a lot more than just memory management, and is the best thing you can do given the time budgeted to it, but it just isn’t as quick and easy as a memory upgrade in many cases. Even if you buy memory now and tune later, it will continue to help. 64 GB of memory (it’s 128 GB max on Standard Edition now), as Brent Ozar pointed out here, is only a couple hundred dollars here. FYI, I absolutely hate throwing money at issues, and I still listed this first. Partially because it’s an easy fix to an often neglected issue, and partially because every fix is throwing money at the problem because your time isn’t worthless.
Second, keep as little in memory as possible to make efficient use of the space you have. Yeah, it’s that easy. Well, at least it’s that easy to say, but doing it takes a little more work. There are a list of ways you can trim down on the space you need.
- Drop Unused Indexes
- Merge Duplicate Indexes
- Use Your Indexes – SARGability
- Watch for Big Queries
- Look in Your Proc Cache for Opportunities
- Know What’s in Your Buffer Pool
- Index Maintenance – Defrag
- Index Maintenance – Statistics
- Purge Your Data
Through all of this, please remember that your initial thought may be to raise a counter, but is that your real goal? I’m betting you want your server to run faster, and PLE is merely one counter that helps put a value on performance. Servers with high PLE can run horrible, they just don’t need to keep running back to disk. Servers with low PLE can run great, they just need to go back to disk to get what they need.
A little bit ago I mentioned buying more memory, this will raise your PLE without making any queries really run more efficiently. Sure, they’re more likely to have their data in cache which is great, but they’re doing just as many logical reads and using just as much CPU to do their joins. The rest of this post isn’t like that, the rest is making queries more efficient in a way that just happens to make your PLE go up.
Drop Unused Indexes
This part’s tricky. SQL 2005-2008 R2 told you how much indexes were used since the last time SQL Server was started in the DMV dm_db_index_usage_stats. It’s still there in SQL 2012 and beyond, but these statistics get reset when you rebuild an index now. That means that best case scenario on a server that’s patched monthly, you have a month’s worth of data to go off of and you can’t even rely on that being the whole picture anymore. I get into more about how to keep track of this over time in my post Indexes – Unused and Duplicates.
This has a minor implication on PLE because as the pages of your unused indexes are updated SQL Server has to read part of the B+tree into memory to find the page, then it has to read the page to be updated into memory, it updates it in memory and marks it as a dirty page, then eventually writes it back to disk. The key here being that pieces are read into memory, AKA the buffer pool, the part of SQL Server you’re trying to clean up.
Merge Duplicated Indexes
This one’s not as tricky, but the performance issues are much worse. The problem is that you have two indexes, we’ll call them ix_A and ix_B, that are very similar. They have the same first two key fields, and stray a little after that. The differences mean that ix_A will be better for one query and ix_B will be better for another, and that means SQL Server will be dragging them both into cache. Sure, if you take the columns that are in ix_B that aren’t in ix_A and include them in ix_A then drop ix_B then the queries that used to use ix_B will probably need to do a little more work, but you’ll end up with a more efficient use of the cache. Your typical testing will show you the worst case scenario here, because you’ll run it each way multiple times and see what it’s like when the indexes are completely in cache. However, the real world is more friendly than that (that’s a first), and the query that used to use ix_B may actually run faster because now it’s using an index that’s more likely to be in cache, cutting down on physical reads. Going to the same blog post, Indexes – Unused and Duplicates, you can see details on how to help relieve this problem.
There’s a bit of an art to this at first, but you’ll get it down to a science with practice.
Use Your Indexes – SARGability
SARGability (Search ARGument capable) is it’s own topic, and it deserves at least one post of its own. You need to understand how indexes work, as stated in my post Indexing Fundamentals, and make sure that your queries can take advantage of indexes. If you look in a phone book for people with the last name like ‘%ood’ it’s no good because you need the first letter of the last name to take advantage of that index. If you look up people with the first name ‘Steve’ then that’s useless because the first key field was last name. Doing functions, implicit conversions, and many other techniques can cause you to need to scan an entire index.
In terms of PLE, that means you read the entire index into memory instead of just the pieces you need. That can add up to a lot of data being kicked out of cache to make room for a lot of physical reads from disk.
This isn’t an easy topic, and I’m not sure of any resources that say everything that needs to be said on the topic. While I have plans to write a post on it and will update this one when I do, it’s still an unwritten post. Until that time comes, Google “SQL Server SARGable” and see what pops up.
If you know how to read execution plans, there are two sections on a seek, scan, or lookup that you need to know here. Predicate and Seek Predicate. Predicate is what it had to scan for, while a seek predicate is what it could find efficiently using the fact that the key fields are sorted.
Watch for Big Queries
It doesn’t matter if you use Extended Events or Profiler Traces, you need to know when large queries are being run on your servers. If you want to watch this at a statement level then you’ll want to make the jump to Extended Events, but I’ve never seen it hurt to watch SQL Batch Completed and RPC Completed filtered only by duration over 10 seconds.
The point of this when it comes to PLE is that the queries that aren’t SARGable or don’t have an index to take advantage of will be doing a lot of scans. The bigger the scan the longer the query will take to run, and if you care about PLE then you’ll know what queries those are. Sure, some are legitimately doing enough work where they’ll always take that long to run, but most of the queries that take a while can be tuned. Be it changes to the query itself (should be your first instinct) or indexing changes, there’s often a way to have a query require less data in cache.
This is an extremely useful technique to use in correlation with watching for drops in PLE. If it drops then check to see what was running at the time. This shows you what queries were running, which would be most of your issues, but it could be outside of the query itself and actually be something like a large auto update statistics task being kicked off that you won’t see here.
Know What’s in Your Buffer Pool
Very few people do this, yet it’s so obvious once you start. You want to raise PLE, right? PLE is a measurement of how long stuff stays in your buffer pool, and drops because something else needed to be put in there. So, what’s in there that’s taking up all your space and how’d it get there? I felt like a fool that it took me years to come up with that questions, but then I realized that most people never ask it at all.
It was actually one of my biggest tuning revelations since I read Grant Fritchey’s Execution Plans book. And you always know it’s a great revelation when you find yourself yelling at yourself for not realizing it earlier…years earlier.
So, how do you do this? Query your cache to find the indexes that take up the most space there, pick one that is an excessive amount of space or doesn’t look like it belongs there, query the proc cache to find out where that index is used, and tune that query either through code changes or indexing changes. The scripts to do this and a more detailed description of the process can be found in my post Cleaning up the Buffer Pool to Increase PLE, and a newer post of mine, Query the Buffer Pool, has an even better script to see what’s in cache across all databases on the instance at once in a more efficient query.
Note that if your PLE is low then what’s in your buffer pool will be changing quickly. That does NOT mean the scripts in the posts I just mentioned are useless, it means you’ll come up with new opportunities each time you run them.
Look In Your Proc Cache for Opportunities
Lets start by saying this isn’t perfect. These numbers get reset throughout the day and some queries never make it in here at all. That’s saying this isn’t an all-inclusive, one-stop shop. It is in no way saying that you can’t make amazing improvements on the queries you find here.
There are two way you can use the cache. Most people know that you can get your most expensive queries such as on the MSDN sys.dm_exec_query_stats page, and you can use your imagination to sort this by any of the counters available here. That will give you an idea of what needs tuning, if you needed somewhere to start. I love taking this a step further and monitoring that exact information so I have a historical view instead of just what happens to be in cache now. You can read all about that in my post Query Stats.
The other way is parsing through the XML in the plans to find specific items. I heard that grunt when I mentioned XML, and you’re not alone. Not many of us are good at XML; Jonathan Kehayias is an extreme exception here. He gives you his queries so you can run with it, do what he does, and alter it to go even further. This is exactly what I did in my post Cleaning up the Buffer Pool to Increase PLE that I just mentioned in the last section.
Index Maintenance – Defrag
Many people only thing about defragmenting their indexes to help get contiguous reads on their disk, but that’s only half the story. In fact, the better you do everything in this post the less you’ll have to worry about your disks.
However, you also have to keep in mind how full each page of your indexes is. If you have an index page that’s 100% full then you have a full 8kb of data there. Add another row and you now have 8.1kb of data that’s split between two 8kb pages, so you’re wasting almost 50%. 50% isn’t even your worst case scenario because pages are never automatically removed or merged if records are deleted unless you delete every row stored in that page. Paul Randal’s post Performance Issues From Wasted Buffer Pool Memory takes a deeper look at this problem, and Ola Hallengren’s scripts can help you with a solution with trusted scripts to clean up fragmentation.
Index Maintenance – Statistics
It may seem obvious to do your index defrag job off hours. While I’ve seen servers without the job, I’ve never seen one scheduled for mid-day. That’s a good thing, because a lot of data needs to be pulled into cache to play around with indexes like that.
However, do you have auto update statistics turned on? Do NOT turn it off because of this, but understand that it updates the statistics by reading about 1% of the index into cache. If that’s a 100 GB index then you’re reading 1 GB of data which is a descent portion of your cache. To help avoid this, you should be updating your stats off hours, and Ola Hallengren’s scripts can help with this, too.
You need to update your stats because as more and more updates are made to a table the less accurate your stats are, making your execution plans less accurate. Once you update about 20% of the rows of a table since the last time your statistics were updated then they’ll automatically be updated again if you leave the default option turned on. Again, don’t go turning it off because of this post alone. Instead, schedule your stats to be updated off-hours and only the tables that have 20% of their rows updated throughout the day will get their stats updated automatically, and that will stop most auto-updates on the larger tables that would cause issues.
Purge Your Data
We talked a lot about what to do with the data you have, but do you need all of your data? Step away from SQL Server once and go talk to an accountant. Ask them where they have paper work from last month and they’ll point to a filing cabinet. Now, knowing they’re legally obligated to keep a lot of stuff for 7 years, ask them where a 5-year-old document is. It’s not right in front of them, but they know where to look for it. It’s not taking up valuable space in their office (read: not on the main production server), often not in the same building they’re in (read: not online), and it will take a bit to get to it. They no longer have an active business use for it, and they can get to it in the rare case that it’s needed.
Ask them where something is that’s 8 years old, and they’ll find a friendly way to tell you that they bought an OCD pyromaniac a pair of scissors and a book of matches. We’re more eloquent with deleting things in batches, but their way sounds like a lot more fun.
If they kept everything in one room would you call that room a cluttered, unmanageable mess? If they kept historical records forever, would you think they were being obsessive and wasteful with purchasing storage?
So, can you pull up a detailed sales report for February 29th, 2000 from Prod for me?
This one’s an eye opener, and one that I believe is a bug enough where I opened a connect item on the issue. The larger your TempDB data files the more space you’ll find in your buffer pool used by unallocated pages in TempDB.
I go into details in my post TempDB Memory Leak?, but here are the basics. TempDB can use memory up to the size of the data files, not just the size of the used space in the data files. The only workaround I know of right now is to make the size of the data files smaller.
I’m not talking about making TempDB tiny and letting it grow, there’s no excuse for that. Look at your monitoring software to see how large TempDB has to be to avoid hitting autogrowth outside of accidental issues, then size TempDB about 20% larger than that. I specifically avoid the best practice of presizing TempDB to fill a dedicated drive for this reason alone.
Other…Am I Giving Up?
There are too many things to list in one blog post. You could write a book on this subject. I listed what I feel will help you the most, but I also wanted to take time to let you know that my list isn’t some magical, all-inclusive, everything you can do to make your servers run better. It’s a start. Hopefully a good one you found to be productive, but a mere start no matter how you look at it.
Speaking of “you could write a book on the subject”, well, they did. A lot of them did. Some even made them free PDFs. Not bootleg bit-torrent copies, but actual real, legal, free PDFs made possible by Red Gate in their book selection on SQL Server Central
Some books you have to buy, if that’s your thing:
- SQL Server 2012 Internals by Kalen Delaney, Bob Beauchemin, Conner Cunningham, and Jonathan Kehayias
- Professional SQL Server 2012 Internals and Troubleshooting by Christian Bolton, Rob Farley, Glenn Berry, Justin Langford, Gavin Payne, and Amit Banerjee
Also, I’m not the only person blogging about this topic:
- Paul Randal talks about NUMA nodes and PLE
- Paul Randal talks about wasted space in the buffer pool that would kill your PLE
- Jonathan Kehayias talks about the plan cache and PLE
- Brent Ozar does comprehensive tuning and even offers online classes
- Kendra Little explains index tuning and how you should just DO IT!
If you did all of this and there’s something running on your servers to trend performance, you’ll notice:
- Page Life Expectancy: Raised significantly, but you saw that coming
- Page Reads/sec: (Physical Reads) Dropped because we’re not cycling data in and out of cache as much
- % Processor Time: Dropped due to lower I/O, more efficient queries, less pressure on procedure cache, etc.
- Critical query execution time: Typically less due to less chance of waiting for physical reads and lower CPU stress.
- PageIOLatch wait types: Dropped due to fewer physical reads leading to fewer waits on physical reads.
- DBA Pay Rate: If this isn’t on the list, try using a chart of the above counters
The largest part of memory management in SQL Server is indexing. This can be changing the indexes themselves or how your queries interact with them. Doing either requires a great understanding of what indexes are and how they work. I’ve written several posts on the topic and recently added them to my Indexing page to help you browse them easier.
- Sys.dm_os_performance_counters Demystified (simplesqlserver.com)
- Cleaning Up the Buffer Pool (simplesqlserver.com)
- SQL Server Running Slow (simplesqlserver.com)
- Indexes – Unused and Duplicates (simplesqlserver.com)
- Extended Events Intro (simplesqlserver.com)
Howdy sir! Good post, but one quick note to clarify something. You write, “Queries don’t read from disk and they don’t write to disk.” Well, check those STATISTICS IO results for work tables – don’t forget that SQL Server can write to TempDB in order to handle those sorts & joins.
That’s an excellent point, and one that I should have had on there even with a more simplified view of things. My goal with my entire blog is to help anyone look like a DBA-II, and a DBA-II better know how TempDB is used!
I updated this from “Queries don’t read from disk and they don’t write to disk.” to “Queries don’t read from or write to disk for the data they want, only to TempDB for processing.” It’s still keeping with the easier approach I was going for while being more complete and accurate.
Steve, I wanted to take a moment to thank you for yet another awesome post. With all these different topics that are new to me as I get started as a DBA, I don’t always have time to research everything as thoroughly as I’d like to, but your posts are always great resources and I wanted to at least take the time to thank you and let you know when I’ve read one that’s helpful. Thanks again for this and all the other advice you’ve given me so far!
You’re welcome. It’s always great to hear that I’m helping.
I am trying to report on this data with SCOM, have you integrated this into a monitoring and alerting system before?
I appreciate your thoroughness here and will bookmark this for troubleshooting in the future thanks for the insight!
My monitoring has always been done as a standalone product while being conscious of not overlapping alerting systems too much. Since every system is different in what it collects and if you can reasonably query it, there is no single standard that I can recommend.
I LOVED your last talk in SQLPass Pittsburgh, was super awesome. I cannot wait to start applying those techniques. Please keep posting 😀
Thank you. I haven’t forgotten about my promise to upload my slide deck, and i hope to have that here for you soon.
I want to share my problem here.
We have enough memory on our server and 21-27GB are available for cache.
I don’t have many databases and the largest is about 28GB, 15GB are on the transactions table.
I checked cache contents, it notes I only use 1.8GB for this DB and 4GB for all databases.
When I run a query that reads all records from transactions (15GB)
That DB cache grows to 17GB, that is great.
After about an hour it is back to below 2GB for no reason I know of.
I want to share some ideas I have to the cause:
1. Sql gets request from the system to free unwanted memory. Probably originates from vm.
2. An sql process arises from time to time, raising non cache memory needs. (at the moment memory used for locking records is about 500MB – can this suddenly grow?)
3. Replication cleanup job deletes many records, creating many dirty pages inside the cache.
Following is the solution to the problem I had.
Due to a bug the Lazy writer cleared too much memory. Fixed by installing SP2.
Details in link:
Excellent !! Nice crucial information !!
I was late to your class that one time in Pittsburgh but what little I did see was awesome. Well done, Steve. And, then I run across this post on the subject. You can just bet I book marked this in about 4 places because it not only covers PLE but also covers so many performance aspects that manifest themselves as a simple drop in PLE.
Again, very well done, Steve!
Jeff, I thought I responded to this about 8 months ago!!! Thank you for the compliment, and hopefully I’ll be seeing you in Pittsburgh again in about 2 months.
For everyone else, it’s not some secret meeting between us. Come to SQL Saturday Pittsburgh on September 30, 2017.
I am new to this after many years with Oracle. I can’t see much value to PLE. Is that all there is? What good is a page hanging around in the cache if it isn’t accessed after it gets placed there? Cache hit rate would be much more valuable.
Well, both are true. If the data in cache is very transient then you’re obviously going to disk more often, so a low PLE is going to be a low cache hit. Since SQL Server holds data that is hit more in cache longer then this correlation is even more true. While this doesn’t necessarily mean the data in memory is being used, it does mean that memory isn’t in such bad shape that relatively frequently used data is getting the boot.
No counter is going to be perfect, especially on its own. Read this on Cache Hit Ratio written by Jonathan Kehayias to see some of the ups and downs of that counter: https://www.red-gate.com/simple-talk/sql/database-administration/great-sql-server-debates-buffer-cache-hit-ratio/
This is a well detailed blog. Thanks!