MySQL|8分钟带你深入了解MySQL是如何利用索引的,网友:大师,我悟了( 二 )
where 后面在使用or 查询的时候 , 大部分情况下不会走到索引 。 所以 , 对于这种查询 , 可以使用union来优化In many cases MySQL won't be able to use an index to apply an OR condition and as a result this query is not index-able.Therefore we recommend to avoid such OR conditions and consider splitting the query to two parts combined with a UNION DISTINCT (or even better UNION ALL in case you know there won't be any duplicate results)
建索引时 , 范围字段放在联合索引的最后 , 因为按照最左前缀匹配原则 , 碰到范围字段就终止匹配了 , 后面的字段不会去匹配 。
区分度大的字段在建索引时放在前面 。区分度公式:count(distinct col)/count(*) , 就是一个字段当选择了一个值时 , 要能过滤掉大部分字段 。
mysql NULL
- 【MySQL|8分钟带你深入了解MySQL是如何利用索引的,网友:大师,我悟了】NULL is not data type
- NULL is a value place holder for optional table fields.
- MySQL treats the NULL value differently from other data types. The NULL values when used in a condition evaluates to the false Boolean value.
- Performing arithmetic operations on NULL values always returns NULL results.
- The comparison operators such as [ = etc.
cannot be used to compare NULL values. - ‘+ - * / = != ’这些操作在作用于NULL时 , 永远返回NULL , 在返回NULL做条件判断时返回false 。
- 对于NULL的判断 , 使用is NULL 和is not NULL来判断 , 使用=,!=都不会得到你想要的结果 。
- 建表时 , 尽量所有字段都设置为非NULL , 设为NULL时 , mysql还需要额外使用字段来标记是否为NULL 。
锁为了解并发问题 , 引入锁 , mysql中锁分为读锁和写锁 , 即share lock和exclusive lock 。 顾名思义 , share lock之间不互斥 , share lock和exclusive lock之间互斥 , exclusive lock之间互斥 。 mysql 提供行锁row lock和表锁 table lock的multiple granularity locking 。
对于表锁 , mysql提供一种意图锁的机制 , 意图锁也是分为两种 , intention share lock和intention exclusive lock 。 对于intention lock
- Before a transaction can acquire a shared lock on a row in a table it must first acquire an IS lock or stronger on the table.
- Before a transaction can acquire an exclusive lock on a row in a table it must first acquire an IX lock on the table.
- Intention locks do not block anything except full table requests (for example LOCK TABLES ... WRITE). The main purpose of intention locks is to show that someone is locking a row or going to lock a row in the table.
- intention lock之间并不互斥 , intention lock只是告诉你有人对表中的某些行在上锁 。
事务事务是指一批操作 , 要么全部成功 , 要么全部失败 。
数据库事务的ACID特性
- atomicity原子性:即一个事务已一个原子的操作执行 , 是一个不可分隔的最小单元 , 事务中的操作 , 要么全部执行成功 , 要么全部失败 。
- consistency 一致性:数据库总是从一个一致的状态转移到另一个一致的状态
- isolation: 隔离性:一个事务中的修改 , 在什么时候对另一个事务可见
- durability: 持久性: 提交的事务不会丢失
隔离级别是对不同的事务而言的 。
- read uncommitted:一个事务中未提交的修改也对另外的事务可见 , 在这里隔离级别下 , 会出现脏读 , 即事务1未提交的修改可能被别的事务可见 。
- MySQL|华为不再孤单?国产巨头杀入5G领域,市值暴涨4千亿超越腾讯!
- MySQL|它可能是300元内最值得购入的耳机
- 卢伟冰不讲“武德”,跑分75万+38分钟满电+OIS防抖,12G+256G仅售2364元
- 太阳光到达地球需要8分钟,人一眼就能看到太阳,眼速比光速快?
- MySQL|中企接连行动,苹果始料未及,央媒的呼吁起作用了?
- MySQL|程序员应知应会之MySQL的存储引擎
- MySQL|关于有手机厂商库存严重这件事,我没有证据不敢乱说是哪一家
- MySQL|果然还得是华为,mate50 Pro即将发布,麒麟+鸿蒙3.0+支持5G网络
- MySQL|千元机天花板,120Hz高刷屏+天玑1200处理器,价格绝对是无可比拟的。
- MySQL|为什么懂行的买苹果三星,半懂的华为,外行的买小米?真是这样?
