MySQL
  Home arrow MySQL arrow Page 7 - MySQL Optimization, part 1
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Moblin 
JMSL Numerical Library 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
MYSQL

MySQL Optimization, part 1
By: Sams Publishing
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 45
    2005-04-13

    Table of Contents:
  • MySQL Optimization, part 1
  • 6.1.4 The MySQL Benchmark Suite
  • 6.2.1 EXPLAIN Syntax (Get Information About a SELECT)
  • 6.2.2 Estimating Query Performance
  • 6.2.6 How MySQL Optimizes IS NULL
  • 6.2.9 How MySQL Optimizes ORDER BY
  • 6.2.12 Speed of INSERT Queries
  • 6.2.15 Other Optimization Tips

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    MySQL Optimization, part 1 - 6.2.12 Speed of INSERT Queries


    (Page 7 of 8 )

    The time to insert a record is determined by the following factors, where the numbers indicate approximate proportions:

    • Connecting: (3)

    • Sending query to server: (2)

    • Parsing query: (2)

    • Inserting record: (1 x size of record)

    • Inserting indexes: (1 x number of indexes)

    • Closing: (1)

    This does not take into consideration the initial overhead to open tables, which is done once for each concurrently running query.

    The size of the table slows down the insertion of indexes by log N, assuming B-tree indexes.

    You can use the following methods to speed up inserts:

    • If you are inserting many rows from the same client at the same time, use INSERT statements with multiple VALUES lists to insert several rows at a time. This is much faster (many times faster in some cases) than using separate single-row INSERT statements. If you are adding data to a non-empty table, you may tune the bulk_insert_buffer_size variable to make it even faster. See Section 4.2.3, "Server System Variables."

    • If you are inserting a lot of rows from different clients, you can get higher speed by using the INSERT DELAYED statement.

    • With MyISAM tables you can insert rows at the same time that SELECT statements are running if there are no deleted rows in the tables.

    • When loading a table from a text file, use LOAD DATA INFILE. This is usually 20 times faster than using a lot of INSERT statements.

    • With some extra work, it is possible to make LOAD DATA INFILE run even faster when the table has many indexes. Use the following procedure:

      1. Optionally create the table with CREATE TABLE.

      2. Execute a FLUSH TABLES statement or a mysqladmin flush-tables command.

        1. Use myisamchk --keys-used=0 -rq /path/to/db/tbl_name. This will remove all use of all indexes for the table.

        2. Insert data into the table with LOAD DATA INFILE. This will not update any indexes and will therefore be very fast.

        3. If you are going to only read the table in the future, use myisampack to make it smaller. See Section 8.1.3.3, "Compressed Table Characteristics."

        4. Re-create the indexes with myisamchk -r -q /path/to/db/tbl_name. This will create the index tree in memory before writing it to disk, which is much faster because it avoids lots of disk seeks. The resulting index tree is also perfectly balanced.

      3. Execute a FLUSH TABLES statement or a mysqladmin flush-tables command.

    • Note that LOAD DATA INFILE also performs the preceding optimization if you insert into an empty MyISAM table; the main difference is that you can let myisamchk allocate much more temporary memory for the index creation than you might want the server to allocate for index re-creation when it executes the LOAD DATA INFILE statement.

    • As of MySQL 4.0, you can also use ALTER TABLE tbl_name DISABLE KEYS instead of myisamchk --keys-used=0 -rq /path/to/db/tbl_name and ALTER TABLE tbl_name ENABLE KEYS instead of myisamchk -r -q /path/to/db/tbl_name. This way you can also skip the FLUSH TABLES steps.

    • You can speed up INSERT operations that are done with multiple statements by locking your tables:
      LOCK TABLES a WRITE;
      INSERT INTO a VALUES (1,23),(2,34),(4,33);
      INSERT INTO a VALUES (8,26),(6,29);
      UNLOCK TABLES;
    • A performance benefit occurs because the index buffer is flushed to disk only once after all INSERT statements have completed. Normally there would be as many index buffer flushes as there are different INSERT statements. Explicit locking statements are not needed if you can insert all rows with a single statement.

    • For transactional tables, you should use BEGIN/COMMIT instead of LOCK TABLES to get a speedup.

    • Locking also lowers the total time of multiple-connection tests, although the maximum wait time for individual connections might go up because they wait for locks. For example:
      Connection 1 does 1000 inserts
      Connections 2, 3, and 4 do 1 insert
      Connection 5 does 1000 inserts
    • If you don't use locking, connections 2, 3, and 4 will finish before 1 and 5. If you use locking, connections 2, 3, and 4 probably will not finish before 1 or 5, but the total time should be about 40% faster.

    • INSERT, UPDATE, and DELETE operations are very fast in MySQL, but you will obtain better overall performance by adding locks around everything that does more than about five inserts or updates in a row. If you do very many inserts in a row, you could do a LOCK TABLES followed by an UNLOCK TABLES once in a while (about each 1,000 rows) to allow other threads access to the table. This would still result in a nice performance gain.

    • INSERT is still much slower for loading data than LOAD DATA INFILE, even when using the strategies just outlined.

    • To get some more speed for MyISAM tables, for both LOAD DATA INFILE and INSERT, enlarge the key cache by increasing the key_buffer_size system variable. See Section 6.5.2, "Tuning Server Parameters."

    6.2.13 Speed of UPDATE Queries

    Update queries are optimized as a SELECT query with the additional overhead of a write. The speed of the write depends on the amount of data being updated and the number of indexes that are updated. Indexes that are not changed will not be updated.

    Also, another way to get fast updates is to delay updates and then do many updates in a row later. Doing many updates in a row is much quicker than doing one at a time if you lock the table.

    Note that for a MyISAM table that uses dynamic record format, updating a record to a longer total length may split the record. If you do this often, it is very important to use OPTIMIZE TABLE occasionally.

    6.2.14 Speed of DELETE Queries

    The time to delete individual records is exactly proportional to the number of indexes. To delete records more quickly, you can increase the size of the key cache. See Section 6.5.2, "Tuning Server Parameters."

    If you want to delete all rows in the table, use TRUNCATE TABLE tbl_name rather than DELETE FROM tbl_name.

    This article is excerpted from MySQL Administrator's Guide, by MySQL AB (editor) (Sams, 2004; ISBN 0672326345). Check it out at your favorite bookstore today. Buy this book now.

    More MySQL Articles
    More By Sams Publishing


     

       

    MYSQL ARTICLES

    - Take Some Load off MySQL with MemCached
    - MySQL Table Prefix Changer Tool in PHP
    - Using the SIGNAL Statement for Error Handling
    - Error Handling Examples
    - Error Handling
    - Completing a Search Engine with MySQL and PH...
    - Paginating Result Sets for a Search Engine B...
    - Building a Search Engine with MySQL and PHP 5
    - Using Boolean Operators for Full Text and Bo...
    - PHP, MySQL and the PEAR Database
    - Working with PHP and MySQL
    - Getting PHP to Talk to MySQL
    - Creating an RSS Reader: the Reader
    - MySQL Security Overview
    - Creating the Admin Script for a PHP/MySQL Bl...





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway