High Performance MySQL 3rd Edition Sample -

Transcription

Table of ContentsForeword . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvPreface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii1. MySQL Architecture and History . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1MySQL’s Logical ArchitectureConnection Management and SecurityOptimization and ExecutionConcurrency ControlRead/Write LocksLock GranularityTransactionsIsolation LevelsDeadlocksTransaction LoggingTransactions in MySQLMultiversion Concurrency ControlMySQL’s Storage EnginesThe InnoDB EngineThe MyISAM EngineOther Built-in MySQL EnginesThird-Party Storage EnginesSelecting the Right EngineTable ConversionsA MySQL TimelineMySQL’s Development ModelSummary123344679101012131517192124282933342. Benchmarking MySQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35Why Benchmark?Benchmarking Strategies3537iii

What to MeasureBenchmarking TacticsDesigning and Planning a BenchmarkHow Long Should the Benchmark Last?Capturing System Performance and StatusGetting Accurate ResultsRunning the Benchmark and Analyzing ResultsThe Importance of PlottingBenchmarking ToolsFull-Stack ToolsSingle-Component ToolsBenchmarking Exampleshttp loadMySQL Benchmark Suitesysbenchdbt2 TPC-C on the Database Test SuitePercona’s TPCC-MySQL ToolSummary3840414244454749505151545455566164663. Profiling Server Performance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69Introduction to Performance OptimizationOptimization Through ProfilingInterpreting the ProfileProfiling Your ApplicationInstrumenting PHP ApplicationsProfiling MySQL QueriesProfiling a Server’s WorkloadProfiling a Single QueryUsing the Profile for OptimizationDiagnosing Intermittent ProblemsSingle-Query Versus Server-Wide ProblemsCapturing Diagnostic DataA Case Study in DiagnosticsOther Profiling ToolsUsing the USER STATISTICS TablesUsing 124. Optimizing Schema and Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115Choosing Optimal Data TypesWhole NumbersReal NumbersString Typesiv Table of Contents115117118119

Date and Time TypesBit-Packed Data TypesChoosing IdentifiersSpecial Types of DataSchema Design Gotchas in MySQLNormalization and DenormalizationPros and Cons of a Normalized SchemaPros and Cons of a Denormalized SchemaA Mixture of Normalized and DenormalizedCache and Summary TablesMaterialized ViewsCounter TablesSpeeding Up ALTER TABLEModifying Only the .frm FileBuilding MyISAM Indexes 1411421431455. Indexing for High Performance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147Indexing BasicsTypes of IndexesBenefits of IndexesIndexing Strategies for High PerformanceIsolating the ColumnPrefix Indexes and Index SelectivityMulticolumn IndexesChoosing a Good Column OrderClustered IndexesCovering IndexesUsing Index Scans for SortsPacked (Prefix-Compressed) IndexesRedundant and Duplicate IndexesUnused IndexesIndexes and LockingAn Indexing Case StudySupporting Many Kinds of FilteringAvoiding Multiple Range ConditionsOptimizing SortsIndex and Table MaintenanceFinding and Repairing Table CorruptionUpdating Index StatisticsReducing Index and Data 182184185187188189190192193194194195197199Table of Contents v

6. Query Performance Optimization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201Why Are Queries Slow?Slow Query Basics: Optimize Data AccessAre You Asking the Database for Data You Don’t Need?Is MySQL Examining Too Much Data?Ways to Restructure QueriesComplex Queries Versus Many QueriesChopping Up a QueryJoin DecompositionQuery Execution BasicsThe MySQL Client/Server ProtocolThe Query CacheThe Query Optimization ProcessThe Query Execution EngineReturning Results to the ClientLimitations of the MySQL Query OptimizerCorrelated SubqueriesUNION LimitationsIndex Merge OptimizationsEquality PropagationParallel ExecutionHash JoinsLoose Index ScansMIN() and MAX()SELECT and UPDATE on the Same TableQuery Optimizer HintsOptimizing Specific Types of QueriesOptimizing COUNT() QueriesOptimizing JOIN QueriesOptimizing SubqueriesOptimizing GROUP BY and DISTINCTOptimizing LIMIT and OFFSETOptimizing SQL CALC FOUND ROWSOptimizing UNIONStatic Query AnalysisUsing User-Defined VariablesCase StudiesBuilding a Queue Table in MySQLComputing the Distance Between PointsUsing User-Defined FunctionsSummaryvi Table of 6248248249249256256258262263

