一、怎样用索引才高效

1.隔离索引列

MySQL generally can’t use indexes on columns unless the columns are isolated in the query. “Isolating” the column means it should not be part of an expression or be inside a function in the query.

如,以下的查询不能用actor_id索引

SELECT actor_id FROM sakila.actor WHERE actor_id + 1 = 5;

这个也不能应用索引

 SELECT ... WHERE TO_DAYS(CURRENT_DATE) - TO_DAYS(date_col) <= 10;

2.给长文本加上前缀索引

 -- 示范以列的部分前缀来建索引,首先找出数量最多的列和最常查询的列
CREATE TABLE sakila.city_demo(city VARCHAR(50) NOT NULL);
INSERT INTO sakila.city_demo(city) SELECT city FROM sakila.city;
-- Repeat the next statement five times:
INSERT INTO sakila.city_demo(city) SELECT city FROM sakila.city_demo;
-- Now randomize the distribution (inefficiently but conveniently):
UPDATE sakila.city_demo
SET city = (SELECT city FROM sakila.city ORDER BY RAND() LIMIT 1); SELECT COUNT(*) AS cnt, city FROM sakila.city_demo GROUP BY city ORDER BY cnt DESC LIMIT 10; SELECT COUNT(*) AS cnt, LEFT(city, 3) AS pref FROM sakila.city_demo GROUP BY pref ORDER BY cnt DESC LIMIT 10; SELECT COUNT(*) AS cnt, LEFT(city, 7) AS pref FROM sakila.city_demo GROUP BY pref ORDER BY cnt DESC LIMIT 10; --to find the full column’s selectivity:
SELECT COUNT(DISTINCT city)/COUNT(*) FROM sakila.city_demo; -- to find the selectivity of several prefix lengths in one query: SELECT COUNT(DISTINCT LEFT(city, 3))/COUNT(*) AS sel3,
COUNT(DISTINCT LEFT(city, 4))/COUNT(*) AS sel4,
COUNT(DISTINCT LEFT(city, 5))/COUNT(*) AS sel5,
COUNT(DISTINCT LEFT(city, 6))/COUNT(*) AS sel6,
COUNT(DISTINCT LEFT(city, 7))/COUNT(*) AS sel7
FROM sakila.city_demo; ALTER TABLE sakila.city_demo ADD KEY (city(7));

缺点:

Prefix indexes can be a great way to make indexes smaller and faster, but they have downsides too: MySQL cannot use prefix indexes for ORDER BY or GROUP BY queries, nor can it use them as covering indexes.
A common case we’ve found to benefit from prefix indexes is when long hexadecimal identifiers are used.

3.Multicolumn Indexes

When you see an index merge in EXPLAIN , you should examine the query and table structure to see if this is really the best you can get. You can disable index merges with the optimizer_switch option or variable. You can also use IGNORE INDEX

4.Choosing a Good Column Order

 -- 选择正确的列顺序作索引
SELECT SUM(staff_id = 2), SUM(customer_id = 584) FROM payment\G
SELECT SUM(staff_id = 2) FROM payment WHERE customer_id = 584\G
SELECT COUNT(DISTINCT staff_id)/COUNT(*) AS staff_id_selectivity,
COUNT(DISTINCT customer_id)/COUNT(*) AS customer_id_selectivity,
COUNT(*)
FROM payment\G ALTER TABLE payment ADD KEY(customer_id, staff_id); SELECT COUNT(DISTINCT threadId) AS COUNT_VALUE
FROM Message
WHERE (groupId = 10137) AND (userId = 1288826) AND (anonymous = 0)
ORDER BY priority DESC, modifiedDate DESC SELECT COUNT(*), SUM(groupId = 10137),
SUM(userId = 1288826), SUM(anonymous = 0)
FROM Message\G

5.等。。。。

