前言

  之前已经简单介绍了MySQL的优化步骤,那么接下来自然而是就是常用的SQL优化,比如inseer、group by等常用SQL的优化,会涉及SQL语句内部细节(这正是我缺乏的)。最后希望自己能记录完成的一套MySQL优化博文!

  注:其中部分我并没有全部实验(并不代表是错的),这里只相当于记录下,接下来会慢慢补充!

  参考资料:《深入浅出MySQL》(有需要PDF电子书的伙伴可以评论或者私信我)


1、大批量插入数据优化

(1)对于MyISAM存储引擎的表,可以使用:DISABLE KEYS 和 ENABLE KEYS 用来打开或者关闭 MyISAM 表非唯一索引的更新。

ALTER TABLE tbl_name DISABLE KEYS;
loading the data
ALTER TABLE tbl_name ENABLE KEYS;

(2)对于InnoDB引擎,有以下几种优化措施:

  ① 导入的数据按照主键的顺序保存:这是因为InnoDB引擎表示按照主键顺序保存的,如果能将插入的数据提前按照排序好自然能省去很多时间。

  比如bulk_insert.txt文件是以表user主键的顺序存储的,导入的时间为15.23秒

mysql> load data infile 'mysql/bulk_insert.txt' into table user;
Query OK, 126732 rows affected (15.23 sec)
Records: 126732 Deleted: 0 Skipped: 0 Warnings: 0

  没有按照主键排序的话,时间为:26.54秒

mysql> load data infile 'mysql/bulk_insert.txt' into table user;
Query OK, 126732 rows affected (26.54 sec)
Records: 126732 Deleted: 0 Skipped: 0 Warnings: 0

  ② 导入数据前执行SET UNIQUE_CHECKS=0,关闭唯一性校验,带导入之后再打开设置为1:校验会消耗时间,在数据量大的情况下需要考虑。

  ③ 导入前设置SET AUTOCOMMIT=0,关闭自动提交,导入后结束再设置为1:这是因为自动提交会消耗部分时间与资源,虽然消耗不是很大,但是在数据量大的情况下还是得考虑。

2、INSERT的优化

(1)尽量使用多个值表的 INSERT 语句,这种方式将大大缩减客户端与数据库之间的连接、关闭等消耗。(同一客户的情况下),即:

 INSERT INTO tablename values(1,2),(1,3),(1,4)

实验:插入8条数据到user表中(使用navicat客户端工具)

insert into user values(1,'test',replace(uuid(),'-',''));
insert into user values(2,'test',replace(uuid(),'-',''));
insert into user values(3,'test',replace(uuid(),'-',''));
insert into user values(4,'test',replace(uuid(),'-',''));
insert into user values(5,'test',replace(uuid(),'-',''));
insert into user values(6,'test',replace(uuid(),'-',''));
insert into user values(7,'test',replace(uuid(),'-',''));
insert into user values(8,'test',replace(uuid(),'-',''));

得到反馈:

[SQL] insert into user values(1,'test',replace(uuid(),'-',''));
受影响的行: 1
时间: 0.033s
[SQL]
insert into user values(2,'test',replace(uuid(),'-',''));
受影响的行: 1
时间: 0.034s
[SQL]
insert into user values(3,'test',replace(uuid(),'-',''));
受影响的行: 1
时间: 0.056s
[SQL]
insert into user values(4,'test',replace(uuid(),'-',''));
受影响的行: 1
时间: 0.008s
[SQL]
insert into user values(5,'test',replace(uuid(),'-',''));
受影响的行: 1
时间: 0.008s
[SQL]
insert into user values(6,'test',replace(uuid(),'-',''));
受影响的行: 1
时间: 0.024s
[SQL]
insert into user values(7,'test',replace(uuid(),'-',''));
受影响的行: 1
时间: 0.004s
[SQL]
insert into user values(8,'test',replace(uuid(),'-',''));
受影响的行: 1
时间: 0.004s

总共的时间为0.171秒,接下来使用多值表形式:

insert into user values
(9,'test',replace(uuid(),'-','')),
(10,'test',replace(uuid(),'-','')),
(11,'test',replace(uuid(),'-','')),
(12,'test',replace(uuid(),'-','')),
(13,'test',replace(uuid(),'-','')),
(14,'test',replace(uuid(),'-','')),
(15,'test',replace(uuid(),'-','')),
(16,'test',replace(uuid(),'-',''));

