标签:
ANALYZE TABLE语句更新表的统计数据,使查询优化器可以做出更合理的优化。存储引擎的特殊调试,索引和配置GUIDELINES解决,那么使用EXPLAIN语句查看更具体的信息结果字段
id : SELECT的标识符select_type : SELECT的类型
SIMPLE : Simple SELECT (not using UNION or subqueries)PRIMARY : Outermost SELECTUNION : Second or later SELECT statement in a UNIONDEPENDENT UNION : Second or later SELECT statement in a UNION, dependent on outer queryUNION RESULT RESULT of a UNIONSUBQUERY First SELECT in subqueryDEPENDENT SUBQUERY First SELECT in subquery, dependent on outer query.DERIVED Derived table SELECT (subquery in FROM clause)MATERIALIZED Materialized subqueryUNCACHEABLE SUBQUERY A subquery for which the result cannot be cached and must be re-evaluated for each row of the outer querytable : The name of the table to which the row of output refers
partitions : 使用的分区type : JOIN TYPEpossible_keys : 可选的索引key : 查询用到的key或者indexkey_len : The key_len column indicates the length of the key that MySQL decided to use. The length is NULL if the key column says NULL. Note that the value of key_len enables you to determine how many parts of a multiple-part key MySQL actually usesref : The ref column shows which columns or constants are compared to the index named in the key column to select rows from the table.rows : The rows column indicates the number of rows MySQL believes it must examine to execute the queryfiltered : The filtered column indicates an estimated percentage of table rows that will be filtered by the table condition. That is, rows shows the estimated number of rows examined and rows × filtered / 100 shows the number of rows that will be joined with previous tables.Extra : This column contains additional information about how MySQL resolves the query. See Explain Extra Information使用MIN()或者MAX()查询的时候多列索引的使用也遵循最左列的原则,即WHERE中要先使用多列索引中的最左列,那么查询优化器会将MIN()和MAX()做一个key上的扫描,然后将常数的结果进行替换。例如: SELECT MIN(key_part2),MAX(key_part2) FROM tbl_name WHERE key_part1=10;
LIKE也能使用索引,只要参数是常量,并且不以通配符开头Any index that does not span all AND levels in the WHERE clause is not used to optimize the query. In other words, to be able to use an index, a prefix of the index must be used in every AND group. The following WHERE clauses use indexes:
... WHERE index_part1=1 AND index_part2=2 AND other_column=3 /* index = 1 OR index = 2 */ ... WHERE index=1 OR A=10 AND index=2 /* optimized like "index_part1=‘hello‘" */ ... WHERE index_part1=‘hello‘ AND index_part3=5 /* Can use index on index1 but not on index2 or index3 */ ... WHERE index1=1 AND index2=2 OR index1=3 AND index3=3; These WHERE clauses do not use indexes: /* index_part1 is not used */ ... WHERE index_part2=1 AND index_part3=2 /* Index is not used in both parts of the WHERE clause */ ... WHERE index=1 OR A=10 /* No index spans all rows */ ... WHERE index_part1=1 OR index_part2=10
buffer pool 是Mysql中用来存放InnoDB的表和索引数据的内存区域, 这些内存区域被划分成页。buffer pool使用使用linked list作为pages的实现。页的交换使用LRU算法,在大内存的系统中,你可以将buffer pool划分成多个buffer pool instances。 innodb_buffer_pool_size中配置的内存大小被所有buffer pool实例所分割,多buffer pool实例更适给Innodb buffer pool划分几个G以上空间的应用,每一个实例都可以得到一个1G或者以上大小的空间。
如果InnoDB可以提前知道一个事务是只读事务,那么它就可以避免使用transaction ID(TRX_ID)。Transaction ID只需要在语句存在更新或者读语句中存在锁的情况中使用。 InnoDB是这样检测只读事务的:
对于innodb的表而言,insert、update、delete等操作虽然都是加行级锁,但这些行锁都是通过给索引上的索引项加锁来实现的,这就意味着:只有通过索引条件检索数据,innodb才能使用行级锁,否则,innodb将使用表级锁。 * 在不通过索引条件检索的时候,innodb使用的是表锁,不是行锁。 例如:
create table tab_no_index(id int,name varchar(10)) engion=innodb; create table tab_with_index(id int,name varchar(10),key id_idx(id)) engion=innodb; 语句select * from tab_no_index where id=1 for update;会加表锁,而select * from tab_with_index where id=1 for update;会加行锁。
虽然访问不同行的记录,但是如果是使用相同的索引键,仍然会出现锁冲突。 例如,上例表tab_with_index中有两条记录为(1,’1’)和(1,’4’),则select * from tab_with_index where id=1 and name=’1’ for update;会对这两条记录都加锁。
当表有多个索引时,不同的事务可以使用不同的索引锁定不同的行。此外,不论是使用主键索引、唯一索引或普通索引,innodb都会使用行锁对数据加锁。
当使用范围条件而不是相等条件检索数据,并请求共享或排他锁时,innodb会给符合条件的已有数据记录的索引项加锁;对于键值在条件范围内但并不 存在的记录,叫做“间隙”,innodb也会对这个“间隙”加锁。例如,emp表中只有101条记录,其中empid为索引键,值分别为 1,2,……,100,101,语句select * from emp where empid>100 for update;不仅会对101记录加锁,还会对empid大于101(这些记录不存在)的“间隙”加锁。
标签:
原文地址:http://www.cnblogs.com/katsura/p/5776152.html