7. Advanced MySQL Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265Partitioned TablesHow Partitioning WorksTypes of PartitioningHow to Use PartitioningWhat Can Go WrongOptimizing QueriesMerge TablesViewsUpdatable ViewsPerformance Implications of ViewsLimitations of ViewsForeign Key ConstraintsStoring Code Inside MySQLStored Procedures and FunctionsTriggersEventsPreserving Comments in Stored CodeCursorsPrepared StatementsPrepared Statement OptimizationThe SQL Interface to Prepared StatementsLimitations of Prepared StatementsUser-Defined FunctionsPluginsCharacter Sets and CollationsHow MySQL Uses Character SetsChoosing a Character Set and CollationHow Character Sets and Collations Affect QueriesFull-Text SearchingNatural-Language Full-Text SearchesBoolean Full-Text SearchesFull-Text Changes in MySQL 5.1Full-Text Tradeoffs and WorkaroundsFull-Text Configuration and OptimizationDistributed (XA) TransactionsInternal XA TransactionsExternal XA TransactionsThe MySQL Query CacheHow MySQL Checks for a Cache HitHow the Cache Uses MemoryWhen the Query Cache Is HelpfulHow to Configure and Maintain the Query 0310312313314315315316318320323Table of Contents vii

InnoDB and the Query CacheGeneral Query Cache OptimizationsAlternatives to the Query CacheSummary3263273283298. Optimizing Server Settings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 331How MySQL’s Configuration WorksSyntax, Scope, and DynamismSide Effects of Setting VariablesGetting StartedIterative Optimization by BenchmarkingWhat Not to DoCreating a MySQL Configuration FileInspecting MySQL Server Status VariablesConfiguring Memory UsageHow Much Memory Can MySQL Use?Per-Connection Memory NeedsReserving Memory for the Operating SystemAllocating Memory for CachesThe InnoDB Buffer PoolThe MyISAM Key CachesThe Thread CacheThe Table CacheThe InnoDB Data DictionaryConfiguring MySQL’s I/O BehaviorInnoDB I/O ConfigurationMyISAM I/O ConfigurationConfiguring MySQL ConcurrencyInnoDB Concurrency ConfigurationMyISAM Concurrency ConfigurationWorkload-Based ConfigurationOptimizing for BLOB and TEXT WorkloadsOptimizing for FilesortsCompleting the Basic ConfigurationSafety and Sanity SettingsAdvanced InnoDB 803833859. Operating System and Hardware Optimization . . . . . . . . . . . . . . . . . . . . . . . . . . . . 387What Limits MySQL’s Performance?How to Select CPUs for MySQLWhich Is Better: Fast CPUs or Many CPUs?CPU Architectureviii Table of Contents387388388390

Scaling to Many CPUs and CoresBalancing Memory and Disk ResourcesRandom Versus Sequential I/OCaching, Reads, and WritesWhat’s Your Working Set?Finding an Effective Memory-to-Disk RatioChoosing Hard DisksSolid-State StorageAn Overview of Flash MemoryFlash TechnologiesBenchmarking Flash StorageSolid-State Drives (SSDs)PCIe Storage DevicesOther Types of Solid-State StorageWhen Should You Use Flash?Using FlashcacheOptimizing MySQL for Solid-State StorageChoosing Hardware for a ReplicaRAID Performance OptimizationRAID Failure, Recovery, and MonitoringBalancing Hardware RAID and Software RAIDRAID Configuration and CachingStorage Area Networks and Network-Attached StorageSAN BenchmarksUsing a SAN over NFS or SMBMySQL Performance on a SANShould You Use a SAN?Using Multiple Disk VolumesNetwork ConfigurationChoosing an Operating SystemChoosing a FilesystemChoosing a Disk Queue SchedulerThreadingSwappingOperating System StatusHow to Read vmstat OutputHow to Read iostat OutputOther Helpful ToolsA CPU-Bound MachineAn I/O-Bound MachineA Swapping MachineAn Idle 9431432434435436438438440441442443444444445Table of Contents ix

