SQL笔记--第四章

来源:互联网 发布:如何选笔记本电脑 知乎 编辑:程序博客网 时间:2024/05/29 09:43
1 索引弊端:占用额外的磁盘空间,降低写操作。


2 索引准则
(1)最适合索引的列是出现在WHERE子句的列,或连接子句中指定的列
(2)唯一值的列,索引效果最好
(3)使用短索引。对串列进行索引,应指定一个前缀长度
(4)利用最左前缀。N列作为一个索引,则可利用N个索引。如:state city zip作为一个索引。则可用索引state,state city,state city zip
(5)不要过度索引
(6)考虑在列上进行的比较类型。索引可用于< <= + >= > between。若列用于其他类型的运算,对其进行索引没有价值。


3 explain显示如何使用索引处理select以及连接表。用于选择更好的索引和写出更优化的查询语句。


4 索引优化
(1)比较中尽量傅索引列独立,如
应为 where my_col < 4/2
不应为 where my_col * 2 <4
应为 where date_col < "1900-01-01"
不应为 where year(date_col) < 1900
应为 where date_col < date_add(current_date, interval cutoff day)
不应为 where to_days(date_col) - to_days(current_date) < cutoff
(2)LIKE模式转换
应为 where last_name >= "Mac" and last_name < "Mad"
不应为 where last_name like "Mac%"


5 索引列类型选择
(1)使用定长列
(2)尽量使用较短的列
(3)将列定义为NOT NULL
(4)考虑使用ENUM列
(5)使用procedure analyse(),获取列的选择
select * from tbl_name procedure analyse()
(6)避免检索较大的BLOB或TEXT值
(7)将BLOB值隔离在一个独立的表中


6 成批装载较单行快,因为不需要刷新索引,装完后才刷新
无索引比索引装载快


7 写入请求按其到达的次序进行处理
写入具有比读取更高的优先权
可通过关键字改变优先级
insert low_priority into tbl_name values();
high_priority


8 写入延迟,对innodb无效
对myisam有效,insert delayed into tbl_name values()
0 0
原创粉丝点击