Your SlideShare is downloading. ×
Оптимизация MySQL. Что должен знать каждый разработчик
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Оптимизация MySQL. Что должен знать каждый разработчик

2,426

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
2,426
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
16
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. MySQL:Основыоптимизации
    или
    Чтодолжен знать каждыйразработчик
  • 2. АгниславОнуфрийчук
    Обо мне
  • 3. АгниславОнуфрийчук
    Веб-разработчик
    Обо мне
  • 4. АгниславОнуфрийчук
    Веб-разработчик
    Люблю девушек
    Обо мне
  • 5. АгниславОнуфрийчук
    Веб-разработчик
    Люблю девушек
    Не люблю MS Internet Exporerи Opera
    Обо мне
  • 6. Ктоработает с MySQL?
    О вас
  • 7. Ктоработает с MySQL?
    КтоиспользуетMyISAM?
    О вас
  • 8. Ктоработает с MySQL?
    КтоиспользуетMyISAM?
    Ктоещё не используетInnoDB?
    О вас
  • 9. Ктоработает с MySQL?
    КтоиспользуетMyISAM?
    Ктоещё не используетInnoDB?
    Кто не знает про my.ini/my.cnf?
    О вас
  • 10. Здесьзнают толк в оптимизацииMySQL
    О PortaOne
  • 11. Здесьзнают толк в оптимизацииMySQL
    Здесьзнают толк в девушках
    О PortaOne
  • 12. Здесьзнают толк в оптимизацииMySQL
    Здесьзнают толк в девушках
    Здесьзнают толк в программистах
    О PortaOne
  • 13. Здесьзнают толк в оптимизацииMySQL
    Здесьзнают толк в девушках
    Здесьзнают толк в программистах
    Здесьзнают толк в after-parties.
    О PortaOne
  • 14. Здесьзнают толк в оптимизацииMySQL
    Здесьзнают толк в девушках
    Здесьзнают толк в программистах
    Здесьзнают толк в after-parties.
    О PortaOne
  • 15. Начнём?
  • 16. ОптимизацияMySQL
  • 17. Всё плохо.

    ОптимизацияMySQL
  • 18. Сегоднямы не поговорим о…
  • 19. Шардинге
    Сегоднямы не поговорим о…
  • 20. Шардинге
    Железе
    Сегоднямы не поговорим о…
  • 21. Шардинге
    Железе
    Операционных системах
    Сегоднямы не поговорим о…
  • 22. Шардинге
    Железе
    Операционных системах
    Файловых системах
    Сегоднямы не поговорим о…
  • 23. Сегоднямыпоговорим о…
  • 24. Движках (engines)
    Сегоднямыпоговорим о…
  • 25. Движках (engines)
    Базовых настройках
    Сегоднямыпоговорим о…
  • 26. Движках (engines)
    Базовых настройках
    Полезныхутилитах
    Сегоднямыпоговорим о…
  • 27. Движках (engines)
    Базовых настройках
    Полезныхутилитах
    Запросах
    Сегоднямыпоговорим о…
  • 28. Движках (engines)
    Базовых настройках
    Полезныхутилитах
    Запросах
    И о чём-нибудьприятном
    Сегоднямыпоговорим о…
  • 29.  Engines
     Settings
     Utilities
     Queries
     Girls 
    Roadmap
  • 30.  Engines
     Settings
     Utilities
     Queries
     Girls 
    Roadmap
  • 31.  Engines
     Settings
     Utilities
     Queries
    Roadmap
  • 32.  Engines
     Settings
     Utilities
     Queries
    Roadmap
  • 33. Engines
  • 34. WTF?
    Engines
  • 35. WTF? It’s cool!
    Engines
  • 36. InnoDB/XtraDB
    MyISAM/Maria
    Memory
    NDB
    CSV
    Blackhole
    Engines
  • 37. Extremely fast
    Not-transactional
    Table-level locking
    No BLOB/TEXT data types
    Engines: Memory
  • 38. Extremely fast
    Transactional
    Row-level locking
    Good scaling capabilities
    Engines: NDB
  • 39. Simple data exchange
    No table partitioning
    No NULL values (since 5.1.23)
    Engines: CSV
  • 40. Extremely fast (faster then Memory/NDB)
    Transactional
    All types of indexes
    Low-cost storage
    Engines: Blackhole
  • 41. Default for years (up to 5.5)
    Non-Transactional
    Table-level locking
    Full-text search indexes
    Fast read
    Engines: MyISAM
  • 42. Transactional
    Row-level locking
    Foreign keys
    Simple performance tuning
    Engines: InnoDB
  • 43. All InnoDB feature plus:
    “Out-of-box” better performance
    Most powerful performance tuning
    “Troubleshoot without guesswork”
    Engines: XtraDB
  • 44.  Engines
     Settings
     Utilities
     Queries
    Roadmap
  • 45. There hundreds of them…
    Settings
  • 46. There hundreds of them…
    but a couple of them are enough for us
    Settings
  • 47. Using memory is better than using disk
    The main point is…
  • 48. query_cache_size
    Does matter for SELECT-dedicated server
    32M is generally enough
    Settings
  • 49. innodb_buffer_pool_size
    Give all you can:
    Servers: 50-75% of available memory
    Dev PC: 25-50% of available memory
    Settings
  • 50. innodb_log_file_size
    Make your log files big.
    128M/256M is a good value
    Settings
  • 51. innodb_log_buffer_size
    How often we’ll flush log data to disk
    4M is a good value unless you’re piping large blobs.
    Settings
  • 52. innodb_flush_log_at_trx_commit
    0 – write log buffer to file and flush it to disk every second
    1 – write&flush to disk for every transaction
    2 – write for every transaction and flush every second
    Settings
  • 53. innodb_thread_concurrency=8
    Just use this value
    Settings
  • 54. innodb_flush_method=O_DIRECT
    Avoid double buffering
    Settings
  • 55. innodb_file_per_table
    Use it if you don’t have tons of tables
    Settings
  • 56.  Engines
     Settings
     Utilities
     Queries
    Roadmap
  • 57. tuning-primer.sh
    mysql-tuner.pl
    maatkit
    innotop
    Utilities
  • 58. http://www.day32.com/MySQL/
    tuning-primer.sh
  • 59. SLOW QUERIES
    Current long_query_time = 10 sec.
    You have 526 out of 36204146 that take longer than 10 sec. to complete
    The slow query log is NOT enabled.
    Your long_query_time may be too high, I typically set this under 5 sec.
    tuning-primer.sh
  • 60. QUERY CACHEQuery cache is enabledCurrent query_cache_size = 8 MCurrent query_cache_used = 7 MCurrent query_cach_limit = 1 MCurrent Query cache fill ratio = 89.38 %However, 254246 queries have been removed from the query cache due to lack of memoryPerhaps you should raise query_cache_sizeMySQL won't cache query results that are larger than query_cache_limit in size 
    tuning-primer.sh
  • 61. TEMP TABLES
    Current max_heap_table_size = 16 M
    Current tmp_table_size = 32 M
    Of 35170 temp tables, 74% were created on disk
    Effective in-memory tmp_table_size is limited to max_heap_table_size.
    Perhaps you should increase your tmp_table_size and/or max_heap_table_size to reduce the number of disk-based temporary tables
    Note! BLOB and TEXT columns are not allow in memory tables.
    If you are using these columns raising these values might not impact your ratio of on disk temp tables.
    tuning-primer.sh
  • 62. http://mysql-tuner.pl
    mysql-tuner.pl
  • 63. -------- Storage Engine Statistics ---------------------
    [--] Status: -Archive +BDB -Federated +InnoDB -ISAM -NDBCluster
    [--] Data in MyISAM tables: 19M (Tables: 90)
    [!!] InnoDB is enabled but isn't being used
    [!!] BDB is enabled but isn't being used
    [!!] Total fragmented tables: 18
    mysql-tuner.pl
  • 64. -------- Performance Metrics ---------------------------
    [--] Up for: 16m 37s (6K q [6.059 qps], 146 conn, TX: 54M, RX: 665K)
    [--] Reads / Writes: 62% / 38%
    [--] Total buffers: 298.0M global + 6.3M per thread (100 max threads)
    [OK] Maximum possible memory usage: 929.2M (26% of installed RAM)
    [OK] Slow queries: 0% (0/6K)
    [OK] Highest usage of available connections: 5% (5/100)
    [OK] Key buffer size / total MyISAM indexes: 256.0M/2.3M
    [!!] Key buffer hit rate: 91.3% (1K cached / 101 reads)
    [OK] Query cache efficiency: 97.6% (5K cached / 5K selects)
    mysql-tuner.pl
  • 65. -------- Recommendations -------------------------------
    General recommendations:
    Add skip-innodb to MySQL configuration to disable InnoDB
    Add skip-bdb to MySQL configuration to disable BDB
    Run OPTIMIZE TABLE to defragment tables for better performance
    MySQLstarted within last 24 hours - recommendations may be inaccurate
    Enable the slow query log to troubleshoot bad queries
    mysql-tuner.pl
  • 66. maatkit
    http://www.maatkit.org/
  • 67. maatkit
    mk-error-log
    mk-log-player
    mk-index-usage
    mk-query-advisor
    mk-query-digest
    mk-query-profiler
    … (>30)
  • 68. maatkit: mk-error-log
    Count Level Message
    ===== ======= =======================================
    5 info mysqld started
    4 info mysqld version info
    3 info InnoDB: Started
    2 info mysqld ended
    1 unknown Number of processes running now: 0
    1 error [ERROR] /usr/sbin/mysqld: unknown variable 'ssl-ke
    1 error [ERROR] Failed to initialize the master info struc
  • 69. maatkit: mk-log-player
    mk-log-player does two things: it splits MySQL query logs into session files and it plays (executes) queries in session files on a MySQL server.
  • 70. maatkit: mk-index-usage
    This tool connects to a MySQL database server, reads through a query log, and uses EXPLAIN to ask MySQL how it will use each query. When it is finished, it prints out a report on indexes that the queries didn't use.
  • 71. maatkit: mk-query-advisor
    mk-query-advisor examines queries and applies rules to them, trying to find queries that look bad according to the rules. It reports on queries that match the rules, so you can find bad practices or hidden problems in your SQL.
  • 72. maatkit: mk-query-digest
    # pct total min max avg 95% stddev median
    # Count 0 2
    # Exec time 13 1105s 552s 554s 553s 554s 2s 553s
    # Lock time 0 216us 99us 117us 108us 117us 12us 108us
    # Rows sent 20 6.26M 3.13M 3.13M3.13M3.13M 12.73 3.13M
    # Rows exam 0 6.26M 3.13M 3.13M3.13M3.13M 12.73 3.13M

    # Query_time distribution
    # 1us
    # 10us
    # 100us
    # 1ms
    # 10ms
    # 100ms
    # 1s
    # 10s+ #############################################################
  • 73. maatkit: mk-query-profiler
    mk-query-profiler reads a file containing one or more SQL statements or shell commands, executes them, and analyzes the output of SHOW STATUS afterwards. It then prints statistics about how the batch performed. For example, it can show how many table scans the batch caused, how many page reads, how many temporary tables, and so forth.
  • 74. http://code.google.com/p/innotop/
    innotop
  • 75.  Engines
     Settings
     Utilities
     Queries
    Roadmap
  • 76. Queries
  • 77. use ORMs
    use Indexes
    be concrete (limit yourself)
    get a map!
    Queries
  • 78. why?
    use ORMs
  • 79. experience
    safety
    clear code
    speeding development
    portability
    use ORMs
  • 80. why?
    use Indexes
  • 81. CREATE TABLE employee (
    employee_number char(10) NOT NULL,
    firstnamevarchar(40),
    surname varchar(40),
    address text,
    tel_novarchar(25),
    salary int(11),
    overtime_rateint(10) NOT NULL
    );
    use Indexes
  • 82. use Indexes
    NOT using PRIMARY KEY
    Using PRIMARY KEY
    EXPLAIN SELECT employee_number, firstname, surname FROM employee WHERE id=1000G
    id: 1
    select_type: SIMPLE
    table: employee
    type: const
    possible_keys: PRIMARY
    key: PRIMARY
    key_len: 4
    ref: const
    rows: 1
    Extra:
    id: 1
    select_type: SIMPLE
    table: employee
    type: ALL
    possible_keys: NULL
    key: NULL
    key_len: NULL
    ref: NULL
    rows: 10000
    Extra: Using where
  • 83. use Indexes
    ALTER TABLE employee ADD INDEX(surname, firstname);
    SELECT overtime_rate FROM employee
    WHERE surname='Madida';
    SELECT overtime_rate FROM employee
    WHERE firstname='Mpho';
    SELECT overtime_rate FROM employee
    WHERE surname='Madida' and firstname="Mpho";
    SELECT overtime_rate FROM employee
    WHERE firstname="Mpho" and surname='Madida';
  • 84. use Indexes
    ALTER TABLE employee ADD INDEX(surname, firstname);
    SELECT overtime_rate FROM employee
    WHERE surname='Madida';
    SELECT overtime_rate FROM employee
    WHERE firstname='Mpho';
    SELECT overtime_rate FROM employee
    WHERE surname='Madida' and firstname="Mpho";
    SELECT overtime_rate FROM employee
    WHERE firstname="Mpho" and surname='Madida';
  • 85. use Indexesand be concrete
    select … WHERE year(my_date) > 2010
    select … where email LIKE “%@gmail.com”
    select * from table;
    select * from table limit 10000;
    select col1 from table where id > 0;
    insert into table values(‘1’, ‘bla’);
  • 86. create table `ids` (
    `id` int(11) NOT NULL AUTO_INCREMENT,
    `id1` int(11) NOT NULL,
    `id2` int(11) DEFAULT NULL,
    …,
    PRIMARY KEY (`id`)
    ) ENGINE=InnoDB;
    SELECT COUNT(*) FROM `ids`;
    SELECT COUNT(`id`) FROM `ids`;
    SELECT COUNT(`id1`) FROM `ids`;
    SELECT COUNT(`id2`) FROM `ids`;
    get a map!
  • 87.  Engines
    Settings
     Utilities
     Queries
    Roadmap
  • 88. Questions?
  • 89. Contacts
    vcard
    agnislav @
    gmail
    skype
    twitter
    facebook
    vkontakte
    livejournal

×