10. Replication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 447Replication OverviewProblems Solved by ReplicationHow Replication WorksSetting Up ReplicationCreating Replication AccountsConfiguring the Master and ReplicaStarting the ReplicaInitializing a Replica from Another ServerRecommended Replication ConfigurationReplication Under the HoodStatement-Based ReplicationRow-Based ReplicationStatement-Based or Row-Based: Which Is Better?Replication FilesSending Replication Events to Other ReplicasReplication FiltersReplication TopologiesMaster and Multiple ReplicasMaster-Master in Active-Active ModeMaster-Master in Active-Passive ModeMaster-Master with ReplicasRing ReplicationMaster, Distribution Master, and ReplicasTree or PyramidCustom Replication SolutionsReplication and Capacity PlanningWhy Replication Doesn’t Help Scale WritesWhen Will Replicas Begin to Lag?Plan to UnderutilizeReplication Administration and MaintenanceMonitoring ReplicationMeasuring Replication LagDetermining Whether Replicas Are Consistent with the MasterResyncing a Replica from the MasterChanging MastersSwitching Roles in a Master-Master ConfigurationReplication Problems and SolutionsErrors Caused by Data Corruption or LossUsing Nontransactional TablesMixing Transactional and Nontransactional TablesNondeterministic StatementsDifferent Storage Engines on the Master and Replicax Table of 5486487488489494495495498498499500

Data Changes on the ReplicaNonunique Server IDsUndefined Server IDsDependencies on Nonreplicated DataMissing Temporary TablesNot Replicating All UpdatesLock Contention Caused by InnoDB Locking SelectsWriting to Both Masters in Master-Master ReplicationExcessive Replication LagOversized Packets from the MasterLimited Replication BandwidthNo Disk SpaceReplication LimitationsHow Fast Is Replication?Advanced Features in MySQL ReplicationOther Replication 1151151251251451651811. Scaling MySQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 521What Is Scalability?A Formal DefinitionScaling MySQLPlanning for ScalabilityBuying Time Before ScalingScaling UpScaling OutScaling by ConsolidationScaling by ClusteringScaling BackLoad BalancingConnecting DirectlyIntroducing a MiddlemanLoad Balancing with a Master and Multiple 656056456512. High Availability . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 567What Is High Availability?What Causes Downtime?Achieving High AvailabilityImproving Mean Time Between FailuresImproving Mean Time to RecoveryAvoiding Single Points of FailureShared Storage or Replicated Disk567568569570571572573Table of Contents xi

Synchronous MySQL ReplicationReplication-Based RedundancyFailover and FailbackPromoting a Replica or Switching RolesVirtual IP Addresses or IP TakeoverMiddleman SolutionsHandling Failover in the ApplicationSummary57658058158358358458558613. MySQL in the Cloud . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 589Benefits, Drawbacks, and Myths of the CloudThe Economics of MySQL in the CloudMySQL Scaling and HA in the CloudThe Four Fundamental ResourcesMySQL Performance in Cloud HostingBenchmarks for MySQL in the CloudMySQL Database as a Service (DBaaS)Amazon RDSOther DBaaS SolutionsSummary59059259359459559860060060260214. Application-Level Optimization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 605Common ProblemsWeb Server IssuesFinding the Optimal ConcurrencyCachingCaching Below the ApplicationApplication-Level CachingCache Control PoliciesCache Object HierarchiesPregenerating ContentThe Cache as an Infrastructure ComponentUsing HandlerSocket and memcached AccessExtending MySQLAlternatives to 962015. Backup and Recovery . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 621Why Backups?Defining Recovery RequirementsDesigning a MySQL Backup SolutionOnline or Offline Backups?Logical or Raw Backups?xii Table of Contents622623624625627

