My Database Skills Killed the Server
Embed Size (px)
Transcript of My Database Skills Killed the Server
My sql skills killed the server
My Database skills killed the server
Dave Ferguson@dfgrumpyCFSummit 2015
Who am I?I am an Adobe Community Professional
I started building web applications a long time ago
Contributor to Learn CF in a week
I have a ColdFusion podcast called CFHour w/ Scott Stroz (@boyzoid)(please listen)
3x District Champion in Taekwondo
What will we cover?Running QueriesWhen good SQL goes badBulk processingLarge volume datasetsIndexesOutside influences
(I KNOW SQL)WHY AM I HERE?
Because you have probably written something like this
select * from myTable
I can write SQL in my sleepselect * from myTable where id = 2
I can write joins and other complex SQLSelect mt.* from myTable mtjoin myOtherTable moton mt.id = mot.idwhere mot.id = 2
I might even create a table CREATE TABLE `myFakeTable` ( `id` int(11) unsigned NOT NULL AUTO_INCREMENT, `someName` varchar(150) NOT NULL DEFAULT '', `someDescription` text, `type` varchar(50) DEFAULT NULL, `status` int(11) NOT NULL, PRIMARY KEY (`id`));
But, how do you know if what you did was the best / most efficient way to do it?
Did the internet tell you it was right?
Did you get some advice from a someone?
My app works fine. It has thousands of queries and we only see slowness every once in a while.
Have you ever truly looked at what your queries are doing?
Most developers don't bother.
They leave all that technicaldatabase stuff up to the DBA.
But what if you are the developer AND the DBA?
Query PlanUses Execution ContextsCreated for each degree of parallelism for a queryExecution ContextSpecific to the query being executed. Created for each query
Execution Context & Query Plan
Have you ever lookedat a query plan?
Do you know what a query plan is?
Query Plan, In the event you were curious
What a query plan will tell youPath taken to get dataAlmost like a Java stack traceIndexes usageHow the indexes are being usedCost of each section of planPossible suggestions for performance improvement Whole bunch of other stuff
How long are plans / contexts kept?
1 Hour1 DayTil SQL server restartsDiscards it immediatelyThe day after foreverTill the server runs out of cache space
What can cause plans to be flushed from cache?
Forced via codeMemory pressureAlter statementsStatistics updateauto_update statistics on
HOW CAN WE KEEP THE DATABASE FROM THROWING AWAY THE PLANS?
HOW CAN WE GET THE DATABASE TO USE THE CACHED PLANS?
Force itUse paramsUse stored proceduresGet more ramUse less queriesSimple answer
HOW DOES SQL DETERMINE IF THERE IS A QUERY PLAN?
THIS QUERY WILL CREATE A EXECUTION CONTEXT..select id, name from myTable where id = 2THAT
WILL NOT BE USED BY THIS QUERY.select id, name from myTable where id = 5
WHY IS THAT?
Well, the queries are not the same.
According to the SQL optimizer,
select id, name from myTable where id = 2select id, name from myTable where id = 5this query and this query are not the same.So, they each get their own execution context.
PLANS CAN BECOME DATA HOGSselect id, name from myTable where id = 2If the query above ran 5,000 times over the course of an hour (with different ids), you could have that many plans cached.That could equal around 120mb of cache space!
EXECUTION CONTEXTS ARE GOOD
TOO MANY ARE BAD
There is only so much cache room to go around34
USING QUERY PARAMS
The secret sauce to plan reuse
select a.ARTID, a.ARTNAME from ART awhere a.ARTID =