提交 5b66cbcf 编写于 作者: C CyC2018
......@@ -6,7 +6,7 @@
<div align="center">
<img src="other/LogoMakr_0zpEzN.png" width="150px">
<br>
<a href="other/Group.md"> <img src="https://img.shields.io/badge/>-group-4ab8a1.svg"></a> <a href="https://legacy.gitbook.com/book/cyc2018/interview-notebook/details"> <img src="https://img.shields.io/badge/_-gitbook-4ab8a1.svg"></a>
<a href="other/Group.md"> <img src="https://img.shields.io/badge/>-qq-4ab8a1.svg"></a> <a href="https://legacy.gitbook.com/book/cyc2018/interview-notebook/details"> <img src="https://img.shields.io/badge/_-gitbook-4ab8a1.svg"></a>
</div>
<!-- [![](https://img.shields.io/badge/>-gitter-blue.svg)](https://gitter.im/CyC2018-Interview-Notebook/Lobby?utm_source=share-link&utm_medium=link&utm_campaign=share-link) [![](https://img.shields.io/badge/_-gitbook-4ab8a1.svg)](https://legacy.gitbook.com/book/cyc2018/interview-notebook/details) -->
......
......@@ -304,6 +304,64 @@ Explain 用来分析 SELECT 查询语句,开发人员可以通过分析 Explai
最有效的方式是使用索引来覆盖查询。
### 3. 不要在列上使用函数和进行运算
在列上使用函数和计算将导致索引失效而进行全表扫描。
### 4. 尽量避免使用 != 或 not in 或 <> 等否定操作符
尽量避免在 where 子句中使用 or 来连接条件,因为这会导致索引失效而进行全表扫描。
### 5. 尽量避免使用 or 来连接条件
尽量避免在 where 子句中使用 or 来连接条件,因为这会导致索引失效而进行全表扫描。
### 6. 多个单列限制应该改成一个多列索引(复合索引)查询
因为 MySQL 只能使用一个单列索引,所以为多个列创建单列索引并不能提高 MySQL 的查询性能。正确的做法是将这多个要限定条件的列单独做成一个多列索引(复合索引)。
### 7. 查询时要按照复合索引的最左列开始查找
查询条件中没有使用复合索引的第一个字段,索引是不会被使用的。复合索引遵守“最左前缀”原则,即在查询条件中使用了复合索引的第一个字段,索引才会被使用。
假如我们只有一个复合索引 ```age_height_idx(age, height)```
如果我们使用
```mysql
select * from user where height = 170;
```
那么复合索引 ```age_height_idx(age, height)```将不会被使用。
### 8. 尽量不用范围查询
如果查询中的某个列有范围查询,则其右边所有列都无法使用索引优化查找。
比如说我们使用
```mysql
select * from users where age > 16 and age < 30 and height = 180
```
则索引中 age 右边所有列都无法使用索引优化查找。换句话说,```age_height_idx(age, height)``` 索引等价于 ```age_height_idx(age)```
### 9. 索引不要包含有NULL值的列
只要列中包含有 NULL 值,它就不会被包含在索引中,复合索引中只要有一列含有 NULL 值,那么这一列对于此复合索引就是无效的。
因此,在数据库设计时,除非有一个很特别的原因使用 NULL 值,不然尽量不要让字段的默认值为 NULL。
### 10. 隐式转换可能带来不好的影响
当 where 查询条件左右两侧类型不匹配的时候会发生隐式转换,隐式转换可能带来的不好影响就是导致索引失效而进行全表扫描。
下面的案例中,brith_date 是字符串,然而匹配的是整数类型,从而发生隐式转换。
```mysql
select from users where brith_date = 19950610;
```
### 11. like 语句的索引失效问题
当我们使用 like “value%” 的方式时是会使用索引的,但是对于 like “%value%” 这样的方式,必定会执行全表查询,这在数据量小的表,不存在性能问题,但是对于海量数据,全表扫描是非常可怕的事情。所以,根据业务需求,考虑使用 ElasticSearch 或 Solr 是个不错的方案。
## 重构查询方式
### 1. 切分大查询
......@@ -425,3 +483,5 @@ MySQL 读写分离能提高性能的原因在于:
- [How to create unique row ID in sharded databases?](https://stackoverflow.com/questions/788829/how-to-create-unique-row-id-in-sharded-databases)
- [SQL Azure Federation – Introduction](http://geekswithblogs.net/shaunxu/archive/2012/01/07/sql-azure-federation-ndash-introduction.aspx "Title of this entry.")
- [MySQL 索引背后的数据结构及算法原理](http://blog.codinglabs.org/articles/theory-of-mysql-index.html)
- [服务端指南 数据存储篇 | MySQL(04) 索引使用的注意事项](http://blog.720ui.com/2017/mysql_core_04_index_item/)
- [mysql 聚簇索引 和聚簇索引 (二级索引)的 那些事](https://blog.csdn.net/bigtree_3721/article/details/51335479)
......@@ -1296,7 +1296,10 @@ private boolean isSubtreeWithRoot(TreeNode root1, TreeNode root2) {
## 解题思路
### 递归
<<<<<<< HEAD
=======
>>>>>>> 0d7909db6d01bdfa0a0fd9abaee469b9ddd2e3a4
```java
public void Mirror(TreeNode root) {
if (root == null)
......@@ -1314,6 +1317,7 @@ private void swap(TreeNode root) {
```
### 迭代
<<<<<<< HEAD
```java
public void Mirror(TreeNode root) {
......@@ -1334,6 +1338,27 @@ private void swap(TreeNode node) {
node.left = node.right;
node.right = t;
}
=======
```java
public void Mirror(TreeNode root) {
if (root == null)
return;
Stack<TreeNode> stack = new Stack<>();
stack.push(root);
while (!stack.isEmpty()) {
TreeNode p = stack.pop();
if (p.left == null && p.right == null)
continue;
TreeNode left = p.left;
p.left = p.right;
p.right = left;
if (p.left != null)
stack.push(p.left);
if (p.right != null)
stack.push(p.right);
}
}
>>>>>>> 0d7909db6d01bdfa0a0fd9abaee469b9ddd2e3a4
```
# 28 对称的二叉树
......
......@@ -389,11 +389,11 @@ MVCC 不能解决幻读的问题,Next-Key Locks 就是为了解决这个问题
## Gap Locks
锁定一个范围内的索引,例如当一个事务执行以下语句,其它事务就不能在 t.c 中插入 15
间隙锁锁住的是多行,是一个数据范围。间隙锁主要是为了防止出现幻读,但是它会把锁定范围扩大,有时候也会给我们带来麻烦。在数据库参数中,控制间隙锁的参数是 ```innodb_locks_unsafe_for_binlog```, 这个参数默认值是 OFF,也就是启用间隙锁,它是一个bool值,当值为 true 时表示 disable 间隙锁。那为了防止间隙锁是不是直接将 ```innodb_locaks_unsafe_for_binlog``` 设置为 ```true``` 就可以了呢?不一定!而且这个参数会影响到主从复制及灾难恢复,这个方法还尚待商量
```sql
SELECT c FROM t WHERE c BETWEEN 10 and 20 FOR UPDATE;
```
间隙锁的出现主要集中在同一个事务中先 delete 后 insert 的情况下,当我们通过一个参数去删除一条记录的时候,如果参数在数据库中存在,那么这个时候产生的是普通行锁,锁住这个记录,然后删除,最后释放锁。如果这条记录不存在,问题就来了,数据库会扫描索引,发现这个记录不存在,这个时候的 delete 语句获取到的就是一个间隙锁,然后数据库会向左扫描扫到第一个比给定参数小的值,向右扫描扫描到第一个比给定参数大的值,然后以此为上下界限构建一个区间,进而锁住整个区间内的数据,一个特别容易出现死锁的间隙锁诞生了。
通过修改 ```innodb_locaks_unsafe_for_binlog``` 参数来取消间隙锁从而达到避免这种情况的死锁的方式尚待商量,那就只有修改代码逻辑,存在才删除,尽量避免去删除不存在的记录。
## Next-Key Locks
......
......@@ -12,4 +12,4 @@
交流群不讨论政治,不讨论有争议性的话题,不发表仇视言论,不传播谣言,不发布广告(招聘信息之类的可以)。
</br> <div align="center"><img src="group.jpg" width="300px"></div> </br>
</br> <div align="center"><img src="group.png" width="300px"></div> </br>
......@@ -5,7 +5,7 @@
## LeetCode 习题集合
* [LeetCode 解题集合](https://github.com/apachecn/leetcode/docs/Leetcode_Solutions)
* [LeetCode 解题集合](https://github.com/apachecn/LeetCode/tree/master/docs/Leetcode_Solutions)
## 模版要求
......@@ -19,7 +19,7 @@
> **案例模版**
[模版:007. Reverse Integer 反转整数](https://github.com/apachecn/leetcode/docs/Leetcode_Solutions/007._Reverse_Integer.md)
[模版:007. Reverse Integer 反转整数](https://github.com/apachecn/LeetCode/tree/master/docs/Leetcode_Solutions/007._Reverse_Integer.md)
## 项目贡献者
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册