What to Back UpStorage Engines and ConsistencyReplicationManaging and Backing Up Binary LogsThe Binary Log FormatPurging Old Binary Logs SafelyBacking Up DataMaking a Logical BackupFilesystem SnapshotsRecovering from a BackupRestoring Raw FilesRestoring Logical BackupsPoint-in-Time RecoveryMore Advanced Recovery TechniquesInnoDB Crash RecoveryBackup and Recovery ToolsMySQL Enterprise BackupPercona XtraBackupmylvmbackupZmanda Recovery ManagermydumpermysqldumpScripting 65265365565865865865965965966066166416. Tools for MySQL Users . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 665Interface ToolsCommand-Line UtilitiesSQL UtilitiesMonitoring ToolsOpen Source Monitoring ToolsCommercial Monitoring SystemsCommand-Line Monitoring with InnotopSummary665666667667668670672677A. Forks and Variants of MySQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 679B. MySQL Server Status . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 685C. Transferring Large Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 715D. Using EXPLAIN . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 719Table of Contents xiii

E. Debugging Locks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 735F. Using Sphinx with MySQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 745Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 771xiv Table of Contents

ForewordI’ve been a fan of this book for years, and the third edition makes a great book evenbetter. Not only do world-class experts share that expertise, but they have taken thetime to update and add chapters with high-quality writing. While the book has manydetails on getting high performance from MySQL, the focus of the book is on the process of improvement rather than facts and trivia. This book will help you figure outhow to make things better, regardless of changes in MySQL’s behavior over time.The authors are uniquely qualified to write this book, based on their experience, principled approach, focus on efficiency, and commitment to improvement. By experience, I mean that the authors have been working on MySQL performance from the dayswhen it didn’t scale and had no instrumentation to the current period where things aremuch better. By principled approach, I mean that they treat this like a science, firstdefining problems to be solved and then using reason and measurement to solve thoseproblems.I am most impressed by their focus on efficiency. As consultants, they don’t have theluxury of time. Clients getting billed by the hour want problems solved quickly. So theauthors have defined processes and built tools to get things done correctly and efficiently. They describe the processes in this book and publish source code for the tools.Finally, they continue to get better at what they do. This includes a shift in concernfrom throughput to response time, a commitment to understanding the performanceof MySQL on new hardware, and a pursuit of new skills like queueing theory that canbe used to understand performance.I believe this book augurs a bright future for MySQL. As MySQL has evolved to supportdemanding workloads, the authors have led a similar effort to improve the understanding of MySQL performance within the community. They have also contributeddirectly to that improvement via XtraDB and XtraBackup. I continue to learn from themand hope you take the time to do so as well.—Mark Callaghan, Software Engineer, Facebookxv

CHAPTER 8Optimizing Server SettingsIn this chapter, we’ll explain a process by which you can create a good configurationfile for your MySQL server. It is a roundabout trip, with many points of interest andside trips to scenic overlooks. These are necessary, because determining the shortestpath to a good configuration doesn’t start with studying configuration options andasking which ones you should set or how you should change them, nor does it startwith examining server behavior and asking whether any configuration options can improve it. It’s best to begin with an understanding of MySQL’s internals and behavior.You can then use that knowledge as a guide for how MySQL should be configured.Finally, you can compare the desired configuration to the current configuration andcorrect any differences that are important and worthwhile.People often ask, “What’s the optimal configuration file for my server with 32 GB ofRAM and 12 CPU cores?” Unfortunately, it’s not that simple. The server should beconfigured for the workload, data, and application requirements, not just the hardware.MySQL has scores of settings that you can change—but you shouldn’t. It’s usuallybetter to configure the basic settings correctly (and there are only a few that really matterin most cases) and spend more time on schema optimization, indexes, and query design.After you’ve set MySQL’s basic configuration options correctly, the potential gainsfrom further changes are usually small.On the other hand, the potential downside of fiddling with the configuration can begreat. We’ve seen more than one “highly tuned” server that was crashing constantly,stalling, or performing slowly due to unwise settings. We’ll spend a bit of time on whythat can happen and what not to do.So what should you do? Make sure the basics such as the InnoDB buffer pool and logfile size are appropriate, set a few safety and sanity options if you wish to prevent badbehavior (but note that these usually won’t improve performance—they’ll only avoidproblems), and then leave the rest of the settings alone. If you begin to experience aproblem, diagnose it carefully with the techniques shown in Chapter 3. If the problemis caused by a part of the server whose behavior can be corrected with a configurationoption, then you might need to change it.331