得到反馈:

[SQL] insert into user values
(9,'test',replace(uuid(),'-','')),
(10,'test',replace(uuid(),'-','')),
(11,'test',replace(uuid(),'-','')),
(12,'test',replace(uuid(),'-','')),
(13,'test',replace(uuid(),'-','')),
(14,'test',replace(uuid(),'-','')),
(15,'test',replace(uuid(),'-','')),
(16,'test',replace(uuid(),'-',''));
受影响的行: 8
时间: 0.038s

得到时间为0.038,这样一来可以很明显节约时间优化SQL

(2)如果在不同客户端插入很多行,可使用INSERT DELAYED语句得到更高的速度,DELLAYED含义是让INSERT语句马上执行,其实数据都被放在内存的队列中。并没有真正写入磁盘。LOW_PRIORITY刚好相反。

(3)将索引文件和数据文件分在不同的磁盘上存放(InnoDB引擎是在同一个表空间的)。

(4)如果批量插入,则可以增加bluk_insert_buffer_size变量值提供速度(只对MyISAM有用)

(5)当从一个文本文件装载一个表时,使用LOAD DATA INFILE,通常比INSERT语句快20倍。

3、GROUP BY的优化

  在默认情况下,MySQL中的GROUP BY语句会对其后出现的字段进行默认排序(非主键情况),就好比我们使用ORDER BY col1,col2,col3...所以我们在后面跟上具有相同列(与GROUP BY后出现的col1,col2,col3...相同)ORDER BY子句并没有影响该SQL的实际执行性能。

  那么就会有这样的情况出现,我们对查询到的结果是否已经排序不在乎时,可以使用ORDER BY NULL禁止排序达到优化目的。下面使用EXPLAIN命令分析SQL。

  在user_1中执行select id, sum(money) form user_1 group by name时,会默认排序(注意group by后的column是非index才会体现group by的排序,如果是primary key,那之前说过了InnoDB默认是按照主键index排好序的)

mysql> select*from user_1;
+----+----------+-------+
| id | name | money |
+----+----------+-------+
| 1 | Zhangsan | 32 |
| 2 | Lisi | 65 |
| 3 | Wangwu | 44 |
| 4 | Lijian | 100 |
+----+----------+-------+
4 rows in set

不禁止排序,即不使用ORDER BY NULL时:有明显的Using filesort。

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAkMAAADvCAYAAADxeNAnAAAZKUlEQVR4nO3dy471yF0A8HMOZ3JBgCCBhKwhCYE8wCxnlSfIG0QCBGITwSr55puZRT9Bv0G/AOIiLgJplrPlJsQlyiogJESUKImChpnDd7o/d1dbZVf5XrZ/P2m+6T5ll8tlH/vfVXbV8bvf//hyeO2XPnU4/MEffftwd3tzAADYg/Px+PRL+PPS3n///We/v/XWW9H0+uelSJW/az6l7edY+ze1VP2F+7HWfSjRGGVew7Ep3bUO91Z3zps8a7mGz+V0jX8eY6BLy5Izux6YtoNTpdUPaClS5V+7OfZvjmNb+jFaYyB0NfT7Wd/vUr/nJdtrna3tuzKF1LEPv1++Yw9OVSB0fPxnPfZwELceV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3pAlfqX65T1rHjN47c1o+h9b2mICgUjgdU6vcMIEc0GBr6V1/bVAOxZZrSUp/lliG17S755uTfZftTDCw31vFrq//c/Ysd/5z6H3r8cs7BNksFQuE2h+Q95PzMSQ8/FxABazfJA9RNF8XqAlvvJgg/G7MMqYtz3+2nyp9KD8tV7ypZi7YuntTxr+eRSkuVob5+vf67BkJdDc137HINPT/n+H4ClOR0nHi2+qF/MZZ4Ea7fIJpu3NVfzGvWVP5q31L7t+UWgylahcYw9vm5xmAdoIvJxxmqLqJbu5jGbjhNy2xNTrdXZa3HP7f7p7RWoXq+U5+fU7e6Aczhvpts6tahrsIL+RzdHF2Ff1HnBASpv8Cb0nNbX8ZWev2nhOUfUnepY5uqj9Txm6o+5zw/w20ArNXxr//9o0v1y5c+ezi8ePHi8I2vfy26cO5FL/b8R9dlwgttn4tu2w2o6UYxJP+m8jel15fpk55btiF5NNV/vWyx5Zu2m1P/Yx6/oTft3PK1nQOp8yNcZszjl7v9vukCIWALosHQ3e3NkmXi4GYzxBjBLgD7YW6yQrlx9xc+7xN+BgAxgiE2SfADQK5JxhkCAFgLwRAAsGv/D3qj865EUayrAAAAAElFTkSuQmCCAA==" alt="" width="624" height="258" />  

