SlideShare a Scribd company logo
1 of 25
Download to read offline
www.nyphp.com / www.nyphp.org




    MySQL and PHP – State of the Union
    Connectors, Best Practices, Performance, and the “Cloud”




New York City MySQL Meetup      Web Performance for PHP Developers   Scaling & Optimizing MySQL
December 9th, 2008
                                 Web Performance Meetup/Etsy.com              Sun Microsystems
CommunityOne East                         June 16th, 2009                   December 16th, 2008
Sun Microsystems
March 18th, 2009
                                Hans Zaunere, Managing Member


     6/18/2009                           © 2008 New York PHP, LLC                       1
www.nyphp.com / www.nyphp.org




                                Overview
    • Introduction

    • Connectors

    • Best Practices and Techniques

    • Performance and Scaling

    • Scaling and Performance

    • Clouds and Smog

    • Questions/Resources

    6/18/2009                   © 2008 New York PHP, LLC   2
www.nyphp.com / www.nyphp.org




  Introduction
 AMP Needs No Introduction
                                                           LAMP
  • Apache/MySQL/PHP
  • Other variations on the theme                          BAMP
       – But the fundamentals remain
                                                           OSAMP

                                                           SAMP
     PHP glues together high-
    speed database with high-                              WAMP
      speed external libraries
                                                           XAMP

                                                           DAMP


    6/18/2009                   © 2008 New York PHP, LLC          3
www.nyphp.com / www.nyphp.org




                                    Connectors…
                                      I’m a


    • mysql – The Classic

    • mysqli – Improving a Classic

    • PDO – Abstracted Abstraction

    • mysqlnd – A PHP Native


                                Pick the Right Connector
    6/18/2009                          © 2008 New York PHP, LLC   4
www.nyphp.com / www.nyphp.org




                                 Connectors…
                            mysql Extension - The Classic

    •    http://www.php.net/mysql
    •    Still one of the fastest and quickest to implement
    •    Results are always strings
    •    Very mature and proven code
    •    Strictly functional
    •    No binary protocol support
    •    Always be real
           – mysql_real_escape_string()            EVERYTHING




    6/18/2009                        © 2008 New York PHP, LLC   5
www.nyphp.com / www.nyphp.org




                                Connectors…
                     mysqli Extension – Improving a Classic

    •    http://www.php.net/mysqli
    •    Recommended for all new development
    •    Supports new and old MySQL features
    •    Prepared statements and binary protocol available
           – Result sets are returned as PHP native types
           – Some advantages/disadvantages
    • Choice between functional or object-based code
    • Just got persistent connections
           – Now “idiot proof” and yields almost 7x connections/second gain




    6/18/2009                      © 2008 New York PHP, LLC                   6
www.nyphp.com / www.nyphp.org




                                     Connectors…
                                PDO – Abstracted Abstraction
    • http://www.php.net/pdo
      http://www.php.net/pdo-mysql
      http://dev.mysql.com/tech-resources/articles/mysql-pdo.html
    • PHP Data Object
    • Provides consistent OO interface to multiple database
           – May deviously hide differences between databases
             Know thy configuration and available database features
    • Segmentation fault issues early on – still gun shy
    • Still being developed, including support for mysqlnd
    • Recommended only if abstraction is an immediate
      requirement

    6/18/2009                           © 2008 New York PHP, LLC      7
www.nyphp.com / www.nyphp.org




                                 Connectors…
                           mysqlnd Library – A PHP Native
    • http://blog.ulf-wendel.de/ (current)
      http://blog.felho.hu/what-is-new-in-php-53-part-3-mysqlnd.html
    • Optional backend library for mysql/mysqli/PDO
    • Takes advantage of PHP/Zend Engine internal structure
    • Supports async queries
          • Deprecates --enable-mysqlnd-threading
    • Long time in coming but still apparently developed
    • Available in PHP 5.3 (alpha)
    • Not recommended for prime time quite yet



    6/18/2009                       © 2008 New York PHP, LLC           8
www.nyphp.com / www.nyphp.org




                                Connectors
                                …Survey Says




    • Stick with - or upgrade to - mysqli




    6/18/2009                    © 2008 New York PHP, LLC   9