Sometimes you might also need to set specific configuration options that can have asignificant performance impact in special cases. However, these should not be part ofa basic server configuration file. You should set them only when you find the specificperformance problems they address. That’s why we don’t suggest that you approachconfiguration options by looking for bad things to improve. If something needs to beimproved, it should show up in query response times. It’s best to start your search withqueries and their response times, not with configuration options. This could save youa lot of time and prevent many problems.Another good way to save time and trouble is to use the defaults unless you know youshouldn’t. There is safety in numbers, and a lot of people are running with defaultsettings. That makes them the most thoroughly tested settings. Unexpected bugs canarise when you change things needlessly.How MySQL’s Configuration WorksWe’ll begin by explaining MySQL’s configuration mechanisms, before covering whatyou should configure in MySQL. MySQL is generally pretty forgiving about its configuration, but following these suggestions might save you a lot of work and time.The first thing to know is where MySQL gets configuration information: fromcommand-line arguments and settings in its configuration file. On Unix-like systems,the configuration file is typically located at /etc/my.cnf or /etc/mysql/my.cnf. If you useyour operating system’s startup scripts, this is typically the only place you’ll specifyconfiguration settings. If you start MySQL manually, which you might do when you’rerunning a test installation, you can also specify settings on the command line. Theserver actually reads the contents of the configuration file, removes any comment linesand newlines, and then processes it together with the command-line options.A note on terminology: because many of MySQL’s command-line options correspond to server variables, we sometimes use the terms option and variable interchangeably. Most variables have the same namesas their corresponding command-line options, but there are a few exceptions. For example, --memlock sets the locked in memory variable.Any settings you decide to use permanently should go into the global configurationfile, instead of being specified at the command line. Otherwise, you risk accidentallystarting the server without them. It’s also a good idea to keep all of your configurationfiles in a single place so that you can inspect them easily.Be sure you know where your server’s configuration file is located! We’ve seen peopletry unsuccessfully to configure a server with a file it doesn’t read, such as /etc/my.cnfon Debian servers, which look in /etc/mysql/my.cnf for their configuration. Sometimes332 Chapter 8: Optimizing Server Settings

there are files in several places, perhaps because a previous system administrator wasconfused as well. If you don’t know which files your server reads, you can ask it: which mysqld/usr/sbin/mysqld /usr/sbin/mysqld --verbose --help grep -A 1 'Default options'Default options are read from the following files in the given order:/etc/mysql/my.cnf /.my.cnf /usr/etc/my.cnfThis applies to typical installations, where there’s a single server on a host. You candesign more complicated configurations, but there’s no standard way to do this. TheMySQL server distribution used to include a now-deprecated program called mysqlmanager, which can run multiple instances from a single configuration with separatesections. (This was a replacement for the even older mysqld multi script.) However,many operating system distributions don’t include or use this program in their startupscripts. In fact, many don’t use the MySQL-provided startup script at all.The configuration file is divided into sections, each of which begins with a line thatcontains the section name in square brackets. A MySQL program will generally readthe section that has the same name as that program, and many client programs alsoread the client section, which gives you a place to put common settings. The serverusually reads the mysqld section. Be sure you place your settings in the correct sectionin the file, or they will have no effect.Syntax, Scope, and DynamismConfiguration settings are written in all lowercase, with words separated by underscores or dashes. The following are equivalent, and you might see both forms in command lines and configuration files:/usr/sbin/mysqld --auto-increment-offset 5/usr/sbin/mysqld --auto increment offset 5We suggest that you pick a style and use it consistently. This makes it easier to searchfor settings in your files.Configuration settings can have several scopes. Some settings are server-wide (globalscope); others are different for each connection (session scope); and others are perobject. Many session-scoped variables have global equivalents, which you can think ofas defaults. If you change the session-scoped variable, it affects only the connectionfrom which you changed it, and the changes are lost when the connection closes. Hereare some examples of the variety of behaviors of which you should be aware: The query cache size variable is globally scoped. The sort buffer size variable has a global default, but you can set it per-sessionas well.How MySQL’s Configuration Works 333