当使用ORDER BY NULL禁止排序后,Using filesort不存在

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAnsAAAD0CAYAAADjTeCzAAAWn0lEQVR4nO3dW7LktnkA4O5TZ2F68gq8A1UlqaTy4kqe7JmR9HBWcHZwNqJ9xJEdP8bXSiq+anyiLg1HEAQQIAne0N9XpdJMgwR/guzmPwBJXP/wp7+//uu//+zy8vx0AQCgLw97BwAAwHokewAAHXu8XvcO4Ttffvnl9/7+ySefJMvjz4+iFP/Ueo62n632b22l9gv346z7cEQtYj7DsTm6WxveW9s5b9rZoy3PcPxq85Nc+RYxjHm4vLYNZolb4GPBD2XxDh9FKf6z22L/tji2Rz9GZ0z0bpZ+P+P9Pur3/Mjutc3O9l05sj3a8gzHryY/afUP3bHycDtT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okewCUXH/5u/evb96+uftkj3VNTQb3cMSYUs4SJwDH4J49NjH0Xq15z+ES8XD9kWK7mdrzBwADyR6bOXKCcuTYbo4eHwDH5QENAICOSfYAADom2QMA6JhkDwCgY8kHNI74NOIU9/5qCsdvnRhK7+GrrTO3/tL613aE4wLAdD/o2ett1oGjWbt9Hb/lSi9gHv4LP59SZ2r9pfVv4YgxAVD2sWevpx/wo/Y8rNnGjl8btb1XS9u7tP5Rj2f4vsSjfs8A+L6Pyd7Sf7XXTPW1dDqrKTGUtj2l3pr6p2x/jRcLtzp+U6YTy+1f6vjXtP/S41dzDo45e6K35PysKQ8/l/ABnEezBzRyP/q5Yarws5YxlC4+c7dfir9UHsYVD9WdxdgQY+n4x3WUykoxxOvH7T810Ztqab01+znF0vNzi+8nAPtY5Wncpf/iP+JFJr4A5hKTocfjzHLxD/tW0zN1tkS21lF79Vqfn2f8xwgAaatMlzZcJHq7WKQuqLllelMzLDs46/GvHZ48Wq9evN7a5+favaYAtHWY9+yFF6othuGmCntEahKeUg9Krry296y1o7d/SRj/krYrHdtSe5SO31qJ3pbn55I4Adje9Ze/f//65s2by6c//lFygdof9dT9V1OXCS8kcy4qYxfY3IVwSf25+HPl8TJzymtjW1JHrv3j2FLL57Zb0/4tj9/SpKQ2vrFzoHR+zKl/zNLzc+oxBOAcPiZ7L89Pe8dy91xM52uRzANAj1a5Z495JCbzhffbhZ8BwL2T7NENyR0A/NBhHtAAAKA9yR4AQMckewAAHZPsAQB0TLIHANCx/wdwiwa4WGTQNQAAAABJRU5ErkJgggA=" alt="" />

4、ORDER BY 的优化  

MySQL可以使用一个索引来满足ORDER BY 子句的排序,而不需要额外的排序,但是需要满足以下几个条件:

(1)WHERE 条件和OREDR BY 使用相同的索引:即key_part1与key_part2是复合索引,where中使用复合索引中的key_part1

SELECT*FROM user WHERE key_part1=1 ORDER BY key_part1 DESC, key_part2 DESC;

(2)而且ORDER BY顺序和索引顺序相同:

SELECT*FROM user ORDER BY key_part1, key_part2;

(3)并且要么都是升序要么都是降序:

SELECT*FROM user ORDER BY key_part1 DESC, key_part2 DESC;

但以下几种情况则不使用索引:

(1)ORDER BY中混合ASC和DESC:

SELECT*FROM user ORDER BY key_part1 DESC, key_part2 ASC;

