Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1862455
  • 博文数量: 211
  • 博客积分: 464
  • 博客等级: 下士
  • 技术积分: 3794
  • 用 户 组: 普通用户
  • 注册时间: 2011-01-24 18:25
个人简介

阿弥陀佛

文章分类

全部博文(211)

文章存档

2020年(2)

2019年(3)

2018年(5)

2017年(6)

2016年(10)

2015年(9)

2014年(73)

2013年(90)

2012年(13)

分类: Mysql/postgreSQL

2018-03-20 14:51:20


1. 当某一列检索到的内容特别多的时候以至于使用索引无法提高性能,则sql不会使用正常的多列索引。
使用了错误的类型,无法正确比较,不会出现索引, 详见7.。
2. django 的 chain filter 不会创造多个sql语句,而是将各个filter的条件放在一起。chain filter的用法:models.objects.filter(f=1).filter(a=2)
3. 当column的类型与比较的数值的类型不一致的情况下不会使用索引。
4. 多列索引在创建时应该选择selective比较大的列放在前面。例如:
SELECT count(distinct lookup_1)/count(*) as `lookup_1` , count(distinct lookup_2)/count(*) as `lookup2`,
count(distinct lookup_3)/count(*) as `lookup_3` , count(distinct lookup_4)/count(*) as `lookup4`, count(*) FROM beepay_txn_db.event_addon_5_tab;
如果某一列非重复的部分越多,那么他的selectivity就越大,索引的效果就越好。
5. Innodb的secondary index 会指向primary key。
get_batch_transfer_txn_by_partner_id_and_create_time(1,0, 1520000000)
MyModel.objects.all().query 可以查询到Model的sql。
django model 还好不会将function带入到sql中。
6. 是否使用index也和key的分布有关系,如果某一个column的key的分布非常不平均,使用key才会有效。
7. 关于mysql是否会使用某个index, 会根据数据的分布情况,而不仅仅是cardinality, low cardinality 虽然看上去并不利于选择index,但是这个并不是唯一的评判, 有些column虽然只有两个值,但是’MALE‘ 占了95%,'FEMALE' 只占了5%。

mysql> EXPLAIN SELECT id FROM user WHERE sex="MALE";
+----+-------------+-------+------+---------------+------+---------+------+------+-------------+
| id | select_type | table | type | possible_keys | key  | key_len | ref  | rows | Extra       |
+----+-------------+-------+------+---------------+------+---------+------+------+-------------+
|  1 | SIMPLE      | user  | ALL  | sex_age       | NULL | NULL    | NULL | 3687 | Using where |
+----+-------------+-------+------+---------------+------+---------+------+------+-------------+
1 row in set (0.11 sec)
 
mysql> EXPLAIN SELECT id FROM user WHERE sex="FEMALE";
+----+-------------+-------+------+---------------+---------+---------+-------+------+-------------+
| id | select_type | table | type | possible_keys | key     | key_len | ref   | rows | Extra       |
+----+-------------+-------+------+---------------+---------+---------+-------+------+-------------+
|  1 | SIMPLE      | user  | ref  | sex_age       | sex_age | 2       | const |    9 | Using where |
+----+-------------+-------+------+---------------+---------+---------+-------+------+-------------+
1 row in set (0.00 sec)


8. 要保证每个表的index的数量不要太多,同时也要保证index的level不要太多,影响性能。
阅读(1322) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~