www.nyphp.com / www.nyphp.org




                                Best Practices…
    • Modern applications should use mysqli
    • Know your hardware architecture
           –    Use x64 where possible for RAM
           –    Avoid mixed 32/x64 environments/libs
           –    PHP needs --with-libdir=lib64
           –    In production environments strive to have a pure lib64 system
           –    Install and link against the right libraries for your architecture
    • Know your compile options
           – Don’t let ./configure guess
           – --with-mysqli=/usr/bin/mysql_config - specify file!
           – --with-mysqli=mysqlnd (alpha)
    • Know your client/server versions
           – Make sure they match as closely as possible
    6/18/2009                          © 2008 New York PHP, LLC                      10
www.nyphp.com / www.nyphp.org




                                …and Techniques…
    • mysqli objects or functions? Your choice but…
           – Objects may provide a more flexible, elegant and fool-proof
             implementation


    • Be explicit
           – Specify the MySQL resource or result in every function
           – mysqli’s OO implementation enforces better practices

          $MYDB = mysql_connect('www.nyphp.com','root','=ro0t--');

          // BAD
          $R = mysql_query('SELECT * FROM BB.Account');

          // GOOD
          $R = mysql_query('SELECT * FROM BB.Account',$MYDB);

    6/18/2009                       © 2008 New York PHP, LLC               11
www.nyphp.com / www.nyphp.org




                                …and Techniques
    • Be prepared with prepared statements?
           – Consider what the actual benefits are for your application
           – Not all applications may benefit



    • What’s wrong with this picture?
            $R = mysql_query('SELECT * FROM BB.Account',$MYDB);

            $R2 = array();

            while( ($R2[] = mysql_fetch_assoc($R)) !== FALSE );




    6/18/2009                      © 2008 New York PHP, LLC               12
www.nyphp.com / www.nyphp.org




                       Performance and Scaling…
                           www.mysqlperformanceblog.com
    • Consider use case
           – Need to support varied queries in a framework environment?
           – … or highly repetitive queries in a batch environment?


    • Consider highest-cost resources for your application
           – RAM? CPU? Database round-trips?


    • Consider your data’s flavor
           – Big blobs? Small strings and ints?
           – Lots of rows? Small result sets?
           – Complexity and relationships?


    6/18/2009                      © 2008 New York PHP, LLC               13
www.nyphp.com / www.nyphp.org




                       Performance and Scaling…
    • Consider memory usage vs CPU
           – Storing results as an array of arrays (non-columnar)
                – mysqli_result_fetch_all(), mysqli_result_fetch_row(), etc.
           – Storing results as an columnar array
                – mysql_stmt_fetch_column() available in C but not in ext/mysqli (NEEDED)
            // 1663 rows
            $R = mysql_query('SELECT * FROM BB.Account',$MYDB);
            $R2 = array();

            // Non-columnar    0.034s         3.99mb
            while( ($T = mysql_fetch_assoc($R)) !== FALSE )
                $R2[] = $T;

            // Columnar        0.048s 41.18%  3.37mb -15.54%
            while( ($T = mysql_fetch_assoc($R)) !== FALSE )
            {
                foreach( $T as $Col => $Val )
                        $R2[$Col][] = $Val;
            }
    6/18/2009                            © 2008 New York PHP, LLC                       14
www.nyphp.com / www.nyphp.org




                       Performance and Scaling…
                      Connectors - Connectors - Connectors
    • Simple benchmarking of the current Connector scene
           • 500 executions of a simple SELECT
           • Retrieving 1663 rows each time
            mysql> SELECT * FROM BB.Account

            `AccountGID`        bigint(20)
            `R_UserGID`         bigint(20)
            `InsertedTS`        timestamp
            `UpdatedTS`         timestamp
            `LastLoginTS`       timestamp
            `Status`            varchar(31)             collate utf8_unicode_ci
            `Type`              varchar(31)             collate utf8_unicode_ci
            `Admin`             tinyint(3)
            `Bucks`             decimal(15,2)
            `BID`               varchar(11)             collate utf8_unicode_ci
            `CurrentAlias`      varchar(63)             collate utf8_unicode_ci
            `Description`       varchar(1023)           collate utf8_unicode_ci


    6/18/2009                      © 2008 New York PHP, LLC                       15
