Effective MySQL : optimizing SQL statements ; [practical knowledge ...

89 downloads 180 Views 156KB Size Report
CONTENTS. Acknoledgements xi. Introduction xii. 1. The Five Minute DBA. 1. Identifying Performance Problems. 2. Finding a Slow SQL Statement. 2. Confirming ...
\

ORACLG

Oracle Press

Effective

MySQL

Optimizing SQL

Statements

Ronald Bradford

Mc Grauu Hill

CONTENTS

Acknoledgements

xi

Introduction 1

xii

The Five Minute DBA

1

Performance Problems

Identifying Finding

a

Slow

Confirming the Optimizing

Your

SQL

2

Statement

Slow

2

Query

2 4

Query

What You Should Not Do

5

Confirm Your

5

The Correct

Optimization

Approach

6

An Alternative Solution

7

Conclusion 2

The Essential

7

Analysis

Commands

9

EXPLAIN

10

EXPLAIN PARTITIONS

11

EXPLAIN EXTENDED

12

SHOW CREATE TABLE

13

SHOW INDEXES

14

SHOW TABLE STATUS

15

SHOW STATUS

17

SHOW VARIABLES

19

INFORMATIONSCHEMA

20

Conclusion 3



Understanding MySQL Indexes Example MySQL Data

Tables

20

23 24

Index Usages

25

Integrity

25

Optimizing

Data Access

Table Joins

Sorting

26 28

Results

28 28

Aggregation

V

Contents

vi

About

28

Storage Engines

Index Terminology Index

MySQL

Types

Index Data Structure

Theory

MySQL Implementation MySQL Partitioning

43

Conclusion

4

^

Creating MySQL Indexes

46

Tables

Example

47

Existing Indexes

48

Single Column Indexes

48

Syntax

Restricting Rows with Joining Tables with

an

an

Index

Index

Understanding Index Cardinality Using Indexes for Selecting

a

35

43

Pattern

Unique

Matching

Row

A.fi,

50 ^1 54 55

Ordering Results

57

Multi Column Indexes

58

Determining

Which Index to Use

Multi Column Index Syntax

Providing

a

Better Index

Column Indexes

Many

Combining

61 63 64

Features

66

Hints

Complicated Queries The

61

WHERE and ORDER BY

MySQL Optimizer Query

58

Impact of Adding Indexes

67 71 71

Impact

71

DDL Impact

73

DML

Sp ace Impact

74

Limitations

76

Cost Based Optimizer

76

QEP Pinning

77

Index Statistics

77

Function Based Indexes

77

Multiple Indexes per Table

77

Disk

MySQL

Conclusion

78

Contents

5

Creating

Better MySQL Indexes

Better Indexes

79 •

80

Covering Index

80

Storage Engine Implications

84

Partial Index

85

Conclusion 6

vii

88

MySQL Configuration Options Memory

Related Variables

89 90

key_buffer_size

92

Named

93

Key

Buffers

innodb_buffer_pool_size

93

irvnodb_additional_rnem_pool_size

95

query_cache_size

95

max_heap_table„size

97

tmp_table_size

97

join_buffer_size

99

sort_buffer_size

99

read_buffer_size

99

read_rnd_buffer_size

99

Instrumentation Related Variables

10°

slow_query_log

100

slow_query_log_file

100

generaljog

101

general_log_file

101

long_query_time

101

log_output

101

101

Profiling Other

Optimization

Variables

1°2

optimizers witch

1°2

default_storage_engine

1°2

max_allowed_packet

103

sql_mode

103

innodb_strict_mode

103

Other Variables

1°3

Conclusion

104

viii

Contents

7

The SQL

Lifecycle

105

Capture Statements

106

General Query Log

107

Slow

108

Query Log

Binary Log

109

Processlist

HO

Status

Engine

HO

MySQL Connectors Application

111

Code

112

INFORMATION_SCHEMA

113

PERFORMANCE_SCHEMA

114

SQL Statement Statistics Plugin

114

MySQL Proxy

114

TCP/IP

114

Identify Problematic Statements Slow Query Log

Analysis

TCP/IP Analysis Confirm Statement

118

Operation

119

Environment

119

Timing

120

Analyze Statements

121

Optimize Statements

121

the Results

Verify

121

Conclusion 8

115 117

122

Hidden Performance Index Management

Combining Your

Tips

123

Optimizations

124

DDL

124

Removing Duplicate

Indexes

125

Removing Unused Indexes

126

Monitoring

Ineffective Indexes

126

Improvements

126

Index Column Data

Types

Column Other SQL

Types Optimizations

127 129 130

Eliminating SQL Statements

131

Simplifying SQL Statements

137

Using MySQL Replication

139

Conclusion

139

Contents

9

Explaining

the

MySQL

EXPLAIN

ix

141

Syntax

142

Explain Columns

142

key

144

rows

144

possible_keys

147

keyJen

147

table

149

select_type

149

partitions

150

Extra

150

id

152

ref

152

filtered

153

153

type

Interpreting Conclusion

EXPLAIN

Output

153 155