Mysql function of depth exploration, database design, build master-slave replication, clustering, query optimization, load balancing

Table 1. Structure

 Storage Engine
        MyIsam: application to read and insert operations-based, only a small number of updates and deletes, and the integrity of the transaction, concurrency requirements are not very high. (Read-only suitable table, such as tables and the like)
        the InnoDB: data consistency requirements of the transaction, as well as concurrent conditions. (Suitable for batch operations with frequent changes)

Field Type

  • Appropriate choice of field type, the choice of minimum length to reduce storage space, to make use of type int;
  • Possible use varchar / nvarchar instead of char / nchar, because first of all variable-length fields small storage space, you can save storage space, followed by the query, in a relatively small field of search efficiency obviously higher;
  • Do not use the default null value, will result in a full table scan, other (0, -1) in place;

2. Index

        Index is some specific sorting algorithm for a particular mysql field, such as binary tree algorithm and hash algorithms, hashing through the establishment of eigenvalues, and then to quickly find the feature value. The most versatile, and is the mysql default is binary tree algorithm BTREE, indexed by field BTREE algorithm, such as scanning lines 20 will be able to get the results of the scan lines 2 ^ 20 before BTREE unused.

        In fact, the index is also a table that holds the primary key and index fields, and point to record the entity table.
    (Https://www.cnblogs.com/shijingxiang/articles/4743324.html)

Clustered index, non-clustering index (secondary index) 

    https://www.cnblogs.com/crazylqy/p/7615393.html

    https://blog.csdn.net/u013132035/article/details/82193763

    https://blog.csdn.net/GitChat/article/details/78787837

    http://www.imooc.com/article/267890

  • Primary key index, foreign key indexes;
  • Queries are often used in conditions where Lie Jiasuo primer (based scene analysis);
  • Composite index (determined to be the only query columns simultaneously use);
  • To create an index to select high-dimensional (large number of distinct values ​​appear) column;

Advantages: speed up queries

Disadvantages: the index will reduce write speed (due to the dynamic maintenance of the index), with the increasing amount of data, the index file will be growing        

3. Query Optimization

  • Enabling query caching

       Cache conditions: The query cache can be seen as the result of SQL query text and maps. If SQL and SQL identical to the first query the second query (note must be exactly the same, even if more than one space or different capitalization are considered different) and opens the query cache, then the second query directly from the Query result obtaining cache;
        cache invalidation mechanisms: the structure, or the data table is changed, the query data in the cache is no longer valid.

  • Turn on slow query log, regularly check the SQL statement;
  • Explain the SQL statement using the distraction of performance; https: //www.runoob.com/w3cnote/mysql-index.html
  •  

SQL statement optimization

  • Avoid using select *, what you want to check;
  • Avoid using <>, MySQL only the index for the following operator: <, <=, =,>,> =, BETWEEN, IN;
  • Avoid the use of or, to result in a full table scan, may be used instead of the union;    
  • in some cases and not in will lead to a full table scan; (in general, when you are too much in IN conditions, or can not be estimated, the optimizer inclined to a full table scan is low when IN conditions, if. optimizer thinks, iNDEX SEEK when you can benefit, will still take the index.)
  • A function operation to avoid field, resulting in a full table scan;
  • When more than one table join query (join), please use an alias, you can reduce the resolution time and reduce those grammatical errors caused by Column ambiguity;

 

4. separated from the master configuration, read and write

       In the actual production environment from a single Mysql as a standalone database is completely unable to meet the actual needs, whether it is in all aspects of security, high availability and high concurrency. Thus, in general is to synchronize the data from the copy mode (Master-Slave) through the primary, then a scheme to enhance the load capacity of concurrent database by separate read and write (MySQL-Proxy) to deploy the embodiment.
       Master-slave replication principle:
       the main library (Master) to open binary log (binlog, record all log database operations); the main library authorize an account to the library (Slave), from the library as a client of the main library to access the main library binaries log record to their relay log (relay log); the thread from the SQL database detects own relay log, and executes;
    
    specific implementation process:
        (1) open command execution start slave from the master copy on the server Slave switch, to start from the master copy. 
        (2) At this point, Slave server IO thread will request a connection Master server by copying the user rights on the master has authorized and requested the specified location from the implementation binlog log file (log file name and location is in the configuration master-slave replication services after the implementation of change master command specified) begin sending binlog log contents. 
        After (3) Master server receives a request from the IO Slave server thread, the thread which is responsible for replication of the IO read in batches binlog log information for the specified file specified binlog log information according to the location after the thread Slave IO requests to the server, and then back to the end of the Slave IO thread.
        (4) When the server IO thread Slave to acquire the content log transmission of Master IO thread server, the log file and the location point, the contents will be sequentially written to the log binlog Slave terminal itself Relay Log (i.e. relay log) files ( Master server can tell Mysql-relay-bin.xxx) very end, and a new file name and location binlog recorded master-info file, so that the next end of the new master binlog read the specified file from the new log and log binlog binlog position to start reading the new contents of the log 
        (5) Slave server SQL thread real-time detection of a local Relay log in IO thread the new log, and then promptly put the contents of Relay lOG file parsed into sql statement, and in its own Slave on the server by executing the application parses SQL statements in order of position this sql statement, and the current application relay log file name and location of the points recorded in relay-log.info in
    
   separate read and write (read from master write)
        from the server is only responsible for reading, the main server is responsible for a write operation,
        implementation:
        judgment 1. program (developers write their own)
        2. using middleware, MySQL- Proxy is to achieve "read Separation (Read / Write Splitting) "is a software. The basic principle is to let the Lord handle database write operations (insert, update, delete), and from a database query processing operations (select). The consistency of the database is to achieve the main slave replication. So is the basis of the master copy from the separate read and write.

 

5, sub-libraries, district, sub-table
   

https://blog.csdn.net/u011665991/article/details/90038768

Zoning: https://blog.csdn.net/vbirdbest/article/details/82461109

Zoning principle:

Partition table is implemented by multiple related underlying tables, these tables are also represented by the underlying object handles, so we can also directly access each partition, partition storage engine management and a common list is no different, there is no need to know this storage engine is a ordinary table is part of the partition table.

Operation on the partition table is carried out according to the following operating logic: 

    1. select query: partition layer to open and lock all the underlying table, the optimizer determines whether the filtering portion partition, and then call the corresponding memory interface to access each partition. 
    2. insert operation: open and lock all the partition layer underlying table, then that partition is determined to accept the record, then the record is written which corresponds to the underlying table.
    3. delete operation: open and lock the first partition layer underlying all tables, mysql first determine the target partition, the corresponding final underlying table delete operation.
    4. update operation: open and lock the partition layer to all the underlying tables, mysql first determine which partition record is updated again, and then remove the data update, data after determining which partition the updated should be placed, and then write the underlying table operation, and the underlying raw data table delete operation.

Guess you like

Origin www.cnblogs.com/eess/p/11448696.html