(2)查询行的关键字与ORDER BY所使用的不相同,即WHERE 后的字段与ORDER BY 后的字段是不一样的

SELECT*FROM user WHERE key2 = ‘xxx’ ORDER BY key1;

(3)ORDER BY对不同的关键字使用,即ORDER BY后的关键字不相同

SELECT*FROM user ORDER BY key1, key2;

5、OR的优化

当MySQL使用OR查询时,如果要利用索引的话,必须每个条件列都使独立索引,而不是复合索引(多列索引),才能保证使用到查询的时候使用到索引。

比如我们新建一张用户信息表user_info

mysql> select*from user_info;
+---------+--------+----------+-----------+
| user_id | idcard | name | address |
+---------+--------+----------+-----------+
| 1 | 111111 | Zhangsan | Kunming |
| 2 | 222222 | Lisi | Beijing |
| 3 | 333333 | Wangwu | Shanghai |
| 4 | 444444 | Lijian | Guangzhou |
+---------+--------+----------+-----------+
4 rows in set

之后创建ind_name_id(user_id, name)复合索引、id_index(id_index)独立索引,idcard主键索引三个索引。

mysql> show index from user_info;
+-----------+------------+-------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-----------+------------+-------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| user_info | 0 | PRIMARY | 1 | idcard | A | 4 | NULL | NULL | | BTREE | | |
| user_info | 1 | ind_name_id | 1 | user_id | A | 4 | NULL | NULL | | BTREE | | |
| user_info | 1 | ind_name_id | 2 | name | A | 4 | NULL | NULL | YES | BTREE | | |
| user_info | 1 | id_index | 1 | user_id | A | 4 | NULL | NULL | | BTREE | | |
+-----------+------------+-------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
4 rows in set

测试一:OR连接两个有单独索引的字段,整个SQL查询才会用到索引(index_merge),并且我们知道OR实际上是把每个结果最后UNION一起的。

mysql> explain select*from user_info where user_id=1 or idcard='';
+----+-------------+-----------+------------+-------------+------------------------------+---------------------+---------+------+------+----------+----------------------------------------------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-----------+------------+-------------+------------------------------+---------------------+---------+------+------+----------+----------------------------------------------------+
| 1 | SIMPLE | user_info | NULL | index_merge | PRIMARY,ind_name_id,id_index | ind_name_id,PRIMARY | 4,62 | NULL | 2 | 100 | Using sort_union(ind_name_id,PRIMARY); Using where |
+----+-------------+-----------+------------+-------------+------------------------------+---------------------+---------+------+------+----------+----------------------------------------------------+
1 row in set

测试二:OR使用复合索引的字段name,与没有索引的address,整个SQL都是ALL全表扫描的

mysql> explain select*from user_info where name='Zhangsan' or address='Beijing';
+----+-------------+-----------+------------+------+---------------+------+---------+------+------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-----------+------------+------+---------------+------+---------+------+------+----------+-------------+
| 1 | SIMPLE | user_info | NULL | ALL | NULL | NULL | NULL | NULL | 4 | 43.75 | Using where |
+----+-------------+-----------+------------+------+---------------+------+---------+------+------+----------+-------------+
1 row in set

交换OR位置并且使用另外的复合索引的列,也是ALL全表扫描:

mysql> explain select*from user_info where address='Beijing' or user_id=1;
+----+-------------+-----------+------------+------+----------------------+------+---------+------+------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-----------+------------+------+----------------------+------+---------+------+------+----------+-------------+
| 1 | SIMPLE | user_info | NULL | ALL | ind_name_id,id_index | NULL | NULL | NULL | 4 | 43.75 | Using where |
+----+-------------+-----------+------------+------+----------------------+------+---------+------+------+----------+-------------+
1 row in set

6、优化嵌套查询

使用嵌套查询有时候可以使用更有效的JOIN连接代替,这是因为MySQL中不需要在内存中创建临时表完成SELECT子查询与主查询两部分查询工作。但是并不是所有的时候都成立,最好是在on关键字后面的列有索引的话,效果会更好!

比如在表major中major_id是有索引的:

select * from student u left join major m on u.major_id=m.major_id where m.major_id is null;

而通过嵌套查询时,在内存中创建临时表完成SELECT子查询与主查询两部分查询工作,会有一定的消耗

select * from student u where major_id not in (select major_id from major);

7、使用SQL提示

