My Database Skills Killed the Server

71
MY DATABASE SKILLS KILLED THE SERVER Dave Ferguson @dfgrumpy CFSummit 2015

Transcript of My Database Skills Killed the Server

Page 1: My Database Skills Killed the Server

MY DATABASE SKILLS KILLED THE SERVER

Dave Ferguson@dfgrumpy

CFSummit 2015

Page 2: My Database Skills Killed the Server

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

Page 3: My Database Skills Killed the Server

WHAT WILL WE COVER?

• Running Queries• When good SQL goes bad• Bulk processing• Large volume datasets• Indexes• Outside influences

Page 4: My Database Skills Killed the Server
Page 5: My Database Skills Killed the Server

(I KNOW SQL)

“WHY AM I HERE?”

Page 6: My Database Skills Killed the Server

Because you have probably written something like this…

Page 7: My Database Skills Killed the Server

select * from myTable

Page 8: My Database Skills Killed the Server

“I can write SQL in my sleep”

select * from myTable where id = 2

Page 9: My Database Skills Killed the Server

“I can write joins and other complex SQL”

Select mt.* from myTable mtjoin myOtherTable moton mt.id = mot.idwhere mot.id = 2

Page 10: My Database Skills Killed the Server

“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`));

Page 11: My Database Skills Killed the Server

But, how do you know if what you did was the best / most

efficient way to do it?

Page 12: My Database Skills Killed the Server

Did the internet

tell you it was

right?

Page 13: My Database Skills Killed the Server

Did you get some

advice from a

someone?

Page 14: My Database Skills Killed the Server

“My app works fine. It has thousands of queries and we

only see slowness every once in a while. ”

Page 15: My Database Skills Killed the Server

Have you ever truly looked at what your queries are doing?

Page 16: My Database Skills Killed the Server

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?

Page 17: My Database Skills Killed the Server

Query Plan Uses Execution Contexts Created for each degree of parallelism for

a query

Execution Context Specific to the query being executed. Created for each query

QUERY EXECUTION

Page 18: My Database Skills Killed the Server

Execution Context & Query Plan

Page 19: My Database Skills Killed the Server

Have you ever lookedat a query plan?

Do you know what a query plan is?

Page 20: My Database Skills Killed the Server

Query Plan, In the event you were curious…

Page 21: My Database Skills Killed the Server

WHAT A QUERY PLAN WILL TELL YOU

• Path taken to get data• Almost like a Java stack trace

• Indexes usage• How the indexes are being used

• Cost of each section of plan• Possible suggestions for performance improvement • Whole bunch of other stuff

Page 22: My Database Skills Killed the Server

How long are plans / contexts kept?

1 Hour 1 Day ‘Til SQL server restarts Discards it immediately The day after forever Till the server runs out of cache space

Page 23: My Database Skills Killed the Server

What can cause plans to be flushed from cache?

Forced via code Memory pressure Alter statements Statistics update

auto_update statistics on

Page 24: My Database Skills Killed the Server

HOW CAN WE KEEP THE DATABASE FROM THROWING

AWAY THE PLANS?

Page 25: My Database Skills Killed the Server

MORE IMPORTANTLY,

HOW CAN WE GET THE DATABASE TO USE THE CACHED PLANS?

Page 26: My Database Skills Killed the Server

• Force it• Use params• Use stored procedures• Get more ram• Use less queries

SIMPLE ANSWER

Page 27: My Database Skills Killed the Server

HOW DOES SQL DETERMINE IF THERE

IS A QUERY PLAN?

Page 28: My Database Skills Killed the Server

SomethingLike this…

Page 29: My Database Skills Killed the Server

THIS QUERY WILL CREATE A EXECUTION CONTEXT..

select id, name from myTable where id = 2

THAT…

Page 30: My Database Skills Killed the Server

WILL NOT BE USED BY THIS QUERY.

select id, name from myTable where id = 5

Page 31: My Database Skills Killed the Server

WHY IS THAT?

Well, the queries are not the same.

Page 32: My Database Skills Killed the Server

According to the SQL optimizer,

select id, name from myTable where id = 2

select id, name from myTable where id = 5

this query…

and this query…

are not the same.

So, they each get their own execution context.

Page 33: My Database Skills Killed the Server

PLANS CAN BECOME DATA HOGS

select id, name from myTable where id = 2

If 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!

Page 34: My Database Skills Killed the Server

TO RECAP…

EXECUTION CONTEXTS ARE GOOD

TOO MANY ARE BAD

Page 35: My Database Skills Killed the Server

USING QUERY PARAMS…

The secret sauce to plan reuse

Page 36: My Database Skills Killed the Server

<cfquery name="testQuery">

select a.ARTID, a.ARTNAME from ART awhere a.ARTID = <cfqueryparam value="5”

cfsqltype="cf_sql_integer"></cfquery>

Using a simple query… let’s add a param for the id.

Page 37: My Database Skills Killed the Server

select a.ARTID, a.ARTNAME from ART awhere a.ARTID = ?

THE QUERY OPTIMIZER SEES THIS…

Page 38: My Database Skills Killed the Server

testQuery (Datasource=cfartgallery, Time=1ms, Records=1) in /xxx/x.cfm

select a.ARTID, a.ARTNAME from ART awhere a.ARTID = ?

Query Parameter Value(s) -Parameter #1(cf_sql_integer) = 5

THE DEBUG OUTPUT LOOKS LIKE THIS…

Page 39: My Database Skills Killed the Server

testQuery (Datasource=cfartgallery, Time=8ms, Records=5) in /xxx/x.cfm

select a.ARTID, a.ARTNAME from ART awhere a.ARTID in (?,?,?,?,?)

Query Parameter Value(s) -Parameter #1(CF_SQL_CHAR) = 1Parameter #2(CF_SQL_CHAR) = 2Parameter #3(CF_SQL_CHAR) = 3Parameter #4(CF_SQL_CHAR) = 4Parameter #5(CF_SQL_CHAR) = 5

IT EVEN WORKS ON LISTS…

Page 40: My Database Skills Killed the Server

testQuery (Datasource=cfartgallery, Time=3ms, Records=1) in /xxx/x.cfm

select a.ARTID, a.ARTNAME, (select count(*) from ORDERITEMS oi where oi.ARTID = ?) as ordercountfrom ART awhere a.ARTID in (?)

Query Parameter Value(s) -Parameter #1(cf_sql_integer) = 5Parameter #2(cf_sql_integer) = 5

MORE ACCURATELY, THEY WORK ANYWHERE YOU WOULD HAVE DYNAMIC INPUT...

Page 41: My Database Skills Killed the Server

When can plans cause more harm then help?

► When your data structure changes► When data volume grows quickly► When you have data with a high

degree of cardinality.

Page 42: My Database Skills Killed the Server

How do I deal with all this data?

Page 43: My Database Skills Killed the Server

What do I mean by large data sets?

► Tables over 1 million rows► Large databases► Heavily denormalized data

Page 44: My Database Skills Killed the Server

Ways to manage large data

► Only return what you need (no “select *”)► Try and page the data in some fashion► Optimize indexes to speed up where

clauses► Avoid using triggers on large volume

inserts / multi-row updates► Reduce any post query processing as

much as possible

Page 45: My Database Skills Killed the Server

Inserting / Updating large datasets

► Reduce calls to database by combining queries

► Use bulk loading features of your Database

► Use XML/JSON to load data into Database

Page 46: My Database Skills Killed the Server

Combining Queries: Instead of doing this…

Page 47: My Database Skills Killed the Server

Do this…

Page 48: My Database Skills Killed the Server

Gotcha’s in query combining

► Errors could cause whole batch to fail► Overflowing allowed query string size► Database locking can be problematic► Difficult to get any usable result from query

Page 49: My Database Skills Killed the Server

Upside query combining

► Reduces network calls to database► Processed as a single batch in database► Generally processed many times faster

than doing the insert one at a time

I have used this technique to insert over 50k rows into mysql in under one second.

Page 50: My Database Skills Killed the Server

Indexes

The secret artof a faster select

Page 51: My Database Skills Killed the Server

Index Types

► Unique► Primary key or row ID

► Covering► A collection of columns indexed in an order that

matches where clauses► Clustered

► The way the data is physically stored► Table can only have one

► NonClustered► Only contain indexed data with a pointer back to

source data

Page 52: My Database Skills Killed the Server

Seeking and Scanning

► Index SCAN (table scan)► Touches all rows► Useful only if the table contains small amount of rows

► Index SEEK► Only touches rows that qualify ► Useful for large datasets or highly selective queries

► Even with an index, the optimizer may still opt to perform a scan

Page 53: My Database Skills Killed the Server

To index or not to index…

► DO INDEX► Large datasets where 10 – 15% of the data is usually

returned► Columns used in where clauses with high cardinality

► User name column where values are unique

► DON’T INDEX► Small tables► Columns with low cardinality

► Any column with only a couple unique values

Page 54: My Database Skills Killed the Server

Do I really need an index?

Page 55: My Database Skills Killed the Server

It Depends.

Page 56: My Database Skills Killed the Server

Really… it Depends!

Page 57: My Database Skills Killed the Server

Outside influences

Page 58: My Database Skills Killed the Server

Other things that can effect performance

► Processor load

► Memory pressure

► Hard drive I/O

► Network

Page 59: My Database Skills Killed the Server

Processor

► Give SQL Server process CPU priority► Watch for other processes on the server using

excessive CPU cycles► Have enough cores to handle your database

activity► Try to keep average processor load below

50% so the system can handle spikes gracefully

Page 60: My Database Skills Killed the Server

Memory (RAM)

► Get a ton (RAM is cheap)

► Make sure you have enough RAM to keep your server from doing excess paging

► Make sure your DB is using the RAM in the server

► Allow the DB to use RAM for cache

► Watch for other processes using excessive RAM

Page 61: My Database Skills Killed the Server

Drive I/O

► Drive I/O is usually the largest bottle neck on the server► Drives can only perform one operation at a time

► Make sure you don’t run out of space► Purge log files

► Don’t store all DB and log files on the same physical drives► On windows don’t put your DB on the C: drive

► If possible, use SSD drives for tempdb or other highly transactional DBs

► Log drives should be in write priority mode► Data drives should be in read priority mode

Page 62: My Database Skills Killed the Server

Network

► Only matters if App server and DB server are on separate machines (they should be)

► Minimize network hops between servers► Watch for network traffic spikes that slow data retrieval► Only retrieving data needed will speed up retrieval from DB server to

app server► Split network traffic on SQL server across multiple NIC cards so that

general network traffic doesn’t impact DB traffic

Page 63: My Database Skills Killed the Server

Some Important Database Statistics

Page 64: My Database Skills Killed the Server

Important stats

► Recompiles► Recompile of a proc while running shouldn’t occur► Caused by code in proc or memory issues

► Latch Waits► Low level lock inside DB; Should be sub 10ms

► Lock Waits► Data lock wait caused by thread waiting for

another lock to clear► Full Scans

► Select queries not using indexes

Page 65: My Database Skills Killed the Server

Important stats continued..

► Cache Hit Ratio► How often DB is hitting memory cache vs Disk

► Disk Read / Write times► Access time or write times to drives

► SQL Processor time► SQL server processor load

► SQL Memory► Amount of system memory being used by SQL

Page 66: My Database Skills Killed the Server

Where SQL goes wrong

(Good examples of bad SQL)

Page 67: My Database Skills Killed the Server

Inline queries that shouldn’t be

Page 68: My Database Skills Killed the Server

Over joining data

Page 69: My Database Skills Killed the Server

Transactions – Do you see the issue?

Page 70: My Database Skills Killed the Server
Page 71: My Database Skills Killed the Server

THANK YOUDave Ferguson

@[email protected]

mwww.cfhour.comCFSummit 2015

Don’t forget to fill out the survey