www.nyphp.com / www.nyphp.org




                         Performance and Scaling…
                                 Text Queries (Text Protocol)

                             store_result    use_result
                                                                                   Notes
                              (buffered)    (unbuffered)

                                                               mysql_query()
                ext/mysql       18.2s           10.0s
                                                               mysql_unbuffered_query()

                ext/mysqli      19.2s           10.0s          mysqli_result::free() required for unbuffered

                                                               mysqli_result::fetch_all()
    ext/mysqli::mysqlnd         20.7s           10.0s                     not better than for() iteration
                                                               mysqli_result::free() required for unbuffered



    • All values available as PHP strings, regardless of column’s type
       • NULLs generally remain as NULLs in PHP
    • Always *_free_result() as a best practice
       • Required for unbuffered queries
    6/18/2009                               © 2008 New York PHP, LLC                                       16
www.nyphp.com / www.nyphp.org




                        Performance and Scaling…
                        Prepared Statements (Binary Protocol)
                             store_result    use_result
                                                                                   Notes
                              (buffered)    (unbuffered)

                ext/mysqli   12.5s/12.3s      8.4/8.2s         prepare on execute / prepare once

     ext/mysqli::mysqlnd     14.6s/14.2s     8.4s/8.2s         mysqli_result::free() required for unbuffered


                ext/mysqli      26.6s           21.0s
                                                               additional PHP coding to mimic
                                                               mysqli_result::fetch_assoc() behavior
     ext/mysqli::mysqlnd
                                29.2s           22.4s


      •    Each prepare requires an additional database roundtrip
            • http://forge.mysql.com/worklog/task.php?id=3359
      •    All values available as PHP types corresponding to column type
      •    *_free_result() and *_stmt_close() not always needed
            • But recommended as best practice
    6/18/2009                               © 2008 New York PHP, LLC                                           17
www.nyphp.com / www.nyphp.org




                       Performance and Scaling…
                                Observations and Upshots
    • No significant difference between localhost vs remote
           • …as it relates to buffered vs. unbuffered
    • mysqlnd has actually appeared slower overall
           • Slowdown in handling of PHP variables – WHAT?


    • Framework operations
           • Handle a variety of queries
           • Typically pull rows for later processing
           • Unbuffered (use_result) is faster but..
              • Avoid lengthy per-row processing retrieval as the tables are
                 locked until all rows are fetched (mostly a MyISAM problem)
              • Use buffered (store_result) in this case
    6/18/2009                         © 2008 New York PHP, LLC             18
www.nyphp.com / www.nyphp.org




                       Performance and Scaling…
                                Observations and Upshots
    • Specific/batch operations
           •    Prepared statements are better
           •    Large data, highly repetitive
           •    Not well implemented for general query backend
           •    Complex logic required to mimic array/object row fetching –
                SHAME


    • Generally - or - all things considered…
           • No huge difference between prepared and text queries
           • Prepared problematic depending on data structures required

                 YMIGTV – Your Mileage Is Guaranteed To Vary
    6/18/2009                         © 2008 New York PHP, LLC                19
www.nyphp.com / www.nyphp.org




                       Performance and Scaling…
                   Optimize Optimize Optimize
    • Buffers and configuration
       • Tune buffers for storage engines and operations
    • Queries
       • Indexes and correct usage absolutely critical
       • Avoid automatic query generation
       • WRITE QUERIES CAREFULLY
    • Hardware
       • RAM is fast but disks can be too… pick your battles
       • Augment CPU for crunch-intensive applications

                MySQL Responds Very Well To The Right TLC
    6/18/2009                   © 2008 New York PHP, LLC       20
www.nyphp.com / www.nyphp.org




                       Performance and Scaling…
                           I’ll Say It Again
    • Buffers and configuration
       • Architect the storage engines/schema from the outset
       • Tune the right buffers (key_buffer_size, innodb_*,
         heap, etc)
    • Queries
       • Try to use numeric keys
       • Double check EXPLAIN – left/to/right/prefixed
       • Partitioning and disk layout
       • Be careful with sub-queries and temp. tables
                       Convenience Kills Performance
                       MySQL Will Kill You Without TLC
    6/18/2009                     © 2008 New York PHP, LLC   21
www.nyphp.com / www.nyphp.org




                       …Scaling and Performance
                                Scaling Performance…?
    • Define scaling for your application and requirements
           • Frequent or Complex functional changes vs. moderate traffic
              • Functional Scaling
           • Stagnant or Simple functional changes vs. huge traffic
              • Traffic Scaling


    • Sharding i.e. Application Managed Partitioning
           • Functional vs. Key vs. Combination
           • Be ready for complexity – Bring in expertise


    • Add in memcached for shard recombination caching

    6/18/2009                        © 2008 New York PHP, LLC              22