SQL提示(SQL HINT)是优化数据库的一个重要手段,就是往SQL语句中加入一些人为的提示来达到优化目的。下面是一些常用的SQL提示:

(1)USE INDEX:使用USE INDEX是希望MySQL去参考索引列表,就可以让MySQL不需要考虑其他可用索引,其实也就是possible_keys属性下参考的索引值

mysql> explain select* from user_info use index(id_index,ind_name_id) where user_id>0;
+----+-------------+-----------+------------+------+----------------------+------+---------+------+------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-----------+------------+------+----------------------+------+---------+------+------+----------+-------------+
| 1 | SIMPLE | user_info | NULL | ALL | ind_name_id,id_index | NULL | NULL | NULL | 4 | 100 | Using where |
+----+-------------+-----------+------------+------+----------------------+------+---------+------+------+----------+-------------+
1 row in set mysql> explain select* from user_info use index(id_index) where user_id>0;
+----+-------------+-----------+------------+------+---------------+------+---------+------+------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-----------+------------+------+---------------+------+---------+------+------+----------+-------------+
| 1 | SIMPLE | user_info | NULL | ALL | id_index | NULL | NULL | NULL | 4 | 100 | Using where |
+----+-------------+-----------+------------+------+---------------+------+---------+------+------+----------+-------------+
1 row in set

(2)IGNORE INDEX忽略索引

我们使用user_id判断,用不到其他索引时,可以忽略索引。即与USE INDEX相反,从possible_keys中减去不需要的索引,但是实际环境中很少使用。

mysql> explain select* from user_info ignore index(primary,ind_name_id,id_index) where user_id>0;
+----+-------------+-----------+------------+------+---------------+------+---------+------+------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-----------+------------+------+---------------+------+---------+------+------+----------+-------------+
| 1 | SIMPLE | user_info | NULL | ALL | NULL | NULL | NULL | NULL | 4 | 33.33 | Using where |
+----+-------------+-----------+------------+------+---------------+------+---------+------+------+----------+-------------+
1 row in set

(3)FORCE INDEX强制索引

比如where user_id > 0,但是user_id在表中都是大于0的,自然就会进行ALL全表搜索,但是使用FORCE INDEX虽然执行效率不是最高(where user_id > 0条件决定的)但MySQL还是使用索引。

mysql> explain select* from user_info where user_id>0;
+----+-------------+-----------+------------+------+----------------------+------+---------+------+------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-----------+------------+------+----------------------+------+---------+------+------+----------+-------------+
| 1 | SIMPLE | user_info | NULL | ALL | ind_name_id,id_index | NULL | NULL | NULL | 4 | 100 | Using where |
+----+-------------+-----------+------------+------+----------------------+------+---------+------+------+----------+-------------+
1 row in set

之后强制使用独立索引id_index(user_id):

mysql> explain select* from user_info force index(id_index) where user_id>0;
+----+-------------+-----------+------------+-------+---------------+----------+---------+------+------+----------+-----------------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-----------+------------+-------+---------------+----------+---------+------+------+----------+-----------------------+
| 1 | SIMPLE | user_info | NULL | range | id_index | id_index | 4 | NULL | 4 | 100 | Using index condition |
+----+-------------+-----------+------------+-------+---------------+----------+---------+------+------+----------+-----------------------+
1 row in set

总结

  (1)很多时候数据库的性能是由于不合适(是指效率不高,可能会导致锁表等)的SQL语句造成,本篇博文只是介绍简单的SQL优化

  (2)其中有些优化在真正开发中是用不到的,但是一旦出问题性能下降的时候需要去一一分析。

