MySQL Query optimization

Source: Internet
Author: User
Tags benchmark idate mysql query mysql index mysql query optimization

Database query Optimization There are many things you can do. Here's a summary of some:
Basic principles:
    1. Reduce the number of disk I/Os that occur during database queries
      • Rational use of indexes
      • Avoid full table traversal, scan
    2. Reduce the amount of data transmitted over the network
      • Querying only the fields you need

  1. If you have group by and join at the same time, try group by before join
  2. Avoid using the MySQL function in the WHERE statement
  3. Use locate (substr, str) instead of like '%substr% '
  4. Use composite index (use Sub_part () for extra long fields)
  5. Avoid using SELECT *, you should specify the field to select directly
    • SELECT * Causes the database to translate * into actual fields, one more step
    • * Not every field is what we need, wasting disk I/O and network transport
    • If we need a select field (such as user_id) already indexed, select user_id will return the result directly from the index, while select * needs to find the result from the datasheet, at least one more disk I/O

Optimization Tools:Run several times in succession, looking at total time: SELECT BENCHMARK (100000000, LOCATE (' foo ', ' Foobar '));SELECT BENCHMARK (100000000, ' foobar ' like '%foo% ');
to see if an index is used:
Explain <sql Statement >
fields that can be indexed:
    • Which fields need to be indexed: for fields such as join, WHERE, ORDER by, GROUP by, MAX (), MIN ()
    • Which fields do not fit the index: Fields that contain a large number of duplicate values, such as Boolean

Index Effective Condition:
  • use;, >=, =, <, <=, IF null and between
  • using Max () and  min () function
  • by the order BY, GROUP by use case
  • like use wildcards at the end: where a like ' b% '
  • Which conditions the index does not take effect:
    • Cases with not in, <>
    • Like using wildcards to start with: where a like '%b '
    • If the column in where is already indexed, the index of another column in ORDER by does not take effect
    • For composite indexes, such as (C1,C2): Where c2=2 does not take effect
    • In the case of a column operation, such as: where year (iDate) <2015 (can be changed to where idate< ' 2015-01-01 ')

MySQL Index type:
    • Normal: Normal index type, no uniqueness limit
    • Unique: Requires that the value of the indexed field be unique
    • Fulltext: Can be created on a varchar or text type, only for tables of type MyISAM

Data type selection for indexed fields:
    • Indexed fields try to use small and simple data types (such as shaping) to avoid complex types (such as String types)
    • The indexed fields are limited to NOT NULL, and you can use 0,-1, empty strings instead of NULL

(original article, reprint please specify turn from: Http://blog.csdn.net/clementad)

To see if an index is used:
Explain <sql Statement >

MySQL Query optimization

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.