Wednesday, November 23, 2005

How often should stats be collected?

If you ask Microsoft (or Oracle from version 10g), then just leave it to the database and the clever developers to find out when new stats should be gathered.

Right.

When you collect new stats you are almost guaranteed to change some execution plans in your system. Do you want that? No, you want the same execution plans as long as they have the response time your users want. They haven't asked you for a better (or worse) plan.

Here's a response I saw on a list the other day:

-------------------------------------------------------------
Oracle suggests the following best practices for collecting statistics. http://www.oracle.com/technology/books/pdfs/book_10g_chap9_ce1.pdf

· Collect statistics for normal data dictionary objects using the same interval that you would analyze objects in your schemas. In addition, you need to analyze the dictionary objects after a sufficient amount of DDL operations have occurred.

· Use the procedures gather_database_stats or gather_schema_stats with options set to GATHER AUTO. With this feature, only the objects that need to be re-analyzed are processed every time.

· For fixed objects, the initial collection of statistics is usually sufficient. A subsequent collection is not usually needed, unless workload characteristics have changed dramatically. In the next section, we will examine the changes introduced
-------------------------------------------------------------

And here's my reply:
---------------------------------------------------------------------------
I'd argue that you should be scared of socalled best practices on this and other issues.

How can people seriously write about best practices, when in reality we're talking practices that worked for a few sites that will never, ever resemble your site anyway?

Better to document more and more worst practices, as Cary Millsap suggested several years ago - that way you raise the bar instead of just placing it at a certain position.

By the way: Note the vagueness: "...the same interval" (what interval?), "...after a sufficient amount of DDL operations have occurred" (how much?). Can't be otherwise, but that doesn't make it more usable.

It's like the recommendations from Microsoft regarding SQL Server: Always run automatic stats gathering. Always set parallel to automatic. Always....

Two schools inside Oracle are interesting to study (and we're talking the real experts, not the document writers):

Benchmark guys: Collect 1% estimate after initial load, then never again.

Real World Performance group: Collect compute stats once, then never again.

But there's a better way, I think:

Analyze if it's neccessary!

It's only neccessary if the response time for a SQL statement becomes worse. So monitor the elapsed_time and the hash_plan_value (or plan_hash_value) in V$SQL to see if something goes South. If yes, one possible reason could be stats that are no longer representative for the data (more employees in department 42, much more data due to aquisition of a new company, whatever). THEN it's time to analyze.

Don't do it if you don't have to. Monitor the things that matter.

The reason you can't find any useful rules is that ... you can't find any useful rules.
------------------------------------------------------------------

Plan_hash_value and elapsed_time were added to V$SQL in 9i. Before that you'd need to do your own hashing after an "explain plan for..." command in SQL*Plus and store the result. Peter Gram from Miracle has done it for a 7.3 database.

I could talk for ages about this. How customers have run splendidly for years without analyzing (or precisely because of it), and how it's always ground-hoc day tuning when something changes: A system doesn't "become slow", an application doesn't "become slow". It's one or more SQL statements that change execution plan(s) for the worse. Since we never monitor those we do the same thing over and over again: Find the SQL statement with the largest response time, tune it, back to start.

But what if the SQL statement with the longest response time was NOT the one that changed? What if the SQL statement with the longest response time already had been optimised as best as could be done? That happens more often than you'd like to know.

So instead of zero'ing in on the SQL statement that changed execution plan (and therefor response time) for the worse, and return it to its former state or find out why it went bad, we end up tuning another statement that might not be easily tunable.

Hence my abstract for the Hotsos Symposium 2006: "No, Cary, we're STILL not tuning the right SQL statements" :-).

18 Comments:

Anonymous Anonymous said...

I've written a capacity planner a couple of years ago based on statspack, korn shell etc. I've always used it to do forecasts but more importantly - detect trend breaches. I've noticed that on many sites a trend breach on performance statistics happen after gathering stats.
Just this week I was thinking about adding a module to my program to monitoring execution plans. SO - if I know a trend breach has occured AND I know which execution plans changed for the worse then, I *know* where to look instead of wasting my time searching for the needle in the hay stack...

So *great* idea for a paper !

3:31 AM  
Blogger Moans Nogood said...

That makes sense! I discussed these issues with Jonathan Lewis this morning, and apparently he's writing a few "notes" as he likes to call his papers, along these lines.

2:24 PM  
Anonymous Anonymous said...

An excellent topic. However I just wanted to play devil's advocate on the "when to analyse" side of things.

Is there not an argument for telling the optimiser as much truth as you can and hence analysing frequently to achieve this? I agree you may occasionally cause plans to change but you are occasionally going to experience the flip side of the problem by letting the stats stay stale and having a static plan become an inefficient one.

Either way you still need to be on top of it (using a sql plan monitor like you describe).

Just a thought.

Neil

PS Really enjoyed the Oak Table Day at UKOUG - thanks for putting that on.

1:40 AM  
Anonymous Anonymous said...

Mogens,

This is a very interesting topic and I've swung backwards and forwards (and sideways sometimes) over the years.

On the one hand, it seems sensible to give the optimizer the most accurate information that you can. It's pointless having an optimiser that can respond to changes in data distribution if you tell it not to.

On the other, I've always found it very scary that execution plans will change on a production application without anyone really understanding why. Even if the optimiser picks a better plan, the inconsistency would *still* worry me. Sure, you could find out why, but it's going to take time. Unpredicted changes in behaviour are difficult to explain to managers and business types. Well, they can be explained, but that doesn't mean they will be popular!

Statistics gathering has been one of the hot subjects at work this week because of an initial data load for a production system, followed by go-live and the subsequent population of tables that, as far as the CBO is concerned, are all empty. It seems even more likely that you will hit problems with changing execution plans in the early months of the life of a new system, just when you want good performance!

When you're a day-to-day DBA, it's amazing how often you hit the same old problems.

3:42 AM  
Anonymous Anonymous said...

Performance is very slow.

What's changed ?

Well, we did a large data load and gathered new stats.

Why ?

Well, we have a nightly job to recompute stats for all objects.

Why ?

Because Oracle recommends always updating stats after a large data load.

I see. Well let's reinstate the old statistics to buy us some time while we analyse what's going on ?

What exactly do you mean by 'reinstate the old stats' ?

5:14 AM  
Anonymous Anonymous said...

In 10gR1, the DBMS_STATS package now includes procedures to specify a stats retention history of N days. Correspondingly, there are RESTORE_*_STATS procedures to revert information to old values at different levels of granularity (object, schema, database). One can also lock stats at different levels so that they will not get overwritten if you like things the way they are and do not anticipate a need to change them. Hopefully maintaining some baseline statspack report and/or sql_trace/tkprof output of typical user interactions / batch jobs can help one pinpoint the reason why performance has changed (for better or worse) at a database or operation level.

8:14 PM  
Anonymous Anonymous said...

One exception to the "never re-gather stats" would be with rolling partitions, of course. There you'd constantly be regathering statistics on partitions, and maybe at the global level as well. On the other hand you'd stop gathering statistics at the partition level once each was full.

Would it be fair to say that the aim of not re-gathering statistics is really just a workaround for getting optimizer plan stability without using the real plan stability functionality? I wonder what the trade-off is between the two approaches? What are the (dis)advantages of formal optimizer plan stability in comparison to maintaining stable statistics?

10:23 AM  
Blogger Unknown said...

15711meiqing
red christian louboutin
michael kors handbags
louis vuitton outlet
hollister jeans
hollister clothing store
chanel uk
gucci outlet online
cheap jerseys
cheap nfl jerseys
burberry handbags
coach outlet
coach outlet
jordan 13
coach outlet store online
timberland pro
christian louboutin outlet
cheap lululemon
louis vuitton
abercrombie & fitch new york
burberry outlet
jeremy scott adidas
michael kors outlet
cheap jerseys
louis vuitton handbags
hermes birkin
mulberry handbags
burberry outlet
cheap air max
cheap oakley sunglasses
jordan retro 3
toms outlet
michael kors handbags
tory burch outlet online
toms shoes
ralph lauren polo shirts
toms outlet
michael kors handbags
red timberland boots
tods outlet
coach outlet online

6:56 AM  
Blogger xjd7410@gmail.com said...

20150911 junda
replica watches cheap
michael kors handbags
ralph lauren
timberland uk
ugg boots for women
longchamp handbags
cheap jordans for sale
new balance outlet
nike blazer
ralph lauren homme
nike uk
michael kors handbags
ralph lauren
new balance shoes
adidas trainers
air max 95
cheap uggs
tory burch outlet online
oakley sunglasses outlet
cheap ray ban sunglasses
adidas gazelle trainers
toms shoes outlet
nike trainers
rolex watches
burberry sale
fitflop shoes
soccer jerseys wholesale
michael kors outlet
michael kors uk
ralph lauren
nike uk
air max 90
nike air max
mizuno shoes
kate spade bags
chaussure louboutin
coach outlet store online
coach factory outlet
tory burch shoes
hollister

6:41 PM  
Blogger 柯云 said...