The join buffer size variable has a global default and can be set per-session, buta single query that joins several tables can allocate one join buffer per join, so theremight be several join buffers per query.In addition to setting variables in the configuration files, you can also change many(but not all) of them while the server is running. MySQL refers to these as dynamicconfiguration variables. The following statements show different ways to change thesession and global values of sort buffer size dynamically:SETsort buffer size value ;SET GLOBALsort buffer size value ;SET@@sort buffer size : value ;SET @@session.sort buffer size : value ;SET @@global.sort buffer size : value ;If you set variables dynamically, be aware that those settings will be lost when MySQLshuts down. If you want to keep the settings, you’ll have to update your configurationfile as well.If you set a variable’s global value while the server is running, the values for the currentsession and any other existing sessions are not affected. This is because the sessionvalues are initialized from the global value when the connections are created. Youshould inspect the output of SHOW GLOBAL VARIABLES after each change to make sure it’shad the desired effect.Variables use different kinds of units, and you have to know the correct unit for eachvariable. For example, the table cache variable specifies the number of tables that canbe cached, not the size of the table cache in bytes. The key buffer size is specified inbytes, whereas still other variables are specified in number of pages or other units, suchas percentages.Many variables can be specified with a suffix, such as 1M for one megabyte. However,this works only in the configuration file or as a command-line argument. When youuse the SQL SET command, you must use the literal value 1048576, or an expressionsuch as 1024 * 1024. You can’t use expressions in configuration files.There is also a special value you can assign to variables with the SET command: thekeyword DEFAULT. Assigning this value to a session-scoped variable sets that variable tothe corresponding globally scoped variable’s value; assigning it to a globally scopedvariable sets the variable to the compiled-in default (not the value specified in the configuration file). This is useful for resetting session-scoped variables back to the valuesthey had when you opened the connection. We advise you not to use it for globalvariables, because it probably won’t do what you want—that is, it doesn’t set the valuesback to what they were when you started the server.334 Chapter 8: Optimizing Server Settings

Side Effects of Setting VariablesSetting variables dynamically can have unexpected side effects, such as flushing dirtyblocks from buffers. Be careful which settings you change online, because this can causethe server to do a lot of work.Sometimes you can infer a variable’s behavior from its name. For example, maxheap table size does what it sounds like: it specifies the maximum size to which implicit in-memory temporary tables are allowed to grow. However, the naming conventions aren’t completely consistent, so you can’t always guess what a variable will do bylooking at its name.Let’s take a look at some commonly used variables and the effects of changing themdynamically:key buffer sizeSetting this variable allocates the designated amount of space for the key buffer (orkey cache) all at once. However, the operating system doesn’t actually commitmemory to it until it is used. Setting the key buffer size to one gigabyte, for example,doesn’t mean you’ve instantly caused the server to actually commit a gigabyte ofmemory to it. (We discuss how to watch the server’s memory usage in the nextchapter.)MySQL lets you create multiple key caches, as we explain later in this chapter. Ifyou set this variable to 0 for a nondefault key cache, MySQL discards any indexescached in the specified cache, begins to cache them in the default cache, and deletesthe specified cache when nothing is using it anymore. Setting this variable for anonexistent cache creates it. Setting the variable to a nonzero value for an existingcache will flush the specified cache’s memory. This blocks all operations that tryto access the cache until the flush is finished.table cache sizeSetting this variable has no immediate effect—the effect is delayed until the nexttime a thread opens a table. When this happens, MySQL checks the variable’svalue. If the value is larger than the number of tables in the cache, the thread caninsert the newly opened table into the cache. If the value is smaller than the numberof tables in the cache, MySQL deletes unused tables from the cache.thread cache sizeSetting this variable has no immediate effect—the effect is delayed until the nexttime a connection is closed. At that time, MySQL checks whether there is space inthe cache to store the thread. If so, it caches the thread for future reuse by anotherconnection. If not, it kills the thread instead of caching it. In this case, the numberof threads in the cache, and hence the

MySQL’s Storage Engines 13 The InnoDB Engine 15 The MyISAM Engine 17 Other Built-in MySQL Engines 19 Third-Party Storage Engines 21 Selecting the Right Engine 24 Table Conversions 28 A MySQL Timeline 29