+ All Categories
Home > Technology > PHP performance 101: so you need to use a database

PHP performance 101: so you need to use a database

Date post: 08-Jul-2015
Category:
Upload: leon-fayer
View: 407 times
Download: 0 times
Share this document with a friend
Description:
Being involved in performance audits on systems of every size, from start-up sites hacked together overnight, to a ginormous applications built by world-recognized brand companies, I’ve seen a lot of interesting (and sometimes very unique) performance issues in every level of the stack: code, architecture, databases (sometimes all of the above). But there are a few particular, very “Performance 101″, issues that (unfortunately) appear in a lot of code bases. In this talk I present the most common database-related performance bottlenecks that can happen in most PHP applications.
41
PHP Performance 101: so you need to use a database Leon Fayer @papa_fire
Transcript
Page 1: PHP performance 101: so you need to use a database

PHP Performance 101:so you need to use a database

Leon Fayer@papa_fire

Page 2: PHP performance 101: so you need to use a database

Who am I ?

• 20+ years development and operations of web applications

• currently Vice President at OmniTI

• can be found online:

• @papa_fire

• github:lfayer

• http://fayerplay.com

• https://joind.in/talk/view/11914

Page 3: PHP performance 101: so you need to use a database

what it is about

databases & performance

Page 4: PHP performance 101: so you need to use a database

what it’s not about

NoSQL MySQL

Page 5: PHP performance 101: so you need to use a database

how database connection works

① establish connection

② send query

③ process query

④ send result

⑤ close connection

Page 6: PHP performance 101: so you need to use a database

common database connection

$dbh = new DB(…);

$sth = $dbh->prepare($query);

$sth->execute();

$result = $sth->fetchAll();

$dbh = null;

Page 7: PHP performance 101: so you need to use a database

common database connection

① var $dbh = new DB(…);

② var $sth = $dbh->prepare($query);

① $sth->execute();

② var $result = $sth->fetchAll();

③ $dbh = null;

Page 8: PHP performance 101: so you need to use a database

① establish connection

Page 9: PHP performance 101: so you need to use a database

① establish connection

⑤ close connection

and

Page 10: PHP performance 101: so you need to use a database

problem

connection overhead

Page 11: PHP performance 101: so you need to use a database

visual representation

Page 12: PHP performance 101: so you need to use a database

short answer

persistent connections

Page 13: PHP performance 101: so you need to use a database

short and helpful answer

persistent connections

avoid multiple connections

Page 14: PHP performance 101: so you need to use a database

how it works (high level)

Page 15: PHP performance 101: so you need to use a database

conclusion

reduce # of connections

Page 16: PHP performance 101: so you need to use a database

② send query

Page 17: PHP performance 101: so you need to use a database

most common problem

n+1

Page 18: PHP performance 101: so you need to use a database

n+1

// get a list of items