2016-01-08keyun
louis vuitton purses
cheap toms shoes
ray bans
michael kors outlet
louis vuitton handbags
michael kors outlet clearance
oakley sunglasses
michael kors outlet
cheap oakley sunglasses
cheap toms
hollister clothing
north face
ray ban sunglasses outlet
coach outlet
cheap ugg boots
michael kors outlet sale
north face jackets
swarovski crystal
hollister kids
louis vuitton handbags
louis vuitton purses
ugg boots
uggs outlet
ugg boots sale
ralph lauren outlet
oakley sunglasses cheap
air force 1 trainers
gucci handbags
uggs for women
fitflop clearance
ugg outlet store
cheap toms
louis vuitton handbags
coach outlet store
discount ugg boots
ugg australia
air max 95
chanel outlet
chanel purses
nfl jerseys

11:55 PM  
Blogger oakleyses said...

oakley sunglasses, michael kors outlet, louboutin outlet, prada handbags, air max, burberry outlet, louis vuitton outlet, ray ban sunglasses, tory burch outlet, cheap uggs, michael kors outlet, michael kors outlet, chanel handbags, burberry outlet, oakley sunglasses, kate spade outlet, uggs outlet, oakley sunglasses cheap, prada outlet, longchamp handbags, gucci outlet, air max, louis vuitton, ray ban sunglasses, nike shoes, jordan shoes, nike free, polo ralph lauren, michael kors outlet, polo ralph lauren outlet, ray ban sunglasses, oakley sunglasses, michael kors outlet, christian louboutin, tiffany and co, louboutin, tiffany and co, rolex watches, longchamp outlet, louis vuitton outlet, longchamp handbags, ugg boots clearance, michael kors outlet online sale, uggs, ugg outlet, louis vuitton outlet stores, louboutin shoes, replica watches, louis vuitton handbags

4:35 PM  
Blogger oakleyses said...

vanessa bruno, air max, michael kors uk, coach outlet, michael kors, sac guess, true religion outlet, nike free pas cher, nike free, kate spade handbags, timberland, lululemon outlet online, michael kors outlet, north face, abercrombie and fitch, coach purses, ralph lauren, polo lacoste, oakley pas cher, north face, new balance pas cher, sac hermes, coach outlet store online, converse pas cher, longchamp, nike tn, air max pas cher, hollister, sac burberry, air max, michael kors pas cher, nike air force, hogan outlet, ray ban pas cher, louboutin, polo ralph lauren, true religion jeans, mulberry, sac longchamp pas cher, replica handbags, true religion outlet, nike blazer, vans pas cher, ray ban sunglasses, nike roshe, true religion jeans, nike roshe run pas cher, hollister, nike air max, air jordan

4:38 PM  
Blogger oakleyses said...

converse, pandora charms, toms shoes, barbour, moncler outlet, sac louis vuitton, swarovski, links of london, louis vuitton, barbour, moncler, canada goose, moncler, canada goose jackets, pandora charms, converse shoes, rolex watches, vans, supra shoes, moncler, ugg boots, abercrombie, ugg, marc jacobs, ugg pas cher, ugg, wedding dresses, pandora jewelry, sac lancel, air max, louis vuitton, sac louis vuitton, coach outlet store online, juicy couture, hollister, ugg, canada goose, canada goose, moncler, juicy couture outlet, thomas sabo, moncler, ray ban, louis vuitton uk, montre homme, moncler, gucci, canada goose outlet, pandora jewelry, canada goose, canada goose uk

4:46 PM  
Blogger te12 said...

qzz0727
pandora jewelry outlet
hermes belt
canada goose outlet
jordan shoes
ugg boots
pandora
christian louboutin shoes
fitflops sale clearance
valentino shoes
pandora

2:05 AM  
Blogger Unknown said...

Thank you very much for sharing this very useful information
I was very happy because with this information, I was able to find something very interesting

Obat Gabagen Alami Cara Mengatasi Perut Kembung Obat Uci-Uci Cara Menyembuhkan Kerumut Obat Luka Diabetes Cara Mengobati Benjolan Di Vagina Cara Mengatasi Campak Pada Anak Cara Mengatasi Vagina yang Gatal Cara Menghilangkan Bisul Cara Menurunkan Trigliserida

5:47 AM  
Anonymous Khasiat De Lemon said...

This article will hopefully help and benefit us all

7:35 PM  
Blogger yanmaneee said...

curry 4
air max 97
retro jordans
moncler jackets
yeezy boost 350
fila shoes
golden goose sneakers
nike 97
michael kors handbags
chrome hearts

9:39 PM  
Anonymous Anonymous said...

Golden Goose
golden goose
hermes bags
kd shoes
supreme clothing
golden goose
nike travis scott
jordans shoes

3:04 AM  

Post a Comment

<< Home