www.nyphp.com / www.nyphp.org




                          Scaling and Performance
                                Scaling Performance…?
    • Replication – Tried and True
           • Able to handle very heavy load if done correctly
           • Comfortable with both Functional and Traffic scaling
           • Master-Master is an option if application is aware
    • MySQL Cluster
           • Can provide some of the best performance in the industry…
           • … but only in specific cases
           • Pairs well with Sharding as a replacement for memcached
    • Keep tabs on your data’s path, lifecycle and type
           • Know where it’s come from, what it’s doing, and where it’s going

                            Know Thy Data – Love Thy Data
    6/18/2009                        © 2008 New York PHP, LLC               23
www.nyphp.com / www.nyphp.org




                                 Clouds and Smog
                                Wait, what does that mean?
    • Keep Your Feet On the Ground – And Your Head Out of the Cloud
       • Clouds mean a lot of different things right now
    • Don’t put everything on one server – duh
    • There’s no silver bullet – Don’t try to cheat
       • Give it time, progress is being made
    • No cloud will simply scale a database – why…?
       • Cloud isn’t parallel processing (yet)
    • Varying types of Clouds
       • Application/API - Azure, Google Apps
       • Virtualization - Sun, EC2/Xen, VZ, VMWare
       • Start-up marketing fluff – BE WARY
                It’s All About Architecture and Optimization
                                  ALWAYS
    6/18/2009                          © 2008 New York PHP, LLC       24
www.nyphp.com / www.nyphp.org




                                     Thank You
                                hans.zaunere@nyphp.com



  For renowned online support, New York PHP Mailing Lists
  are free and available to anyone:

                            http://www.nyphp.org/mailinglists.php


  MySQL SIG:
                        http://lists.nyphp.org/mailman/listinfo/mysql



    6/18/2009                          © 2008 New York PHP, LLC         25

More Related Content

More from SergeyChernyshev

Tools of the trade 2014
Tools of the trade 2014Tools of the trade 2014
Tools of the trade 2014SergeyChernyshev
 
What we can learn from CDNs about Web Development, Deployment, and Performance
What we can learn from CDNs about Web Development, Deployment, and PerformanceWhat we can learn from CDNs about Web Development, Deployment, and Performance
What we can learn from CDNs about Web Development, Deployment, and PerformanceSergeyChernyshev
 
Web performance: beyond load testing
Web performance: beyond load testingWeb performance: beyond load testing
Web performance: beyond load testingSergeyChernyshev
 
Introduction to web performance @ IEEE
Introduction to web performance @ IEEEIntroduction to web performance @ IEEE
Introduction to web performance @ IEEESergeyChernyshev
 
Introduction to Web Performance
Introduction to Web PerformanceIntroduction to Web Performance
Introduction to Web PerformanceSergeyChernyshev
 
Performance anti patterns in ajax applications
Performance anti patterns in ajax applicationsPerformance anti patterns in ajax applications
Performance anti patterns in ajax applicationsSergeyChernyshev
 
Taming 3rd party content
Taming 3rd party contentTaming 3rd party content
Taming 3rd party contentSergeyChernyshev
 
Velocity 2010 review
Velocity 2010 reviewVelocity 2010 review
Velocity 2010 reviewSergeyChernyshev
 
Keeping track of your performance using Show Slow
Keeping track of your performance using Show SlowKeeping track of your performance using Show Slow
Keeping track of your performance using Show SlowSergeyChernyshev
 
Keeping track of your performance using show slow
Keeping track of your performance using show slowKeeping track of your performance using show slow
Keeping track of your performance using show slowSergeyChernyshev
 
Building your own CDN using Amazon EC2
Building your own CDN using Amazon EC2Building your own CDN using Amazon EC2
Building your own CDN using Amazon EC2SergeyChernyshev
 
Php Code Profiling Using X Debug
Php Code Profiling Using X DebugPhp Code Profiling Using X Debug
Php Code Profiling Using X DebugSergeyChernyshev
 
Introduction to RDFa
Introduction to RDFaIntroduction to RDFa
Introduction to RDFaSergeyChernyshev
 