MySQL优化(2)--------常用优化的更多相关文章

  1. MySQL基础操作&&常用的SQL技巧&&SQL语句优化

    基础操作     一:MySQL基础操作         1:MySQL表复制             复制表结构 + 复制表数据             create table t3 like t ...

  2. MySQL 基础及性能优化工具

    数据库,用户及权限 常用用户管理操作 # 创建本地用户 abc create user abc@localhost # 创建内网能够访问的用户 abc create user abc@'192.168 ...

  3. [MySQL Reference Manual] 8 优化

    8.优化 8.优化 8.1 优化概述 8.2 优化SQL语句 8.2.1 优化SELECT语句 8.2.1.1 SELECT语句的速度 8.2.1.2 WHERE子句优化 8.2.1.3 Range优 ...

  4. MYSQL的深入学习--优化步骤

    MySql优化的一般步骤 1.通过show status 命令了解各种sql的执行效率 SHOW STATUS提供msyql服务器的状态信息 一般情况下,我们只需要了解以”Com”开头的指令 show ...

  5. 如何处理PHP和MYSQL的并发以及优化

    sql优化,数据缓存和页面静态化首先各种优化程序逻辑优化数据库优化硬件横向扩展数据hash.服务器提升性能.表hash.出钱找oraclec出解决方案页面静态化:Php页面静态化有两种,第一,php模 ...

  6. MySQL存储引擎,优化,事务

    1唯一约束unique和主键key的区别?     1.什么是数据的存储引擎?       存储引擎就是如何存储数据.如何为存储的数据建立索引和如何更新.查询数据等技术的实现方法.因为在关系数据库中数 ...

  7. MySQL优化二(连接优化和缓存优化)

    body { font-family: Helvetica, arial, sans-serif; font-size: 14px; line-height: 1.6; padding-top: 10 ...

  8. 第 8 章 MySQL 数据库 Query 的优化

      前言: 在之前“影响 MySQL 应用系统性能的相关因素”一章中我们就已经分析过了Query语句对数据库性能的影响非常大,所以本章将专门针对 MySQL 的 Query 语句的优化进行相应的分析. ...

  9. SQL优化 MySQL版 - 多表优化及细节详讲

    多表优化及细节详讲 作者 : Stanley 罗昊 [转载请注明出处和署名,谢谢!] 注:本文章需要MySQL数据库优化基础或观看前几篇文章,传送门: B树索引详讲(初识SQL优化,认识索引):htt ...

随机推荐

  1. spring BeanWrapperImpl方便的嵌套属性(list)操作

    beans 包主要提供了接口和类用于处理java beans.     其中最主要的接口是BeanWrapper:     Spring 的中心接口,用于访问javabeans 的低层操作.默认实现为 ...

  2. HTML和CSS初级前端面试题汇总(持续补充)

    1.浏览器内核 IE:trident Firefox:gecko Safari:webkit Opera:以前是presto,现在是Blink Chrome:Blink 2.HTML文件开头的DOCT ...

  3. [转].NET Core、Xamarin、.NET Standard和.NET Framework四者之间的区别

    转至:https://segmentfault.com/a/1190000011539920 前段时日微软(Microsoft)正式发布了.NET Core 2.0,在很多开发社区中反响不错.但还是有 ...

  4. Centos7 网络报错Job for iptables.service failed because the control process exited with error code.

    今天在进行项目联系的时候,启动在待机的虚拟机,发现虚拟机的网络设置又出现了问题. 我以为像往常一样重启网卡服务就能成功,但是它却报了Job for iptables.service failed be ...

  5. 装mongondb数据库

    装mongondb数据库装好以后进入c盘mongondb bin里边 复制地址 将其放入环境变量里边 放之前需要往前边加一个英语的;在 d盘 建一个data文档 里边简历一个db文件夹 cmd命令框输 ...

  6. Android-shape圆形&转圈圈

    圆形: <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="h ...

  7. C#轻量级通通讯组件StriveEngine —— C/S通信开源demo(2) —— 使用二进制协议 (附源码)

    前段时间,有几个研究ESFramework通信框架的朋友对我说,ESFramework有点庞大,对于他们目前的项目来说有点“杀鸡用牛刀”的意思,因为他们的项目不需要文件传送.不需要P2P.不存在好友关 ...

  8. Oracle Data Guard配置

    Oracle Data Guard 的配置在网上有很多资料,但是没有一个完整的,配置下来多少有些问题.在踩了各种坑之后,自己终于配置成功,就想把这过程记录下来. 1   测试环境 主数据库:windo ...

  9. 背水一战 Windows 10 (96) - 选取器: ContactPicker

    [源码下载] 背水一战 Windows 10 (96) - 选取器: ContactPicker 作者:webabcd 介绍背水一战 Windows 10 之 选取器 ContactPicker(联系 ...

  10. Akka-CQRS(1)- Write-side, Persisting event sources:CQRS存写端操作方式

    上篇我们提到CQRS是一种读写分离式高并发.大流量数据录入体系,其中存写部分是通过event-sourcing+akka-persistence实现的.也可以这样理解:event-sourcing(事 ...