高性能MySQL笔记-第5章Indexing for High Performance-004怎样用索引才高效的更多相关文章

  1. 高性能MySQL笔记-第5章Indexing for High Performance-001B-Tree indexes(B+Tree)

    一. 1.什么是B-Tree indexes? The general idea of a B-Tree is that all the values are stored in order, and ...

  2. 高性能MySQL笔记-第5章Indexing for High Performance-002Hash indexes

    一. 1.什么是hash index A hash index is built on a hash table and is useful only for exact lookups that u ...

  3. 高性能MySQL笔记-第5章Indexing for High Performance-005聚集索引

    一.聚集索引介绍 1.什么是聚集索引? InnoDB’s clustered indexes actually store a B-Tree index and the rows together i ...

  4. 高性能MySQL笔记-第5章Indexing for High Performance-003索引的作用

    一. 1. 1). Indexes reduce the amount of data the server has to examine.2). Indexes help the server av ...

  5. 高性能MySQL笔记 第6章 查询性能优化

    6.1 为什么查询速度会慢   查询的生命周期大致可按照顺序来看:从客户端,到服务器,然后在服务器上进行解析,生成执行计划,执行,并返回结果给客户端.其中“执行”可以认为是整个生命周期中最重要的阶段. ...

  6. 高性能MySQL笔记 第5章 创建高性能的索引

    索引(index),在MySQL中也被叫做键(key),是存储引擎用于快速找到记录的一种数据结构.索引优化是对查询性能优化最有效的手段.   5.1 索引基础   索引的类型   索引是在存储引擎层而 ...

  7. 高性能MySQL笔记 第4章 Schema与数据类型优化

    4.1 选择优化的数据类型   通用原则   更小的通常更好   前提是要确保没有低估需要存储的值范围:因为它占用更少的磁盘.内存.CPU缓存,并且处理时需要的CPU周期也更少.   简单就好   简 ...

  8. 高性能MySQL笔记-第1章MySQL Architecture and History-001

    1.MySQL架构图 2.事务的隔离性 事务的隔离性是specific rules for which changes are and aren’t visible inside and outsid ...

  9. 高性能MySQL笔记-第4章Optimizing Schema and Data Types

    1.Good schema design is pretty universal, but of course MySQL has special implementation details to ...

随机推荐

  1. 手动安装mysql-5.0.45.tar.gz

    Linux下编译安装 安装环境:VMware9(桥接模式) + Linux bogon 2.6.32-642.3.1.el6.x86_64(查看linux版本信息:uname -a) 先给出MySQL ...

  2. mysql_union all 纵向合并建表_20170123

    年前事情比较多,博客不能每天更新了. 1.union all 纵向建表和left join 横向建表的数据结构区别 先贴代码 后面再补充 (#销售确认额 SELECT '05收货销售额' AS 标识, ...

  3. 畅通工程(kruskal算法)

    个人心得:日了狗,WR了俩个小时才发现是少了个vector清理,我也是醉了,不过后面还是对这个有了更好得了解,一是我得算法,而是学长改进 后的算法,改进后得算法还要判断所有村庄是否在连在一起,其实我觉 ...

  4. Unity之将Texture保存成png

    http://blog.csdn.net/bingheliefeng/article/details/51177505 using UnityEngine;using System.Collectio ...

  5. http协议及原理分析 1

    1:200与304的区别 浏览器第一次加载成功返回200状态,并会在浏览器的缓存中记录下 max-age 这个值.第二次发起服务器的访问时 会先看缓存中有没有要加载的资源 如果有 再去看有没有超出 m ...

  6. bzoj 1798 [Ahoi2009]Seq 维护序列seq ——线段树

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1798 先乘后加,就可给加法标记乘上乘法标记. 注意可能有 *0 的操作,所以 pshd 时不 ...

  7. oracle+110个常用函数

    1.ASCII  返回与指定的字符对应的十进制数;  SQL> select ascii(A) A,ascii(a) a,ascii(0) zero,ascii( ) space from du ...

  8. Django基础(四)

    Form表单 Admin     Django Form表单 django 中的form 一般有两种功能: 输入html 验证用户输入 1,先写一个form import re from django ...

  9. final,finally和finalize三者的区别和联系

    对于初学者而言(当然也包括我)对于这三者真的不是很陌生,经常会看到它们.但对于三者之间的区别和联系一直是懵懵懂~~ 今天心情不错,那就简单总结一下它们几个的区别和联系吧.如果又不对的地方欢迎批评指正~ ...

  10. 2015.4.25利用UIAutomation 替代API函数,解决了ListView无法读数据的难题,顺便实现了鼠标模拟滚轮

    UIAutomation比API的优点是类似于消息处理机制,而不是主要靠模拟鼠标键盘发送消息 首先添加引用UIAutomationClient和UIAutomationTypes,在安装.net3.5 ...