Open Source Hardware: A Start...
Open Source Hardware: A Start...Open Source Hardware: A Start...
Open Source Hardware: A Start...SergeyChernyshev
 
Open Source Hardware (adolescence)
Open Source Hardware (adolescence)Open Source Hardware (adolescence)
Open Source Hardware (adolescence)SergeyChernyshev
 
Semantic Media Wiki & Semantic Forms
Semantic Media Wiki & Semantic FormsSemantic Media Wiki & Semantic Forms
Semantic Media Wiki & Semantic FormsSergeyChernyshev
 
Behind the Scenes at LiveJournal: Scaling Storytime
Behind the Scenes at LiveJournal: Scaling StorytimeBehind the Scenes at LiveJournal: Scaling Storytime
Behind the Scenes at LiveJournal: Scaling StorytimeSergeyChernyshev
 

More from SergeyChernyshev (19)

Tools of the trade 2014
Tools of the trade 2014Tools of the trade 2014
Tools of the trade 2014
 
What we can learn from CDNs about Web Development, Deployment, and Performance
What we can learn from CDNs about Web Development, Deployment, and PerformanceWhat we can learn from CDNs about Web Development, Deployment, and Performance
What we can learn from CDNs about Web Development, Deployment, and Performance
 
Web performance: beyond load testing
Web performance: beyond load testingWeb performance: beyond load testing
Web performance: beyond load testing
 
Introduction to web performance @ IEEE
Introduction to web performance @ IEEEIntroduction to web performance @ IEEE
Introduction to web performance @ IEEE
 
Introduction to Web Performance
Introduction to Web PerformanceIntroduction to Web Performance
Introduction to Web Performance
 
Performance anti patterns in ajax applications
Performance anti patterns in ajax applicationsPerformance anti patterns in ajax applications
Performance anti patterns in ajax applications
 
Taming 3rd party content
Taming 3rd party contentTaming 3rd party content
Taming 3rd party content
 
Velocity 2010 review
Velocity 2010 reviewVelocity 2010 review
Velocity 2010 review
 
Keeping track of your performance using Show Slow
Keeping track of your performance using Show SlowKeeping track of your performance using Show Slow
Keeping track of your performance using Show Slow
 
Keeping track of your performance using show slow
Keeping track of your performance using show slowKeeping track of your performance using show slow
Keeping track of your performance using show slow
 
In Search of Speed
In Search of SpeedIn Search of Speed
In Search of Speed
 
Building your own CDN using Amazon EC2
Building your own CDN using Amazon EC2Building your own CDN using Amazon EC2
Building your own CDN using Amazon EC2
 
Php Code Profiling Using X Debug
Php Code Profiling Using X DebugPhp Code Profiling Using X Debug
Php Code Profiling Using X Debug
 
Why RDFa?
Why RDFa?Why RDFa?
Why RDFa?
 
Introduction to RDFa
Introduction to RDFaIntroduction to RDFa
Introduction to RDFa
 
Open Source Hardware: A Start...
Open Source Hardware: A Start...Open Source Hardware: A Start...
Open Source Hardware: A Start...
 
Open Source Hardware (adolescence)
Open Source Hardware (adolescence)Open Source Hardware (adolescence)
Open Source Hardware (adolescence)
 
Semantic Media Wiki & Semantic Forms
Semantic Media Wiki & Semantic FormsSemantic Media Wiki & Semantic Forms
Semantic Media Wiki & Semantic Forms
 
Behind the Scenes at LiveJournal: Scaling Storytime
Behind the Scenes at LiveJournal: Scaling StorytimeBehind the Scenes at LiveJournal: Scaling Storytime
Behind the Scenes at LiveJournal: Scaling Storytime
 

Recently uploaded

How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersThousandEyes
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxKatpro Technologies
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Paola De la Torre
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxOnBoard
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhisoniya singh
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 

Recently uploaded (20)

How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptx
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 

