赞
踩
select * from doc where title like '%XX'; --不能使用索引select * from doc where title like 'XX%'; --非前导模糊查询,可以使用索引
select * from doc where status=1union allselect * from doc where status=2;
select * from doc where status in (1, 2);
select * from doc where status = 1 or status = 2
select * from doc where status != 1 and status != 2;
select * from doc where status in (0,3,4);
select uid, login_time from user where login_name=? andpasswd=?
建立联合索引的时候,区分度最高的字段在最左边
存在非等号和等号混合判断条件时,在建立索引时,把等号条件的列前置。如 where a>? and b=?,那么即使a 的区分度更高,也必须把 b 放在索引的最前列。
最左前缀查询时,并不是指SQL语句的where顺序要和联合索引一致。
select uid, login_time from user where passwd=? andlogin_name=?
假如index(a,b,c), where a=3 and b like 'abc%' and c=4,a能用,b能用,c不能用。
select * from employees.titles where emp_no
select * from doc where YEAR(create_time) <= '2016';
select * from doc where create_time <= '2016-01-01';
select * from order where date
select * from order where date
select * from user where phone=13800001234
select * from user where phone='13800001234';
Select uid, login_time from user where login_name=? and passwd=?
selecta.* from 表1 a,(select id from 表1 where 条件 limit100000,20 ) b where a.id=b.id;
select * from user where login_name=?;
select * from user where login_name=? limit 1
Index Selectivity = Cardinality / #T
SELECT count(DISTINCT(title))/count(*) AS Selectivity FROM employees.titles;+-------------+| Selectivity |+-------------+| 0.0000 |+-------------+
EXPLAIN SELECT * FROM employees.employees WHERE first_name='Eric' AND last_name='Anido';+----+-------------+-----------+------+---------------+------+---------+------+--------+-------------+| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |+----+-------------+-----------+------+---------------+------+---------+------+--------+-------------+| 1 | SIMPLE | employees | ALL | NULL | NULL | NULL | NULL | 300024 | Using where |+----+-------------+-----------+------+---------------+------+---------+------+--------+-------------+
SELECT count(DISTINCT(first_name))/count(*) AS Selectivity FROM employees.employees;+-------------+| Selectivity |+-------------+| 0.0042 |+-------------+SELECT count(DISTINCT(concat(first_name, last_name)))/count(*) AS Selectivity FROM employees.employees;+-------------+| Selectivity |+-------------+| 0.9313 |+-------------+
SELECT count(DISTINCT(concat(first_name, left(last_name, 3))))/count(*) AS Selectivity FROM employees.employees;+-------------+| Selectivity |+-------------+| 0.7879 |+-------------+
SELECT count(DISTINCT(concat(first_name, left(last_name, 4))))/count(*) AS Selectivity FROM employees.employees;+-------------+| Selectivity |+-------------+| 0.9007 |+-------------+
ALTER TABLE employees.employeesADD INDEX `first_name_last_name4` (first_name, last_name(4));
SHOW PROFILES;+----------+------------+---------------------------------------------------------------------------------+| Query_ID | Duration | Query |+----------+------------+---------------------------------------------------------------------------------+| 87 | 0.11941700 | SELECT * FROM employees.employees WHERE first_name='Eric' AND last_name='Anido' || 90 | 0.00092400 | SELECT * FROM employees.employees WHERE first_name='Eric' AND last_name='Anido' |+----------+------------+---------------------------------------------------------------------------------+
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。