$sth = $dbh->prepare("select item_id from items

where active = true");

$sth->execute();

$items = $sth->fetchAll();

// get properties for each items

foreach ($items as $i) {

$sth_prop = $dbh->prepare("select * from

item_properties where item_id = ?");

$sth_prop->execute($item);

$item_list[$i[‘item_id’]][‘props’] = $sth_prop->fetchAll();

$item_list[$i[‘item_id’]][‘id’] = $i[‘id’];

}

Page 19: PHP performance 101: so you need to use a database

n+1 you don’t know about

// get a list of items

$items = get_active_item_ids();

// get properties for each items

foreach ($items as $i) {

$item = Item->new($i[‘item_id’])

$item_list[$i[‘item_id’][‘id’] = $item->item_id;

$item_list[$i[‘item_id’]][‘props’] = $item->properties();

}

Page 20: PHP performance 101: so you need to use a database

easy solution

// get a list of items with properties

$sth = $dbh->prepare("select i. item_id, p.* from items i,

item_properties p

where i.item_id = p.item_id

and active = true");

$sth->execute();

$items = $sth->fetchAll();

// arrange object to your liking

foreach ($items as $i) {

$item_list[$i[‘item_id’]][‘props’] = $i;

$item_list[$i[‘item_id’]][‘id’] = $i[‘id’];

}

Page 21: PHP performance 101: so you need to use a database

conclusion

limit number of queries

Page 22: PHP performance 101: so you need to use a database

cool stuff

:BONUS:Common Table Expressions

(CTEs)

* MySQL does not support CTEs

Page 23: PHP performance 101: so you need to use a database

// create temp table naughty_users

// and get data from it

with naughty_users as (

select * from users where banned = 1

)

select userid, email from naughty_users;

Page 24: PHP performance 101: so you need to use a database

even more cool

Writeable

Common Table Expressions

* Postgres 9.1+ only

Page 25: PHP performance 101: so you need to use a database

multiple queries are required

// create user record

insert into users (name, email) values (?,?) returning

userid

// create address record

insert into addresses (userid, address, city, state,

zip) values (?,?,?,?,?) returning addressid

// track changes to user information

insert into user_history (userid, addressid, action)

values (?,?,?) returning historyid

Page 26: PHP performance 101: so you need to use a database

or are they?

with userdata as (

insert into users (name, email) values (?,?)

returning userid

), addressdata as (

insert into addresses (userid, address, city, state, zip)

select userid,?,?,?,? from userdata

returning addressid

), historydata as (

insert into user_history (userid, addressid, action)

select userid, addressid,?

from userdata, addressdata

returning historyid

)

select userid, addressid, historyid

from userdata, addressdata, historydata;

Page 27: PHP performance 101: so you need to use a database

why not use transactions?

• no complicated transaction code

• no complicated error handling code

• reduced query overhead

• better performance

Page 28: PHP performance 101: so you need to use a database

find out more

For more details:

http://omniti.com/seeds/writable-ctes-improve-performance

Page 29: PHP performance 101: so you need to use a database

③ process query

Page 30: PHP performance 101: so you need to use a database

unpopular opinion

ORMs are evil

Page 31: PHP performance 101: so you need to use a database

why?

1. machine-generated

2. object construction overhead

3. false sense of control

Page 32: PHP performance 101: so you need to use a database

in one sentence

you have no idea how it works

Page 33: PHP performance 101: so you need to use a database

timely tweet

Page 34: PHP performance 101: so you need to use a database

conclusion

learn SQL

Page 35: PHP performance 101: so you need to use a database

④ send results

Page 36: PHP performance 101: so you need to use a database

may be shocking, but …

databases can do math

Page 37: PHP performance 101: so you need to use a database

illustrating wrong// get all orders

$sth = $dbh->prepare("select order_id, price from orders");

$sth->execute();

$orders= $sth->fetchAll();

//order by order_id

usort($orders, function ($a, $b) { if ($a['order_id'] == $b['order_id']) { return 0; }

return $a['order_id'] < $b['order_id'] ? -1 : 1; });

// get average $ for last 10 orders

$count = 1;

$total = 0;

$avg = 0;

foreach ($orders as $order) {

$total += $order[‘price’];

if ($count == 10) {

$avg = $total/$count;

break 1;

}

$count++;

}

Page 38: PHP performance 101: so you need to use a database

vs right

// get average $ for last 10 orders

$sth = $dbh->prepare("select avg(price) as avg_price

from (select price from orders

order by order_id desc limit 10) ");

$sth->execute();

$orders= $sth->fetchAll();

$avg = $orders[‘avg_price’];

Page 39: PHP performance 101: so you need to use a database

conclusion

learn SQL

Page 40: PHP performance 101: so you need to use a database

other things to consider

1. cache is a wonderful thing

2. * is not your friend

3. EXPLAIN/ANALYZE are

Page 41: PHP performance 101: so you need to use a database

Questions?


Recommended