Scaling Optimizing Workshop My Sql And Php State Of The Union Connectors Best Practices Performance Cloud

  • 1. www.nyphp.com / www.nyphp.org MySQL and PHP – State of the Union Connectors, Best Practices, Performance, and the “Cloud” New York City MySQL Meetup Web Performance for PHP Developers Scaling & Optimizing MySQL December 9th, 2008 Web Performance Meetup/Etsy.com Sun Microsystems CommunityOne East June 16th, 2009 December 16th, 2008 Sun Microsystems March 18th, 2009 Hans Zaunere, Managing Member 6/18/2009 © 2008 New York PHP, LLC 1
  • 2. www.nyphp.com / www.nyphp.org Overview • Introduction • Connectors • Best Practices and Techniques • Performance and Scaling • Scaling and Performance • Clouds and Smog • Questions/Resources 6/18/2009 © 2008 New York PHP, LLC 2
  • 3. www.nyphp.com / www.nyphp.org Introduction AMP Needs No Introduction LAMP • Apache/MySQL/PHP • Other variations on the theme BAMP – But the fundamentals remain OSAMP SAMP PHP glues together high- speed database with high- WAMP speed external libraries XAMP DAMP 6/18/2009 © 2008 New York PHP, LLC 3
  • 4. www.nyphp.com / www.nyphp.org Connectors… I’m a • mysql – The Classic • mysqli – Improving a Classic • PDO – Abstracted Abstraction • mysqlnd – A PHP Native Pick the Right Connector 6/18/2009 © 2008 New York PHP, LLC 4
  • 5. www.nyphp.com / www.nyphp.org Connectors… mysql Extension - The Classic • http://www.php.net/mysql • Still one of the fastest and quickest to implement • Results are always strings • Very mature and proven code • Strictly functional • No binary protocol support • Always be real – mysql_real_escape_string() EVERYTHING 6/18/2009 © 2008 New York PHP, LLC 5
  • 6. www.nyphp.com / www.nyphp.org Connectors… mysqli Extension – Improving a Classic • http://www.php.net/mysqli • Recommended for all new development • Supports new and old MySQL features • Prepared statements and binary protocol available – Result sets are returned as PHP native types – Some advantages/disadvantages • Choice between functional or object-based code • Just got persistent connections – Now “idiot proof” and yields almost 7x connections/second gain 6/18/2009 © 2008 New York PHP, LLC 6
  • 7. www.nyphp.com / www.nyphp.org Connectors… PDO – Abstracted Abstraction • http://www.php.net/pdo http://www.php.net/pdo-mysql http://dev.mysql.com/tech-resources/articles/mysql-pdo.html • PHP Data Object • Provides consistent OO interface to multiple database – May deviously hide differences between databases Know thy configuration and available database features • Segmentation fault issues early on – still gun shy • Still being developed, including support for mysqlnd • Recommended only if abstraction is an immediate requirement 6/18/2009 © 2008 New York PHP, LLC 7
  • 8. www.nyphp.com / www.nyphp.org Connectors… mysqlnd Library – A PHP Native • http://blog.ulf-wendel.de/ (current) http://blog.felho.hu/what-is-new-in-php-53-part-3-mysqlnd.html • Optional backend library for mysql/mysqli/PDO • Takes advantage of PHP/Zend Engine internal structure • Supports async queries • Deprecates --enable-mysqlnd-threading • Long time in coming but still apparently developed • Available in PHP 5.3 (alpha) • Not recommended for prime time quite yet 6/18/2009 © 2008 New York PHP, LLC 8
  • 9. www.nyphp.com / www.nyphp.org Connectors …Survey Says • Stick with - or upgrade to - mysqli 6/18/2009 © 2008 New York PHP, LLC 9
  • 10. www.nyphp.com / www.nyphp.org Best Practices… • Modern applications should use mysqli • Know your hardware architecture – Use x64 where possible for RAM – Avoid mixed 32/x64 environments/libs – PHP needs --with-libdir=lib64 – In production environments strive to have a pure lib64 system – Install and link against the right libraries for your architecture • Know your compile options – Don’t let ./configure guess – --with-mysqli=/usr/bin/mysql_config - specify file! – --with-mysqli=mysqlnd (alpha) • Know your client/server versions – Make sure they match as closely as possible 6/18/2009 © 2008 New York PHP, LLC 10
  • 11. www.nyphp.com / www.nyphp.org …and Techniques… • mysqli objects or functions? Your choice but… – Objects may provide a more flexible, elegant and fool-proof implementation • Be explicit – Specify the MySQL resource or result in every function – mysqli’s OO implementation enforces better practices $MYDB = mysql_connect('www.nyphp.com','root','=ro0t--'); // BAD $R = mysql_query('SELECT * FROM BB.Account'); // GOOD $R = mysql_query('SELECT * FROM BB.Account',$MYDB); 6/18/2009 © 2008 New York PHP, LLC 11
  • 12. www.nyphp.com / www.nyphp.org …and Techniques • Be prepared with prepared statements? – Consider what the actual benefits are for your application – Not all applications may benefit • What’s wrong with this picture? $R = mysql_query('SELECT * FROM BB.Account',$MYDB); $R2 = array(); while( ($R2[] = mysql_fetch_assoc($R)) !== FALSE ); 6/18/2009 © 2008 New York PHP, LLC 12
  • 13. www.nyphp.com / www.nyphp.org Performance and Scaling… www.mysqlperformanceblog.com • Consider use case – Need to support varied queries in a framework environment? – … or highly repetitive queries in a batch environment? • Consider highest-cost resources for your application – RAM? CPU? Database round-trips? • Consider your data’s flavor – Big blobs? Small strings and ints? – Lots of rows? Small result sets? – Complexity and relationships? 6/18/2009 © 2008 New York PHP, LLC 13
  • 14. www.nyphp.com / www.nyphp.org Performance and Scaling… • Consider memory usage vs CPU – Storing results as an array of arrays (non-columnar) – mysqli_result_fetch_all(), mysqli_result_fetch_row(), etc. – Storing results as an columnar array – mysql_stmt_fetch_column() available in C but not in ext/mysqli (NEEDED) // 1663 rows $R = mysql_query('SELECT * FROM BB.Account',$MYDB); $R2 = array(); // Non-columnar 0.034s 3.99mb while( ($T = mysql_fetch_assoc($R)) !== FALSE ) $R2[] = $T; // Columnar 0.048s 41.18% 3.37mb -15.54% while( ($T = mysql_fetch_assoc($R)) !== FALSE ) { foreach( $T as $Col => $Val ) $R2[$Col][] = $Val; } 6/18/2009 © 2008 New York PHP, LLC 14
  • 15. www.nyphp.com / www.nyphp.org Performance and Scaling… Connectors - Connectors - Connectors • Simple benchmarking of the current Connector scene • 500 executions of a simple SELECT • Retrieving 1663 rows each time mysql> SELECT * FROM BB.Account `AccountGID` bigint(20) `R_UserGID` bigint(20) `InsertedTS` timestamp `UpdatedTS` timestamp `LastLoginTS` timestamp `Status` varchar(31) collate utf8_unicode_ci `Type` varchar(31) collate utf8_unicode_ci `Admin` tinyint(3) `Bucks` decimal(15,2) `BID` varchar(11) collate utf8_unicode_ci `CurrentAlias` varchar(63) collate utf8_unicode_ci `Description` varchar(1023) collate utf8_unicode_ci 6/18/2009 © 2008 New York PHP, LLC 15
  • 16. www.nyphp.com / www.nyphp.org Performance and Scaling… Text Queries (Text Protocol) store_result use_result Notes (buffered) (unbuffered) mysql_query() ext/mysql 18.2s 10.0s mysql_unbuffered_query() ext/mysqli 19.2s 10.0s mysqli_result::free() required for unbuffered mysqli_result::fetch_all() ext/mysqli::mysqlnd 20.7s 10.0s not better than for() iteration mysqli_result::free() required for unbuffered • All values available as PHP strings, regardless of column’s type • NULLs generally remain as NULLs in PHP • Always *_free_result() as a best practice • Required for unbuffered queries 6/18/2009 © 2008 New York PHP, LLC 16
  • 17. www.nyphp.com / www.nyphp.org Performance and Scaling… Prepared Statements (Binary Protocol) store_result use_result Notes (buffered) (unbuffered) ext/mysqli 12.5s/12.3s 8.4/8.2s prepare on execute / prepare once ext/mysqli::mysqlnd 14.6s/14.2s 8.4s/8.2s mysqli_result::free() required for unbuffered ext/mysqli 26.6s 21.0s additional PHP coding to mimic mysqli_result::fetch_assoc() behavior ext/mysqli::mysqlnd 29.2s 22.4s • Each prepare requires an additional database roundtrip • http://forge.mysql.com/worklog/task.php?id=3359 • All values available as PHP types corresponding to column type • *_free_result() and *_stmt_close() not always needed • But recommended as best practice 6/18/2009 © 2008 New York PHP, LLC 17
  • 18. www.nyphp.com / www.nyphp.org Performance and Scaling… Observations and Upshots • No significant difference between localhost vs remote • …as it relates to buffered vs. unbuffered • mysqlnd has actually appeared slower overall • Slowdown in handling of PHP variables – WHAT? • Framework operations • Handle a variety of queries • Typically pull rows for later processing • Unbuffered (use_result) is faster but.. • Avoid lengthy per-row processing retrieval as the tables are locked until all rows are fetched (mostly a MyISAM problem) • Use buffered (store_result) in this case 6/18/2009 © 2008 New York PHP, LLC 18
  • 19. www.nyphp.com / www.nyphp.org Performance and Scaling… Observations and Upshots • Specific/batch operations • Prepared statements are better • Large data, highly repetitive • Not well implemented for general query backend • Complex logic required to mimic array/object row fetching – SHAME • Generally - or - all things considered… • No huge difference between prepared and text queries • Prepared problematic depending on data structures required YMIGTV – Your Mileage Is Guaranteed To Vary 6/18/2009 © 2008 New York PHP, LLC 19
  • 20. www.nyphp.com / www.nyphp.org Performance and Scaling… Optimize Optimize Optimize • Buffers and configuration • Tune buffers for storage engines and operations • Queries • Indexes and correct usage absolutely critical • Avoid automatic query generation • WRITE QUERIES CAREFULLY • Hardware • RAM is fast but disks can be too… pick your battles • Augment CPU for crunch-intensive applications MySQL Responds Very Well To The Right TLC 6/18/2009 © 2008 New York PHP, LLC 20
  • 21. www.nyphp.com / www.nyphp.org Performance and Scaling… I’ll Say It Again • Buffers and configuration • Architect the storage engines/schema from the outset • Tune the right buffers (key_buffer_size, innodb_*, heap, etc) • Queries • Try to use numeric keys • Double check EXPLAIN – left/to/right/prefixed • Partitioning and disk layout • Be careful with sub-queries and temp. tables Convenience Kills Performance MySQL Will Kill You Without TLC 6/18/2009 © 2008 New York PHP, LLC 21
  • 22. www.nyphp.com / www.nyphp.org …Scaling and Performance Scaling Performance…? • Define scaling for your application and requirements • Frequent or Complex functional changes vs. moderate traffic • Functional Scaling • Stagnant or Simple functional changes vs. huge traffic • Traffic Scaling • Sharding i.e. Application Managed Partitioning • Functional vs. Key vs. Combination • Be ready for complexity – Bring in expertise • Add in memcached for shard recombination caching 6/18/2009 © 2008 New York PHP, LLC 22
  • 23. www.nyphp.com / www.nyphp.org Scaling and Performance Scaling Performance…? • Replication – Tried and True • Able to handle very heavy load if done correctly • Comfortable with both Functional and Traffic scaling • Master-Master is an option if application is aware • MySQL Cluster • Can provide some of the best performance in the industry… • … but only in specific cases • Pairs well with Sharding as a replacement for memcached • Keep tabs on your data’s path, lifecycle and type • Know where it’s come from, what it’s doing, and where it’s going Know Thy Data – Love Thy Data 6/18/2009 © 2008 New York PHP, LLC 23
  • 24. www.nyphp.com / www.nyphp.org Clouds and Smog Wait, what does that mean? • Keep Your Feet On the Ground – And Your Head Out of the Cloud • Clouds mean a lot of different things right now • Don’t put everything on one server – duh • There’s no silver bullet – Don’t try to cheat • Give it time, progress is being made • No cloud will simply scale a database – why…? • Cloud isn’t parallel processing (yet) • Varying types of Clouds • Application/API - Azure, Google Apps • Virtualization - Sun, EC2/Xen, VZ, VMWare • Start-up marketing fluff – BE WARY It’s All About Architecture and Optimization ALWAYS 6/18/2009 © 2008 New York PHP, LLC 24
  • 25. www.nyphp.com / www.nyphp.org Thank You hans.zaunere@nyphp.com For renowned online support, New York PHP Mailing Lists are free and available to anyone: http://www.nyphp.org/mailinglists.php MySQL SIG: http://lists.nyphp.org/mailman/listinfo/mysql 6/18/2009 © 2008 New York PHP, LLC 25